Excel 在VBA中使用列名称

Excel 在VBA中使用列名称,excel,vba,Excel,Vba,我有下面的代码,它根据列标题搜索特定文本,如列O、列p等。相反,我想使用第1行中的相应列名进行搜索 我在代码注释中添加了列名 Sub PassFailValidationandupdatecomments() Dim Rng As Range, cl As Range Dim LastRow As Long, MatchRow As Variant With Sheets("DRG") LastRow = .Cells(.Rows.count, "E").End(xlUp).Row

我有下面的代码,它根据列标题搜索特定文本,如列O、列p等。相反,我想使用第1行中的相应列名进行搜索

我在代码注释中添加了列名

Sub PassFailValidationandupdatecomments()

Dim Rng As Range, cl As Range
Dim LastRow As Long, MatchRow As Variant

With Sheets("DRG")
    LastRow = .Cells(.Rows.count, "E").End(xlUp).Row '"E" - Live ASIN
    Set Rng = .Range("E2:E" & LastRow) ' "E" - Live ASIN
End With

With Sheets("Latency")
    For Each cl In .Range("B2:B" & .Cells(.Rows.count, "B").End(xlUp).Row) ` "B" - ASIN
        MatchRow = Application.Match(cl.Value, Rng, 0) 
        If Not IsError(MatchRow) Then 

            Select Case Sheets("DRG").Range("AH" & MatchRow + 1).Value ' "AH" - Final Test Result

                    .Range("O" & cl.Row).Value = "Pass" '"O" - Pass/Fail

                    Case "Pended"
                    .Range("O" & cl.Row).Value = "Fail"'"O" - Pass/Fail

                Case "In progress"
                    .Range("O" & cl.Row).Value = "In progress"'"O" - Pass/Fail                End Select

                  If Not Sheets("DRG").Range("E" & MatchRow + 1).Value = vbNullString Then .Range("P" & cl.Row).Value = .Range("P" & cl.Row).Value & IIf(Not .Range("P" & cl.Row).Value = vbNullString, ";", "") & Sheets("DRG").Range("S" & MatchRow + 1).Value ' "E" - Live ASIN ; "P" - Comments ; "S" - App Trail
        End If
    Next cl
End With

使用搜索字符串“excel vba find column based header”(excel vba find column based based header)在SO中进行搜索,您将发现许多实现您尝试的方法的示例。我尝试过这样做。但我不知道如何在上述代码中实现相同的功能。因此,如果我知道如何在上面的代码中实现一个实例,我可以在其他地方使用相同的方法演示您的工作,即使它不太有效,我们也会有一个起点来帮助您。我尝试使用以下方法:
LastRow=.Cells(.Rows.count,“Live-ASIN”,lookat:=xlother.entireclumn)。End(xlUp.Row)““E”-Live-ASIN
,但它显示语法错误。您必须使用
Find()
方法类似于
lastRow=.Cells(.Rows.count,.Rows(1).Find(what:=“Live ASIN”,lookAt:=xlother,lookIn:=xlValues)。Row