简体   繁体   中英

Excel VBA Loop through some sheets but not all

I have a workbook with about 12 sheets. I am trying to apply conditional formatting and data validation to alternating rows from E to Y on sheets 2 through 6.

The validation and formatting are working perfectly, but the sheet loop is giving me a lot of issues.

It keeps applying the loop to Sheet 1, and it doesn't always apply it to each sheet from 2 to 6, although the sheet that doesn't get hit changes.

I'm at a loss. Some of the code was made with the Record Macro function, so I'm sure it's inefficient and bulky, but here's the code:

Sub ListCF()
Dim i           As Integer
Dim j           As Integer
Dim k           As Integer
Dim cl          As Variant
Dim Row         As Variant
Dim ws          As Worksheet

For i = 2 To 6

    With ThisWorkbook.Worksheets(i)

        Worksheets(i).Activate
        Cells.Select
        Selection.FormatConditions.Delete
        Selection.Validation.Delete
        Selection.NumberFormat = "General"

        For j = 5 To 23 Step 2

            Range(Cells(2, j), Cells(50, j)).Select

            With Selection.Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:="=Reference!$A$2:$A$50"
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .ShowInput = True
                .ShowError = True
            End With

        Next j

        For k = 6 To 24 Step 2

            cl = Mid(Cells(2, k).Address, 2, 1)

            Range("$" & cl & 2, "$" & cl & 50).Select

            Selection.NumberFormat = "m/d/yyyy"

            'With Range(Cells(2, k), Cells(50, k))

                Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
                    "=$" & cl & 2 & ">(TODAY()-60)"

                Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority

                With Selection.FormatConditions(1).Font
                    .Bold = True
                    .Italic = False
                    .Color = -16776961
                    .TintAndShade = 0
                End With

                Selection.FormatConditions(1).StopIfTrue = False

                Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
                    "=$" & cl & 2 & "<(TODAY()-60)"

                Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority

                With Selection.FormatConditions(1).Font
                    .Bold = False
                    .Italic = False
                    .ThemeColor = xlThemeColorLight1
                    .TintAndShade = 0
                End With

                Selection.FormatConditions(1).StopIfTrue = False

            'End With

            Range(Cells(2, k), Cells(50, k)).Select
            Selection.NumberFormat = "m/d/yyyy"

        Next k

    End With

Next i
End Sub

This is untested, but if I didn't loose any . to fully qualify all of the objects under ws (worksheet object), it should be OK.

You can loop through all worksheets with For Each ws In ThisWorkbook.Worksheets , and then check is the worksheet index is between 2 to 6 with Select Case ws.Index .

Code

Sub ListCF()

Dim i           As Integer
Dim j           As Integer
Dim k           As Integer
Dim cl          As Variant
Dim Row         As Variant
Dim ws          As Worksheet

For Each ws In ThisWorkbook.Worksheets
    Select Case ws.Index
    Case 2 To 6 ' if Worksheet.Index is 2 to 6
        With ws         
            With .Cells
                .FormatConditions.Delete
                .Validation.Delete
                .NumberFormat = "General"
            End With

            For j = 5 To 23 Step 2
                With .Range(.Cells(2, j), .Cells(50, j)).Validation
                    .Delete
                    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=Reference!$A$2:$A$50"
                    .IgnoreBlank = True
                    .InCellDropdown = True
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = True
                    .ShowError = True
                End With
            Next j

            For k = 6 To 24 Step 2
                cl = Mid(.Cells(2, k).Address, 2, 1)

                With .Range("$" & cl & 2, "$" & cl & 50)
                    .NumberFormat = "m/d/yyyy"
                    .FormatConditions.Add Type:=xlExpression, Formula1:="=$" & cl & 2 & ">(TODAY()-60)"

                    .FormatConditions(.FormatConditions.Count).SetFirstPriority
                    With .FormatConditions(1).Font
                        .Bold = True
                        .Italic = False
                        .Color = -16776961
                        .TintAndShade = 0
                    End With

                    .FormatConditions(1).StopIfTrue = False
                    .FormatConditions.Add Type:=xlExpression, Formula1:="=$" & cl & 2 & "<(TODAY()-60)"
                    .FormatConditions(.FormatConditions.Count).SetFirstPriority
                    With .FormatConditions(1).Font
                        .Bold = False
                        .Italic = False
                        .ThemeColor = xlThemeColorLight1
                        .TintAndShade = 0
                    End With
                    .FormatConditions(1).StopIfTrue = False
                End With

                .Range(Cells(2, k), Cells(50, k)).NumberFormat = "m/d/yyyy"
            Next k
        End With
    End Select
Next ws

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM