Forum Discussion

JDGriz's avatar
JDGriz
Copper Contributor
Jun 03, 2023

VBA Macro

I'm trying to record a simple macro with keystrokes of: "HOME", CTRL+C, CTRL+G, CTRL+V, ENTER

So far I have not been successful in recording this simple macro. Any help for this?

 

3 Replies

  • SnowMan55's avatar
    SnowMan55
    Bronze Contributor

    JDGriz 

    What is usually important is not emulating those keystrokes, but accomplishing the actions represented by them. It seems you are trying to go a destination address that is stored at the start of the currently-selected row.


    Consider this code instead:

    Sub GoIndirectly1()
    
        Dim in4CurrentRow   As Long
        Dim strDestination  As String
        Dim in4PosnOfExcMrk As Long
        '
        Dim strSheetName    As String
        Dim strCellAddress  As String
        
        '----   Determine the destination from the current selection.
        in4CurrentRow = Selection.Row
        strDestination = Range("A" & in4CurrentRow).Value
        '   OTOH, if your use of the HOME key is intended to take the
        '   user to a different column (e.g., because some early
        '   columns are hidden, or because Freeze Panes has been used)
        '   ...if you can't determine the column programmatically,
        '   you may need to try the SendKeys method of the Application
        '   object.  Avoid that if possible.
        '  --   Perform a trivial validation on the value obtained.
        If Len(strDestination) < 2 Then
            Call MsgBox("Invalid destination was found in row " _
                    & Format$(in4CurrentRow, "#,###,##0") _
                    , vbExclamation Or vbOKOnly, "GoIndirectly1")
            Exit Sub
        End If
        '   If you really need the destination in the copy/paste buffer,
        '   you can try including a Range.Copy method.  But I'll assume
        '   that is a side effect that you do not require.
        
        '----   Determine what the destination really consists of.
        '       [Depending on how you specified it...
        in4PosnOfExcMrk = InStr(1, strDestination, "!")
        If in4PosnOfExcMrk > 0 Then
            '...it's an address in another worksheet.  The "Go To" dialog
            '   requires those cell address to be in A1 (not R1C1) format.
            '   But the Application.GoTo method (below) is more flexible.
            strSheetName = Left$(strDestination, in4PosnOfExcMrk - 1)
            strCellAddress = Mid$(strDestination, in4PosnOfExcMrk + 1)
            '   Because the worksheet name might be wrapped in
            '   apostrophes, and might include doubled apostrophes:
            If Right$(strSheetName, 1) = "'" _
            And Left$(strSheetName, 1) = "'" _
            Then
                strSheetName = Mid$(strSheetName, 2, Len(strSheetName) - 2)
                strSheetName = Replace(strSheetName, "''", "'")
            End If
        Else
            strCellAddress = strDestination
        End If
        
        '----   Go there.
        If UCase$(Left$(strCellAddress, 1)) = "R" _
        And IsNumeric(Mid$(strCellAddress, 2, 1)) _
        And InStr(3, strCellAddress, "C", vbTextCompare) > 0 _
        Then
            '...the cell address is in R1C1 notation.
            Application.Goto strDestination
        Else
            '...the cell address is in A1 notation.
            If Len(strSheetName) > 0 Then
                Application.Goto Worksheets(strSheetName).Range(strCellAddress)
            Else
                Application.Goto Range(strCellAddress)
            End If
        End If
    
    End Sub

     

    If you really need keystrokes, which I have not always found reliable, then see, e.g., this article: How to Use Excel SendKeys Method in Macros

    • JDGriz's avatar
      JDGriz
      Copper Contributor

      SnowMan55 

      Thank you for putting this together. I works perfectly for what I needed it to do. You anticipated what my keystrokes were intended to do. 

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    JDGriz 

    To record a VBA macro with the keystrokes you mentioned (HOME, CTRL+C, CTRL+G, CTRL+V, ENTER), follow these steps:

    1. Open the Excel workbook where you want to record the macro.
    2. Press the "Developer" tab on the Excel ribbon. If you do not see the "Developer" tab, you may need to enable it first. Go to "File" > "Options" > "Customize Ribbon" and check the "Developer" option.
    3. Click on the "Record Macro" button in the "Code" group. This will open the "Record Macro" dialog box.
    4. Enter a name for your macro in the "Macro name" field (e.g., "CopyPasteMacro").
    5. Optionally, you can assign a shortcut key to the macro by typing a letter or number in the "Shortcut key" field.
    6. Choose where to store the macro. You can either store it in the current workbook ("This Workbook") or in your personal macro workbook ("Personal Macro Workbook").
    7. Click on the "OK" button to start recording the macro.
    8. Press the following keystrokes one by one:
      • Press the "HOME" key to move to the beginning of the worksheet.
      • Press "CTRL+C" to copy the selected cell or range.
      • Press "CTRL+G" to open the "Go To" dialog box.
      • Press "CTRL+V" to paste the copied content.
      • Press "ENTER" to confirm the paste operation.
    9. Click on the "Stop Recording" button in the "Code" group on the "Developer" tab to stop recording the macro.

    Your macro is now recorded and ready to be used. You can run it by pressing the assigned shortcut key (if any) or by going to the "Developer" tab, clicking on "Macros," selecting your macro, and clicking "Run."

    Please note that recording macros in Excel may not capture certain actions or keystrokes accurately, especially if they involve complex interactions or depend on specific conditions. In such cases, you may need to manually write the VBA code to achieve the desired functionality.

Resources