简体   繁体   中英

Excel VBA Macro, copy the first value in row

I am trying to write a macro that will copy the value in column A of the same row of the cell I have selected.

So for example, if my spreadsheet looked like:

| John | Doe | 123 | 456 | Apples |

and the selected cell was the one containing 123, it would copy John. If I selected the 456 cell, it would still copy John. Make sense?

I understand the Selection.Offset( ,-3).Copy function (would copy the cell three columns back, same row as current selection) but I'm not starting in the same column every time, so I think I need a more distinct references.

Thanks for the help!

Add the code below in the Worksheet_SelectionChange event of your relevant sheet:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

' copy the cell in Column "A" of the same row clicked
Range("A" & Target.Row).Copy

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM