You can use the following VBA code to accomplish your goal much more easily than generating code for individual ActiveX or Form Control buttons.
The following VBA allows the user to double-click a cell in a designated column (the "button column") and the code will copy the values on the same row the user double-clicked from the source to destination columns you specify. This code works on all rows in your worksheet without requiring you to modify the code in any way.
Don't be thrown off by the code's repeated references to cells on row 1 (e.g. O1
and K1
). In each of these cases, the .Column
property is used because we're only interested in the column in question. The row that will be manipulated is always the same row the user double-clicks.
In the Excel VBA Editor (press Alt+F11 to open) navigate to your worksheet's module, then paste in the following code:
Private Sub
Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
On Error GoTo Quit
'Copy values between columns on same row that user double-clicks
'Quit if Target range includes more than one cell
If Target.Count > 1 Then Exit Sub
With ActiveSheet
'Here, specify the worksheet column the user can double-click to activate this
'routine. For the "B" column, in the Range method specify cell "B1" (all cells
'in column will be used, not just that on row 1).
If Target.Column = .Range("B1").Column Then
.Cells(Target.Row, .Range("O1").Column) = .Cells(Target.Row, .Range("K1").Column)
.Cells(Target.Row, .Range("R1").Column) = .Cells(Target.Row, .Range("N1").Column)
Cancel = True 'Cancel the double-click
End If
'Here you can specify a second "button column" with its own set of copy actions.
If Target.Column = .Range("C1").Column Then
.Cells(Target.Row, .Range("E1").Column) = .Cells(Target.Row, .Range("F1").Column)
.Cells(Target.Row, .Range("G1").Column) = .Cells(Target.Row, .Range("H1").Column)
Cancel = True 'Cancel the double-click
End If
End With
Quit:
End Sub
As written, the code will be triggered when the user double-clicks any cell in column B
. To change this, modify this line of code to point to the column you wish to act as your "button column":
If Target.Column <> .Range("B1").Column Then Exit Sub
The rest of the code should be self-explanatory, but ask any questions you have.
Edit: I changed the VBA to permit two button columns, each able to copy values between different source and destination columns.