How to make a Case Statement Loop through Column

Tash_081

New Member
Joined
Jan 18, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi All, newbie to VBA here so may be dumb question but I am stuck. I have this Case Statement that works as intended however I cant figure out how to make it loop through from K3 through the rest of the column until last row. Any help would be much appreciated.

Sub ShiftColour()

Select Case Range("K3").Value

Case "A", "E", "N", "L", "AP", "EP", "NP", "LP", "AI", "EI", "NI", "LI"
Range("J3").Value = "ES BLACK"
Case "B", "BP", "BI"
Range("J3").Value = "BROWNB"
Case "C", "CP", "CI"
Range("J3").Value = "DARKGREEN"
Case "D", "DP", "DI"
Range("J3").Value = "PURPLED"

End Select

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
In general, it would be something like this.
VBA Code:
Sub ShiftColour()
    Dim rng As Range
    Dim R As Range
    
    With ActiveSheet
        Set rng = .Range("K3", .Range("K" & .Rows.Count).End(xlUp))
    End With
    
    For Each R In rng
        Select Case R.Value
            Case "A", "E", "N", "L", "AP", "EP", "NP", "LP", "AI", "EI", "NI", "LI"
                R.Offset(0, -1).Value = "ES BLACK"
            Case "B", "BP", "BI"
                R.Offset(0, -1).Value = "BROWNB"
            Case "C", "CP", "CI"
                R.Offset(0, -1).Value = "DARKGREEN"
            Case "D", "DP", "DI"
                R.Offset(0, -1).Value = "PURPLED"
        End Select
    Next R
End Sub

(Tip: For future posts , please try to use code tags like I did above when posting code. It makes your code easier to read and copy.
)
 
Upvote 0
Solution
Try it
VBA Code:
Sub ShiftColour()

Dim lastRow As Long
lastRow = Cells(Rows.Count, "K").End(xlUp).Row

For i = 3 To lastRow
    Select Case Range("K" & i).Value
        Case "A", "E", "N", "L", "AP", "EP", "NP", "LP", "AI", "EI", "NI", "LI"
            Range("J" & i).Value = "ES BLACK"
        Case "B", "BP", "BI"
            Range("J" & i).Value = "BROWNB"
        Case "C", "CP", "CI"
            Range("J" & i).Value = "DARKGREEN"
        Case "D", "DP", "DI"
            Range("J" & i).Value = "PURPLED"
    End Select
Next i

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top