Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA-循环浏览表并使用列名更新另一列_Vba_Excel - Fatal编程技术网

VBA-循环浏览表并使用列名更新另一列

VBA-循环浏览表并使用列名更新另一列,vba,excel,Vba,Excel,在ExcelVBA中,我尝试循环遍历Excel表,然后更新该行中的另一列。如果布局发生变化,我希望通过名称而不是偏移量/编号来引用另一列。可能吗 Dim rw As ListRow Dim cl As Range For Each rw In ActiveSheet.ListObjects("MasterData").ListRows 'Pseudo code: if (rw[Notes] = "Test") then DO SOMETHING 'In this line I

在ExcelVBA中,我尝试循环遍历Excel表,然后更新该行中的另一列。如果布局发生变化,我希望通过名称而不是偏移量/编号来引用另一列。可能吗

Dim rw As ListRow
Dim cl As Range

For Each rw In ActiveSheet.ListObjects("MasterData").ListRows
    'Pseudo code:
     if (rw[Notes] = "Test") then DO SOMETHING 'In this line I'd like to refer to the column called "Notes" but not sure how to do it.
Next rw
这项工作的90%将由工程师完成。其余情况如下:

Option Explicit

Sub TestMe()

    Dim rw          As Range
    Dim cl          As Range
    Dim rngCell     As Range

    For Each rw In [MyTable].Rows
        Set rngCell = Intersect(rw, [MyColumnName])
        If Not rngCell Is Nothing Then
            If rngCell = "Text" Then Debug.Print rngCell.Address
        End If
    Next rw

End Sub

  • [MyTable]是表的名称
  • [MyColumnName]是列的名称
将完成约90%的工作。其余情况如下:

Option Explicit

Sub TestMe()

    Dim rw          As Range
    Dim cl          As Range
    Dim rngCell     As Range

    For Each rw In [MyTable].Rows
        Set rngCell = Intersect(rw, [MyColumnName])
        If Not rngCell Is Nothing Then
            If rngCell = "Text" Then Debug.Print rngCell.Address
        End If
    Next rw

End Sub

  • [MyTable]是表的名称
  • [MyColumnName]是列的名称