【问题标题】:VBA - loop through a table and update another column using column nameVBA - 遍历表并使用列名更新另一列
【发布时间】:2017-08-31 19:31:49
【问题描述】:

在 Excel VBA 中,我尝试遍历 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

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    Intersect function 将完成大约 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] 是列的名称。

    【讨论】:

      猜你喜欢
      • 2017-04-09
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多