![](/img/trans.png)
[英]Trying to add elseif statement in excel-vba will not paste or copy
[英]How can I write this ElseIf as a Case statement in Excel-VBA?
根据单元格中的文本格式化绘图区域中的每个单元格,但是想知道如何使用Case语句执行相同的操作。
For Each Cell In Range("B11:AB200")
If Cell.Value = ""
Cell.Interior.Color = RGB(230, 230, 230) Then
ElseIf Cell.Value = "tp" Then
Cell.Interior.Color = RGB(0, 51, 150)
Cell.Font.Color = RGB(170, 170, 170)
Cell.Font.FontStyle = "Normal"
ElseIf Cell.Value = "ot" Then
Cell.Interior.Color = RGB(200, 0, 0)
Cell.Font.Color = RGB(170, 170, 170)
Cell.Font.FontStyle = "Normal"
ElseIf Cell.Value = "lu" Then
Cell.Interior.Color = RGB(180, 180, 50)
Cell.Font.Color = RGB(120, 120, 120)
Cell.Font.FontStyle = "Normal"
Else
Cell.Interior.Color = RGB(255, 255, 0)
Cell.Font.Color = RGB(120, 120, 120)
Cell.Font.FontStyle = "Normal"
End If
Next Cell
我尝试了以下但是得到了'type mistmacth'
For Each Cell In Range("B11:AB200")
Select Case Range("B11:AB200")
Case Cell.Value = ""
Cell.Interior.Color = RGB(230, 230, 230)
Case Cell.Value = "tp"
Cell.Interior.Color = RGB(0, 51, 150)
Cell.Font.Color = RGB(170, 170, 170)
Cell.Font.FontStyle = "Normal"
Case Cell.Value = "ot"
Cell.Interior.Color = RGB(200, 0, 0)
Cell.Font.Color = RGB(170, 170, 170)
Cell.Font.FontStyle = "Normal"
Case Cell.Value = "lu"
Cell.Interior.Color = RGB(180, 180, 50)
Cell.Font.Color = RGB(120, 120, 120)
Cell.Font.FontStyle = "Normal"
Case Else
Cell.Interior.Color = RGB(255, 255, 0)
Cell.Font.Color = RGB(120, 120, 120)
Cell.Font.FontStyle = "Normal"
End Select
Next Cell
只是一些调整。
For Each Cell In Range("B11:AB200")
Select Case Cell.value
Case ""
Cell.Interior.Color = RGB(230, 230, 230)
Case "tp"
Cell.Interior.Color = RGB(0, 51, 150)
Cell.Font.Color = RGB(170, 170, 170)
Cell.Font.FontStyle = "Normal"
Case "ot"
Cell.Interior.Color = RGB(200, 0, 0)
Cell.Font.Color = RGB(170, 170, 170)
Cell.Font.FontStyle = "Normal"
Case "lu"
Cell.Interior.Color = RGB(180, 180, 50)
Cell.Font.Color = RGB(120, 120, 120)
Cell.Font.FontStyle = "Normal"
Case Else
Cell.Interior.Color = RGB(255, 255, 0)
Cell.Font.Color = RGB(120, 120, 120)
Cell.Font.FontStyle = "Normal"
End Select
Next Cell
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.