簡體   English   中英

VBA 如何循環遍歷數組中的每個表

[英]VBA how do I loop through each table in an array

我想清除並重置工作簿中某些 ListObjects 的條件格式。 如何創建一個數組,以便我可以使用 for 循環來應用格式。 這最初是為了適用於整個工作表而編寫的,但現在我想將例程限制為一張表。

這是我的代碼:

Sub Reset()


'Dim ws As Worksheet
Dim lo As Variant
Dim ArrTab As Variant
Dim ws As Variant
Dim Item As Variant
Dim ArrGreen As Variant

ArrTab = Array("Dept1710", "Dept1711", "Dept1713", "Dept1715", "Dept1716", "Dept1717")
ArrGreen = Worksheets("Drop down").Range("Q14:Q18").Value


For Each lo In ArrTab


lo.Select
Selection.FormatConditions.Delete

    For Each Item In ArrGreen
        Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:=Item
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 5287936
        .TintAndShade = 0
    End With
    Next Item

Next lo

End Sub

ArrTab 具有所有表名。

如果桌子分別。 listobject all on the same worksheet 以下修復可以做到

Sub Reset()

    'Dim ws As Worksheet
    Dim lo As ListObject
    Dim ArrTab As Variant
    Dim ws As Variant
    Dim Item As Variant
    Dim ArrGreen As Variant

    ArrTab = Array("Dept1710", "Dept1711", "Dept1713", "Dept1715", "Dept1716", "Dept1717")
    ArrGreen = Worksheets("Drop down").Range("Q14:Q18").Value

    Dim sngElement As Variant                         ' Variable to loop throgh the array
    For Each sngElement In ArrTab

        Set lo = ActiveSheet.ListObjects(sngElement)  ' Assign the listobject
        ' lo.Range.Select                             ' uncomment if you need the complete listobject
        lo.DataBodyRange.Select                       ' select the data (without headers)
        Selection.FormatConditions.Delete

        For Each Item In ArrGreen
            Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:=Item
            Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
            With Selection.FormatConditions(1).Interior
                .PatternColorIndex = xlAutomatic
                .Color = 5287936
                .TintAndShade = 0
            End With
        Next Item

    Next sngElement

End Sub

但通常你不需要Select

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM