I modeled a Select Case loop based on another question I found on here(Link: Excel VBA Select Case Loop Sub).
The changes I had to do to make it applicable were pretty minimal, and I don't see where I went wrong. Here's my code:
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
Which is called via:
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
What am i missing here? I was really trying to avoid an if/elseif for this.
Thank you