我有一个 excel (2010) 表,我从数据库中加载数据,然后根据每行中的特定单元格格式化每一行。格式化代码需要很长时间。大约 150 行和 15 列大约需要 4 分钟。这是代码片段。基本上它是一个循环,检查 row_type 的值并相应地为每一行设置字体颜色、背景颜色等。有没有比使用循环更好的方法?如果循环是答案,我可以做的任何其他改进。
J = 1
While J <= iNumRows
row_type = Worksheets("WaterFall").Range("WaterFallHeaders").Offset(J, -1).Cells(1, 1)
If row_type = "main_row" Then
With Worksheets("WaterFall").Range("WaterFallHeaders").Offset(J, 0).EntireRow.Font
.Bold = False
.ColorIndex = RGB(0, 0, 0)
End With
'yellow Description columns
With Worksheets("WaterFall").Range("WaterFallHeaders").Offset(J, 0).Resize(1, 5).Interior
.Color = RGB(204, 255, 204)
End With
'grey amount columns
With Worksheets("WaterFall").Range("WaterFallHeaders").Offset(J, 5).Resize(1, dtCount + 2).Interior
.Color = RGB(217, 217, 217)
End With
'blue action columns
With Worksheets("WaterFall").Range("WaterFallHeaders").Offset(J, 0).Cells(1, 1).Font
.Bold = True
End With
With Worksheets("WaterFall").Range("WaterFallHeaders").Offset(J, 0).Cells(1, 1).Interior
.Color = RGB(184, 204, 225)
End With
Else
With Worksheets("WaterFall").Range("WaterFallHeaders").Offset(J, 0).Resize(1, colCount - 1).Font
.Bold = True
.Color = RGB(51, 51, 255)
End With
With Worksheets("WaterFall").Range("WaterFallHeaders").Offset(J, 0).Resize(1, colCount - 1).Interior
.Color = RGB(255, 255, 204)
End With
End If
J = J + 1
Wend
谢谢...