Excel VBA循环显示某些工作表,但不是全部

时间:2017-02-13 19:05:52

标签: excel vba

我有一张大约12张的工作簿。我正在尝试将条件格式和数据验证应用于第2到第6页上从E到Y的交替行。

验证和格式化工作完美,但工作表循环给了我很多问题。

它继续将循环应用到Sheet 1,并且它并不总是将它应用于从2到6的每个工作表,尽管没有被击中的工作表发生了变化。

我不知所措。有些代码是使用“录制宏”功能制作的,所以我确定它效率低,体积大,但这里的代码是:

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

1 个答案:

答案 0 :(得分:1)

这是未经测试的,但是如果我没有松开任何For Each ws In ThisWorkbook.Worksheets来完全限定Select Case ws.Index(工作表对象)下的所有对象,那么应该没问题。

您可以使用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 遍历所有工作表,然后检查工作表索引是否介于2到6之间且{{1}}。

<强>代码

{{1}}