如何为 Excel VBA 中的目标查找同一行第一个单元格的值?

How to find values for 1st cell of same row for a target in Excel VBA?

检索目标单元格列 Header 和行 Header(在本例中,它是行的第一个值)我在 VBA 中使用以下代码:-

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Dim ColumnHeader As String
    Dim RowHeader As String

' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
    Set KeyCells = Range("A1:G106")

If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then

' Display a message when one of the designated cells has been
        ' changed.
        ' Place your code here.
        MsgBox "Cell " & Target.Address & " has changed."
        ColumnHeader = Cells(1, Target.Column)
        RowHeader = Cells(1, Target.Row)
        MsgBox ColumnHeader
        MsgBox RowHeader

End If
End Sub

我只获取列Header 的值。我在检索 RowHeader 值时遗漏了什么吗?

示例:

如果我更改 $C3,它应该分别获得值 'LINE2' 和 '222'

试试这个..

RowHeader = Cells(1, Target.Row) 替换为 RowHeader = Cells(Target.Row,1) ... 单元格称为 Cells(RowNumber,ColumnsNumber)