如何在Excel-VBA中将此ElseIf编写为Case语句?

时间:2015-07-04 17:40:03

标签: excel-vba vba excel

根据单元格中的文本格式化绘图区域中的每个单元格,但想知道如何对Case语句进行相同操作。

function maxMinAvg(arr) {
    var newarr= [];
    var max = arr[0];
    var min = arr[0];
    sum = sum + arr[0];
    var avg = sum/arr.length;
    for (var i = 1; i < arr.length; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
        if (arr[i] < min) {
            min = arr[i];
        }
        sum = sum + arr[i];
    }
    newarr.push([max[i],min[i],avg[i]]);
}

return newarr;

我尝试了以下操作,但得到的是#ma; type mistmacth&#39;

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

1 个答案:

答案 0 :(得分:3)

只需进行一些调整。

    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