在Col名称而不是Col标题上运行代码

时间:2017-02-11 04:24:14

标签: excel vba

我有以下使用Col标头运行的代码,例如Col A,Col B等。而我想用Col A,Col B使用的col名称替换它。

我在其他帖子中看到,我们可以使用lookAt:=xlWhole.EntireColumn来实现这一目标。但是我不太确定如何在下面的代码中使用它。

对此的任何帮助都会非常棒。

Sub Test()

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 '<-- find last row with data in column E
    Set Rng = .Range("E2:E" & LastRow)
End With

With Sheets("Latency")
    For Each cl In .Range("B2:B" & .Cells(.Rows.count, "B").End(xlUp).Row) ' loop through all cells in Column B
        MatchRow = Application.Match(cl.Value, Rng, 0) 
        If Not IsError(MatchRow) Then 

        Select Case Sheets("DRG").Range("AH" & MatchRow + 1).Value 
            Case "Approved"
                .Range("O" & cl.Row).Value = "Pass"

            Case "Pended"
                .Range("O" & cl.Row).Value = "Fail"

            Case "In progress"
                .Range("O" & cl.Row).Value = "In progress"
        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
    End If
Next cl
End With

End Sub

0 个答案:

没有答案