Force “F2”+“Enter” on range of cells

后端 未结 12 1720
伪装坚强ぢ
伪装坚强ぢ 2021-01-04 05:31

I have an Excel 2010 worksheet which has macros to copy data from other sheets into a specific format on another sheet.

The data copies but I have an issue with the

相关标签:
12条回答
  • 2021-01-04 05:44

    This worked for me.

    Dim r As Range
    Dim n As Integer
    Dim AssDateLastRow As Long
    
    AssDateLastRow = ActiveSheet.Range("E" & Rows.Count).End(xlUp).Row
    
    Set r = Range("E2:E" & AssDateLastRow)
    r.Select
    r.NumberFormat = "ddmmyyyy;@"
    r.Select
    For n = 1 To r.Rows.Count
        SendKeys "{F2}", True
        SendKeys "{ENTER}", True
    Next n
    
    0 讨论(0)
  • 2021-01-04 05:44

    Sendkeys are not stable. The better way is to store the text in the clipboard and paste it.

    See here on how to store values in the clipboard

    Sub CopyText(Text As String)
    Dim MSForms_DataObject As Object
    Set MSForms_DataObject = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    MSForms_DataObject.SetText Text
    MSForms_DataObject.PutInClipboard
    Set MSForms_DataObject = Nothing
    End Sub
    
    Sub Test()
    CopyText (ActiveCell.Value)
    ActiveCell.PasteSpecial
    End Sub
    'In place of active cell, you may pass a range
    
    0 讨论(0)
  • 2021-01-04 05:44

    I just got it, Simple
    Select all the cells you want to hit F2 and Enter and run this short macro:

    Sub AutoF2Enter()
    Selection.Value = Selection.Value
    End Sub

    Works on date and numbers!
    50.000 cells in a second!

    0 讨论(0)
  • 2021-01-04 05:46

    My variation

    n = Selection.Rows.count
    Dim r1 As range, rv As range
    Set r1 = Selection.Cells(1, 1)
    For I = 1 To n
    Set rv = r1.offset(I - 1, 0)
    vali = rv.value
     IsNumeric(vali) Then
     vali = CDbl(vali)
     rv.value = 0
     rv.value = vali
     End If
    
    0 讨论(0)
  • 2021-01-04 05:47

    Try to press F9 or File-Option-formulas-workbook calculation- automatic

    0 讨论(0)
  • 2021-01-04 05:48

    I can think of two options to get Excel to apply the formatting to the cells in one step.

    The first is to use the Text to columns functionality even though there is nothing in the column to split. The second option is to copy a value of 1 and paste it into the cells using the Paste Special - Multiply option.

    Although either method should force an update of the cell formating, I would lean towards the first option. This is incase some of your dates are is stored as text.

        Sub Format_Text_to_Columns()
    
        Dim AssDateLastRow As Long
    
        AssDateLastRow = ActiveSheet.Range("C" & Rows.Count).End(xlUp).Row
        ActiveSheet.Range("C4:C" & AssDateLastRow).NumberFormat = "yyyy-mm-dd;@"
        'Set the format
    
            Range("C4:C" & AssDateLastRow).Select
            Selection.TextToColumns DataType:=xlDelimited, ConsecutiveDelimiter:=True, _
             Space:=True, FieldInfo:=Array(1, 5)
        'Use text to columns to force a format update
    
        End Sub
    
    
    
        Sub Format_Paste_Special_Multiply()
    
        Dim AssDateLastRow As Long
    
        AssDateLastRow = ActiveSheet.Range("C" & Rows.Count).End(xlUp).Row
        ActiveSheet.Range("C4:C" & AssDateLastRow).NumberFormat = "yyyy-mm-dd;@"
        'Set the format
    
            Range("C1").FormulaR1C1 = "1"
            Range("C1").Copy
            Range("C4:C" & AssDateLastRow).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlMultiply
            Application.CutCopyMode = False
            Range("C1").ClearContents
        'Multiply the dates by 1 to force a format update
    
        End Sub
    
    0 讨论(0)
提交回复
热议问题