如何在vba中遍历表头中的列

时间:2017-10-11 23:02:37

标签: vba

我是VBA的新手,我编写了一个代码来突出我的excel文件中的一些内容。但我有一个问题,我想从vba中的表头遍历列,我无法做到这一点,因为我的列号将来可能会改变。请帮忙。

以下是我的代码 -

谢谢!

Sub LoopThroughRows()

    Application.EnableCancelKey = xlDisabled
    Dim k As Long, lastrow As Long, lastCol As Long, i As Integer, j As Integer, CurrentYear As String, TableHeader As String, CurrentQuarter As String, TargetYear As String, TargetQuarter As String
    lastCol = Range("AA1").End(xlToRight).Column

    With Worksheets("Sheet1")
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    Application.ScreenUpdating = False

    For j = 2 To lastrow                              'Starting the loop from the 2nd row
        For i = 27 To lastCol                         ' Starting the loop from the 27th column AA1
            With Worksheets("Sheet1")
                If .Cells(j, i).Value > 0 Then
                    TableHeader = Cells(1, i).Text
                    Exit For
                End If
            End With
        Next                                          'Loop to traverse columns ends if the condition is met

        CurrentYear = Right(Cells(1, i), 2)           ' Extracting the last 2 characters
        CurrentQuarter = Mid(Cells(1, i), 2, 1)       'Extracting the Quarter number 2nd character
        TargetYear = Right(Range("R" & j), 2)         'Extracting the last 2 characters
        TargetQuarter = Right(Range("Q" & j), 1)      'Extracting the quarter number
        Range("BX1" & j) = "Status"


        If Not IsNull(CurrentYear & TargetYear) Then
            If CurrentYear < TargetYear Then
                Range("A" & j).EntireRow.Interior.ColorIndex = 3
                Range("BX" & j) = "Early Start"
            ElseIf TargetYear = CurrentYear Then
                If CurrentQuarter < TargetQuarter Then
                    Range("A" & j).EntireRow.Interior.ColorIndex = 3
                    Range("BX" & j) = "Early Start"
                End If
            End If

        End If

        If Not IsNull(CurrentYear & TargetYear) Then
            If CurrentYear > TargetYear Then
                Range("A" & j).EntireRow.Interior.ColorIndex = 6
                Range("BX" & j) = "Late Start"
            ElseIf TargetYear = CurrentYear Then
                If CurrentQuarter > TargetQuarter Then
                    Range("A" & j).EntireRow.Interior.ColorIndex = 6
                    Range("BX" & j) = "Late Start"
                End If
            End If

        End If

        If WorksheetFunction.Sum(Range("AA" & j & ":BW" & lastCol)) = 0 Then
            Range("A" & j).EntireRow.Interior.ColorIndex = 5
            Range("BX" & j) = "Not Started"
        End If
    Next                                              ' Moving on to next row
    Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:1)

没有查看您的代码,但您可以从WorksheetObject.Range("TableName[HeaderCaption]").Column访问实际的列。

说我在Sheet1中有一个表“ Table1 ”(ListObject)Dim oWS as Worksheet; Set oWS = Thisworkbook.Worksheets("Sheet1"),然后:

  • 标题地址可以通过以下方式访问:oWS.Range("Table1[#Headers]").Address
  • 特定标题的列:oWS.Range("Table1[MyHeader]").Column

参考:ListObject Object (Excel)