[英]Looping through multiple sheets in excel
我正在嘗試編寫一個程序,該程序循環遍歷每個表中的每一行(每張表一個表),以便對其進行顏色編碼,類似於條件格式。 這不會繼續到下一張工作表,因此只會對我打開的工作表進行顏色編碼。 我希望它自動前進到下一個。 任何輸入表示贊賞。
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
結束子
為您提供斯科特·克蘭納(Scott Craner)評論的完整答案
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")
這樣,它將檢查字符串是否在工作表名稱內
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.