【问题标题】:Looping through multiple sheets in excel在excel中循环多个工作表
【发布时间】:2017-12-27 23:52:50
【问题描述】:

我正在尝试编写一个程序,该程序循环遍历每个表中的每一行(每张表一个表),以便对其进行颜色编码,类似于条件格式。这不会移动到下一张纸,所以它只会对我打开的纸进行颜色编码。我希望它自动移动到下一个。任何输入表示赞赏。

Dim ccShipDate As Variant
Dim ccRow As Integer
Dim wsht As Worksheet

ccRow = 2
ccShipDate = Cells(ccRow, 6)

For Each wsht In Worksheets
    If wsht.Name = "ManualReview" Or wsht.Name = "Filter" Or wsht.Name = "MRF" Or wsht.Name = "ModStd" Then
         With Worksheets(wsht.Name)

                ' loops through "Actual Ship Date" column until empty
                ' past or today = red
                ' one day away = yellow
                ' more than one day = green
                Do Until IsEmpty(ccShipDate)
                    If DateDiff("d", Date, ccShipDate) <= 0 Then
                        Cells(ccRow, 3).Interior.ColorIndex = 3

                     ElseIf DateDiff("d", Date, ccShipDate) = 1 Then
                        Cells(ccRow, 3).Interior.ColorIndex = 6

                        ElseIf DateDiff("d", Date, ccShipDate) > 1 Then
                            Cells(ccRow, 3).Interior.ColorIndex = 4


                    End If
                    ccRow = ccRow + 1
                    ccShipDate = Cells(ccRow, 6).Value

                Loop
         End With
    End If
Next wsht

结束子

【问题讨论】:

  • 使用With 时,您需要将所有链接的子项附加到.。所以.Cells(ccRow, 3).Interior...

标签: vba excel


【解决方案1】:

给你一个完整的回答斯科特克兰纳的评论

Dim ccShipDate As Variant
Dim ccRow As Integer
Dim wsht As Worksheet

ccRow = 2
ccShipDate = Cells(ccRow, 6)

For Each wsht In Worksheets
    If wsht.Name = "ManualReview" Or wsht.Name = "Filter" Or wsht.Name = "MRF" Or wsht.Name = "ModStd" Then
         With Worksheets(wsht.Name)
            Do Until IsEmpty(ccShipDate)
                If DateDiff("d", Date, ccShipDate) <= 0 Then
                    .Cells(ccRow, 3).Interior.ColorIndex = 3

                 ElseIf DateDiff("d", Date, ccShipDate) = 1 Then
                    .Cells(ccRow, 3).Interior.ColorIndex = 6

                 ElseIf DateDiff("d", Date, ccShipDate) > 1 Then
                     .Cells(ccRow, 3).Interior.ColorIndex = 4
                 End If
                     ccRow = ccRow + 1
                     ccShipDate = .Cells(ccRow, 6).Value
            Loop
         End With
    End If
Next wsht

End Sub

我可能还建议将 If Then 语句更改为...

If InStr(1, wsht.Name, "Manual Review") Or InStr(1, wsht.Name, "Filter") Or InStr(1, wsht.Name, "MRF") Or InStr(1, wsht.Name, "ModStd")

这样它会检查字符串是否在工作表名称中

【讨论】:

  • 不明白您对InStr 的建议。有什么意义?
  • 查看与InStr() 相关的文档。本质上,它的作用是检查字符串,如果找到字符串,将返回TrueFalse。因此,它会检查 wsht.Name 是否包含文本 "Manual Review" 等。
  • 我不是在问InStr 是如何工作的。 OP 事先知道工作表的名称,因此他可以直接使用它们。那么,为什么要InStr
  • @JohnyL 以防 OP 想在工作表名称中搜索;例如,如果他不知道全名
猜你喜欢
  • 1970-01-01
  • 2016-11-25
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 2014-03-22
  • 1970-01-01
  • 2020-04-05
  • 1970-01-01
相关资源
最近更新 更多