在VBA中使用列名称

时间:2017-02-09 06:45:00

标签: excel vba

我有以下代码,它根据Col标题搜索特定文本,例如Col O,Col 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

0 个答案:

没有答案