如何在 VBA 中使用 for 循环进行多种条件格式设置
How to use a for loop in VBA for multiple conditional formatting
如何为满足我条件的第一次出现的单元格的cell/row着色。
条件是每周第一次出现项目颜色为“肉”的情况。
下面是我的代码
Public Sub Button1_Click()
Dim wb As Workbook
Dim ws As Worksheet
Dim count As Long
Dim i As Long
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
For i = 1 To 52
count = 0
Do While ws.Cells(9 + count, 3).Value <> vbNullString
If ws.Cells(9 + count, 3).Value = i And _
ws.Cells(9 + count, 8).Value = "Meat" Then
ws.Cells(9 + count, 8).Interior.ColorIndex = 5
End If
count = count + 1
Loop
Next i
End Sub
一旦标记第一次出现,您就无法逃脱 Do While 循环。循环只是继续标记所有出现的事件,因为它不知道在第一次出现后停止。
那么,这应该可以完成您想要做的事情...
Public Sub Button1_Click()
Dim wb As Workbook
Dim ws As Worksheet
Dim count As Long
Dim i As Long
Dim bFnd As Boolean
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
For i = 1 To 52
count = 0
bFnd = False
Do While ws.Cells(9 + count, 5).Value <> vbNullString
If ws.Cells(9 + count, 5).Value = i And _
ws.Cells(9 + count, 8).Value = "Meat" Then
If bFnd = False Then
ws.Cells(9 + count, 8).Interior.ColorIndex = 5
bFnd = True
End If
End If
count = count + 1
Loop
Next i
End Sub
另请注意,如果不同的用户设置了不同的主题,使用 .ColorIndex 可能会导致他们看到不同的颜色。通常最好使用固定颜色参考,如 ws.Cells(9 + count, 6).Interior.Color = 16711680
(这是我系统的 ColorIndex 5 的特定颜色)或 ws.Cells(9 + count, 6).Interior.Color = rgb(0, 0, 255)
使用更熟悉的纯蓝色 RGB 比例。
请尝试这个包含一些附加功能的功能,这些功能在处理各种情况时可能会有帮助。
Public Sub Button1_Click()
Dim wb As Workbook
Dim ws As Worksheet
Dim count As Long
Dim i As Long
Dim bFnd As Boolean
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
count = 0
For i = 1 To 52
'This If Loop helps to End Sub when Blank cell is reached in column no. 5
If ws.Cells(9 + count, 5).Value = "" Then
End
End If
Do While ws.Cells(9 + count, 5).Value <> ""
If ws.Cells(9 + count, 5).Value = i And _
ws.Cells(9 + count, 8).Value = "Meat" Then
ws.Cells(9 + count, 8).Interior.ColorIndex = 5
count = count + 1
GoTo Here
End If
count = count + 1
'This If loop helps in going to next i, if in existing Week there is no occurrence of "Meat"
If ws.Cells(9 + count, 5).Value = i + 1 Then
GoTo Here
End If
Loop 'Imp. Note : Please insert "Here:" after this line of code. Useful for exiting Do loop. If Do Loop is not exited then Next i will not be executed
Next i
End Sub
如何为满足我条件的第一次出现的单元格的cell/row着色。
条件是每周第一次出现项目颜色为“肉”的情况。
下面是我的代码
Public Sub Button1_Click()
Dim wb As Workbook
Dim ws As Worksheet
Dim count As Long
Dim i As Long
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
For i = 1 To 52
count = 0
Do While ws.Cells(9 + count, 3).Value <> vbNullString
If ws.Cells(9 + count, 3).Value = i And _
ws.Cells(9 + count, 8).Value = "Meat" Then
ws.Cells(9 + count, 8).Interior.ColorIndex = 5
End If
count = count + 1
Loop
Next i
End Sub
一旦标记第一次出现,您就无法逃脱 Do While 循环。循环只是继续标记所有出现的事件,因为它不知道在第一次出现后停止。
那么,这应该可以完成您想要做的事情...
Public Sub Button1_Click()
Dim wb As Workbook
Dim ws As Worksheet
Dim count As Long
Dim i As Long
Dim bFnd As Boolean
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
For i = 1 To 52
count = 0
bFnd = False
Do While ws.Cells(9 + count, 5).Value <> vbNullString
If ws.Cells(9 + count, 5).Value = i And _
ws.Cells(9 + count, 8).Value = "Meat" Then
If bFnd = False Then
ws.Cells(9 + count, 8).Interior.ColorIndex = 5
bFnd = True
End If
End If
count = count + 1
Loop
Next i
End Sub
另请注意,如果不同的用户设置了不同的主题,使用 .ColorIndex 可能会导致他们看到不同的颜色。通常最好使用固定颜色参考,如 ws.Cells(9 + count, 6).Interior.Color = 16711680
(这是我系统的 ColorIndex 5 的特定颜色)或 ws.Cells(9 + count, 6).Interior.Color = rgb(0, 0, 255)
使用更熟悉的纯蓝色 RGB 比例。
请尝试这个包含一些附加功能的功能,这些功能在处理各种情况时可能会有帮助。
Public Sub Button1_Click()
Dim wb As Workbook
Dim ws As Worksheet
Dim count As Long
Dim i As Long
Dim bFnd As Boolean
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
count = 0
For i = 1 To 52
'This If Loop helps to End Sub when Blank cell is reached in column no. 5
If ws.Cells(9 + count, 5).Value = "" Then
End
End If
Do While ws.Cells(9 + count, 5).Value <> ""
If ws.Cells(9 + count, 5).Value = i And _
ws.Cells(9 + count, 8).Value = "Meat" Then
ws.Cells(9 + count, 8).Interior.ColorIndex = 5
count = count + 1
GoTo Here
End If
count = count + 1
'This If loop helps in going to next i, if in existing Week there is no occurrence of "Meat"
If ws.Cells(9 + count, 5).Value = i + 1 Then
GoTo Here
End If
Loop 'Imp. Note : Please insert "Here:" after this line of code. Useful for exiting Do loop. If Do Loop is not exited then Next i will not be executed
Next i
End Sub