我根据在这里找到的另一个问题(链接:Excel VBA Select Case Loop Sub)对 Select Case 循环进行了建模。
为了使其适用,我必须做的更改非常少,而且我看不出哪里出错了。这是我的代码:
Private Function getColor(ByVal MatVal As Range) As Integer
Select Case MatVal
Case 0 To 1
getColor = 9: Exit Function
Case 1.01 To 3
getColor = 46: Exit Function
Case 3.01 To 5
getColor = 27: Exit Function
Case 5.01 To 10
getColor = 4: Exit Function
Case 10.01 To 20
getColor = 5: Exit Function
Case 20.01 To 30
getColor = 11: Exit Function
Case 30 To 100
getColor = 29: Exit Function
End Select
End Function
通过以下方式调用:
Set LipR = Workbooks("LMacro.xlsm")
Set SecX = Application.Workbooks.Open(Path & "SecX.csv")
Set Xws = SecX.Sheets("SecX")
Set Lws = LipR.Sheets("Funds")
With Lws
For i = 2 To 10 'LwsRows
If Lws.Range("A" & i).Value <> "" Then
LipR.Sheets.Add(After:=LipR.Sheets(LipR.Sheets.Count)).Name = Lws.Range("A" & i).Value
NewFund = Lws.Range("A" & i).Value
Set Fsheet = LipR.Sheets(NewFund)
End If
With Fsheet
FsheetRows = .Range("A" & .Rows.Count).End(xlUp).Row
End With
....
Set MatPhase = Fsheet.Range("O4:O" & FsheetRows)
For Each MatVal In MatPhase.Cells
MatVal.Interior.ColorIndex = getColor(MatVal)
Next MatVal
Fsheet.Cells.EntireColumn.AutoFit
Application.Goto _
Reference:=Fsheet.Range("A1"), Scroll:=True
Next i
End With
我在这里想念什么?我真的试图避免为此使用 if/elseif。
谢谢