Vba 如何在多个工作表上运行3个条件格式循环
我试图有条件地格式化4个不同范围的工作表,而不使用Select 我正试图清理我疯狂的初学者代码并加快进程,但循环不起作用。工作表2和3范围内的所有空单元格均应填写“T”。第4页和第5页范围内的空单元格应为“p”。 表2-4中包含数据的所有单元格的格式相同:粗体字体、中心对齐、边框、有条件替换文本以及字体和字体颜色取决于单元格文本Vba 如何在多个工作表上运行3个条件格式循环,vba,loops,formatting,worksheet,Vba,Loops,Formatting,Worksheet,我试图有条件地格式化4个不同范围的工作表,而不使用Select 我正试图清理我疯狂的初学者代码并加快进程,但循环不起作用。工作表2和3范围内的所有空单元格均应填写“T”。第4页和第5页范围内的空单元格应为“p”。 表2-4中包含数据的所有单元格的格式相同:粗体字体、中心对齐、边框、有条件替换文本以及字体和字体颜色取决于单元格文本 Sub comfor() Dim ws As Worksheet, cell As Range For Each ws In ActiveWorkbook.Shee
Sub comfor()
Dim ws As Worksheet, cell As Range
For Each ws In ActiveWorkbook.Sheets
For i = 2 To 3
With Sheets(i)
For Each cell In ws.Range(ws.Range("A6"),_ ws.Range("A6").SpecialCells(xlLastCell)).Cells
If Text = "" Then
Value = "T"
End If
Next
End With
Next
For i = 4 To 5
With Sheets(i)
For Each cell In ws.Range(ws.Range("A6"),_ ws.Range("A6").SpecialCells(xlLastCell)).Cells
If Text = "Not Recorded" Then
Value = "p"
End If
Next
End With
Next
For i = 2 To 5
With Sheets(i)
For Each cell In ws.Range(ws.Range("A6"),_ ws.Range("A6").SpecialCells(xlLastCell)).Cells
With cell
.HorizontalAlignment = xlCenter
.Font.Bold = True
End With
With cell
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeTop).Weight = xlMedium
.Borders(xlEdgeBottom).Weight = xlMedium
.Borders(xlEdgeRight).Weight = xlMedium
End With
With cell
If .Text = "Incomplete" Then
.Font.Color = vbRed
.Value = "T"
.Font.Name = "Wingdings 2"
ElseIf .Text = "Not Applicable" Then
.Name = "Webdings"
.Value = "x"
.Font.Color = RGB(255, 192, 0)
ElseIf .Text = "Complete" Then
.Font.Color = 5287936
.Value = "R"
.Font.Name = "Wingdings 2"
ElseIf .Text = "Not Recorded" Then
.Font.Color = RGB(129, 222, 225)
.Value = "p"
.Font.Name = "Wingdings"
End If
End With
Next
End With
Next
Next
End Sub
用这个替换你的循环-循环与
with
语句的作用不同-你仍然必须明确地引用cell.Text/cell.Value
-除非你想将with
语句嵌入你的循环中-你绝对可以-但是即使这样,这需要是.Text
和.Value
For i = 2 To 3
With Sheets(i)
For Each cell In ws.Range(ws.Range("A6"), ws.Range("A6").SpecialCells(xlLastCell)).Cells
If cell.Text = "" Then
cell.Value = "T"
End If
Next
End With
Next
For i = 4 To 5
With Sheets(i)
For Each cell In ws.Range(ws.Range("A6"), ws.Range("A6").SpecialCells(xlLastCell)).Cells
If cell.Text = "Not Recorded" Then
cell.Value = "p"
End If
Next
End With
Next
我发现,如果我使用Select Case和ws-Name而不是I,并在每个ws.之前添加“For Each ws..,它工作和运行得非常快。可能不是最优雅的,但很有效
Sub comfor()
Dim daily As Worksheet, mon As Worksheet, per As Worksheet, surf As Worksheet
Dim ws As Worksheet, cell As Range
Set daily = Sheets("Daily")
Set per = Sheets("Personnel")
Set surf = Sheets("Testing")
Set mon = Sheets("Monthly")
For Each ws In ActiveWorkbook.Sheets
Select Case ws.Name
Case "Daily", "Monthly"
For Each cell In ws.Range(("A6"),_
ws.Range("A6").SpecialCells(xlLastCell)).Cells
If cell.Text = "" Then
cell.Value = "T"
cell.Font.Color = vbRed
cell.Value = "T"
cell.Font.Name = "Wingdings 2"
End If
Next
End Select
Next
For Each ws In ActiveWorkbook.Sheets
Select Case ws.Name
Case "Personnel", "Testing"
For Each cell In ws.Range(("A6"), ws.Range("A6").SpecialCells(xlLastCell)).Cells
If cell.Text = "" Then
cell.Value = "p"
cell.Font.Color = RGB(255, 192, 0)
cell.Value = "p"
cell.Font.Name = "Wingdings 3"
End If
Next
End Select
Next
For Each ws In ActiveWorkbook.Sheets
Select Case ws.Name
Case "Daily", "Monthly", "Personnel", "Testing"
For Each cell In ws.Range(ws.Range("A6"),_
ws.Range("A6").SpecialCells(xlLastCell)).Cells
With cell
.HorizontalAlignment = xlCenter
End With
With cell
.Borders(xlInsideVertical).Weight = xlThin
.Borders(xlInsideHorizontal).Weight = xlThin
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeTop).Weight = xlMedium
.Borders(xlEdgeBottom).Weight = xlMedium
.Borders(xlEdgeRight).Weight = xlMedium
End With
If cell.Text = "Incomplete" Then
cell.Font.Color = vbRed
cell.Value = "T"
cell.Font.Name = "Wingdings 2"
ElseIf cell.Text = "Not Applicable" Then
cell.Name = "Webdings"
cell.Value = "x"
cell.Font.Color = RGB(255, 192, 0)
ElseIf cell.Text = "Complete" Then
cell.Font.Color = 5287936
cell.Value = "R"
cell.Font.Name = "Wingdings 2"
End If
Next
End Select
Next
End Sub
抱歉,我发布的代码不正确。在更正之前我点击了提交。什么是
Text
和Value
?就您的代码而言,这些只是未声明的变量。如果cell.Value=“”,那么应该是和cell.Value=“T”
,等等。谢谢。我将“Text”和“Value”改为“cell.Value”“,但当我运行代码时,工作表上的空单元格没有填满,这需要花费很长时间。我用上面的内容替换了我的内容,但仍然没有发生任何事情。ws.Range(“A6”).SpecialCells(xlLastCell)).cells
很可能是罪魁祸首-您希望返回什么范围?每个ws上的范围都不同。列数/ws不会更改,但行数可能会根据当月的天数进行更改。所有ws都有一个标题,所以我将All设置为从A6开始格式化。我最初设置为选择每个页面、范围,然后选择格式,但我希望有一种更干净、更快的方法。另外,保证没有空单元格的唯一一列是日期列“A”。我使用宏记录器和Ctl+End获取ws.Range(ws.Range(“A6”)、ws.Range(“A6”).SpecialCells(xlLastCell)).cells。我注意到,当我将鼠标悬停在“cell”上时,弹出的状态是“cell=”第1页A6中的值!我不想格式化第1页,下一页不会转到另一页。