Home > Blockchain >  excel VBA Sendkey F2 for range of cells
excel VBA Sendkey F2 for range of cells

Time:06-16

I have been looking for a straightforward and reliable method to do sendkey "F2" on range of cells.

I want users to be sent directly to edit mode when selecting certain cells, which will allow them to copy or enter data in a more user friendly manner.

Kind regards, Luke

CodePudding user response:

I am not sure if it is a good idea to change into "edit mode" for some specific cells. The only difference is that the cursor gets visible and jumps to the end of the cell content if the cell is not empty. While it may be convenient to immediately append data, the price is that it is not possible to simply press Ctrl C to copy the cell content. However, I will not argue with you.

As already written in the comment, use the Selection_Change-Event and check the parameter target if one of your "certain cells" was selected. If target.count is greater that 1, the user selected multiple cells - you probably don't want to enter Edit mode in that case.

The following example sends F2 if a cell in column B was selected - adapt it to your needs. You need to put the code into the sheet module of the sheet where you want the magic to happen and change the If-condition to your needs.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    
    If Target.Column = 2 Then ' Change this to check for the cells you are interested in
        Application.SendKeys "{F2}"
    End If
End Sub

CodePudding user response:

Thanks for your help everyone. Found the answer I was looking for on another forum.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.CountLarge = 1 Then
    If Not Excel.Application.Intersect(Target, Range("CertainCells")) Is Nothing Then
        VBA.SendKeys "{F2}"
    End If
End If
End Sub
  • Related