简体   繁体   English

Excel VBA宏,复制行中的第一个值

[英]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. 我正在尝试编写一个宏,它将复制我选择的单元格的同一行的A列中的值。

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. 并且所选择的单元格是包含123的单元格,它将复制约翰。 If I selected the 456 cell, it would still copy John. 如果我选择456单元格,它仍然会复制约翰。 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. 我理解Selection.Offset( ,-3).Copy函数(将单元格三列复制回来,与当前选择相同的行)但我不是每次都在同一列中开始,所以我想我需要一个更清晰的引用。

Thanks for the help! 谢谢您的帮助!

Add the code below in the Worksheet_SelectionChange event of your relevant sheet: 在相关Worksheet_SelectionChange事件中添加以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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