Shorten VBA Code

BenGee

Board Regular
Joined
Mar 5, 2016
Messages
196
Hi - I just wondered if there is a way of shortening this code down (loop it?)?

I want to apply this code to rows 4:43 but excluding rows 8,9,19,20,27,28,40,41

Code:
    If Range("AM4").Value = "E" Then
            Range("P4:S4").Value = "Lunch"
            Range("T4:W4").Value = ""
            Range("AH4:AK4").Value = "*"
        End If
        If Range("AM4").Value = "MS" Then
            Range("R4:U4").Value = "Lunch"
            Range("P4:Q4,V4:W4") = ""
            Range("D4,AJ4:AK4") = "*"
        End If
        If Range("AM4").Value = "L" Then
            Range("T4:W4") = "Lunch"
            Range("P4:S4") = ""
            Range("D4:E4") = "*"
        End If
        If Range("AM4").Value = "ASH" Then
            Range("D4:AK4").Value = "ASH"
            Range("C4").Value = "True"
    End If
    If Range("AM5").Value = "E" Then
            Range("P5:S5").Value = "Lunch"
            Range("T5:W5").Value = ""
            Range("AH5:AK5").Value = "*"
        End If
        If Range("AM5").Value = "MS" Then
            Range("R5:U5").Value = "Lunch"
            Range("P5:Q5,V5:W5") = ""
            Range("D5,AJ5:AK5") = "*"
        End If
        If Range("AM5").Value = "L" Then
            Range("T5:W5") = "Lunch"
            Range("P5:S5") = ""
            Range("D5:E5") = "*"
        End If
        If Range("AM5").Value = "ASH" Then
            Range("D5:AK5").Value = "ASH"
            Range("C5").Value = "True"
    End If
    If Range("AM6").Value = "E" Then
            Range("P6:S6").Value = "Lunch"
            Range("T6:W6").Value = ""
            Range("AH6:AK6").Value = "*"
        End If
        If Range("AM6").Value = "MS" Then
            Range("R6:U6").Value = "Lunch"
            Range("P6:Q6,V6:W6") = ""
            Range("D6,AJ6:AK6") = "*"
        End If
        If Range("AM6").Value = "L" Then
            Range("T6:W6") = "Lunch"
            Range("P6:S6") = ""
            Range("D6:E6") = "*"
        End If
        If Range("AM6").Value = "ASH" Then
            Range("D6:AK6").Value = "ASH"
            Range("C6").Value = "True"
    End If
    If Range("AM7").Value = "E" Then
            Range("P7:S7").Value = "Lunch"
            Range("T7:W7").Value = ""
            Range("AH7:AK7").Value = "*"
        End If
        If Range("AM7").Value = "MS" Then
            Range("R7:U7").Value = "Lunch"
            Range("P7:Q7,V7:W7") = ""
            Range("D7,AJ7:AK7") = "*"
        End If
        If Range("AM7").Value = "L" Then
            Range("T7:W7") = "Lunch"
            Range("P7:S7") = ""
            Range("D7:E7") = "*"
        End If
        If Range("AM7").Value = "ASH" Then
            Range("D7:AK7").Value = "ASH"
            Range("C7").Value = "True"
        End If
    If Range("AM10").Value = "E" Then
            Range("P10:S10").Value = "Lunch"
            Range("T10:W10").Value = ""
            Range("AH10:AK10").Value = "*"
        End If
        If Range("AM10").Value = "MS" Then
            Range("R10:U10").Value = "Lunch"
            Range("P10:Q10,V10:W10") = ""
            Range("D10,AJ10:AK10") = "*"
        End If
        If Range("AM10").Value = "L" Then
            Range("T10:W10") = "Lunch"
            Range("P10:S10") = ""
            Range("D10:E10") = "*"
        End If
        If Range("AM10").Value = "ASH" Then
            Range("D10:AK10").Value = "ASH"
            Range("C10").Value = "True"
    End If
    If Range("AM11").Value = "E" Then
            Range("P11:S11").Value = "Lunch"
            Range("T11:W11").Value = ""
            Range("AH11:AK11").Value = "*"
        End If
        If Range("AM11").Value = "MS" Then
            Range("R11:U11").Value = "Lunch"
            Range("P11:Q11,V11:W11") = ""
            Range("D11,AJ11:AK11") = "*"
        End If
        If Range("AM11").Value = "L" Then
            Range("T11:W11") = "Lunch"
            Range("P11:S11") = ""
            Range("D11:E11") = "*"
        End If
        If Range("AM11").Value = "ASH" Then
            Range("D11:AK11").Value = "ASH"
            Range("C11").Value = "True"
    End If

Otherwise I need to write the above 26 more times and changing the row numbers so just wondering if there was a way to loop the same code through a specific range set (rather than manually type it out for each row)

Thanks in advance for your help
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
This loops, you'll need update after the first set

Code:
Sub BENGEE()
Dim N
For N = 4 To 7 Step 1
If Range("AM" & N).Value = "E" Then
            Range("P" & N & ":S" & N).Value = "Lunch"
            Range("T" & N & ":W" & N).Value = ""
            Range("Ah" & N & ":Ak" & N).Value = "*"
        End If
        If Range("AM4").Value = "MS" Then
            Range("R4:U4").Value = "Lunch"
            Range("P4:Q4,V4:W4") = ""
            Range("D4,AJ4:AK4") = "*"
        End If
        If Range("AM4").Value = "L" Then
            Range("T4:W4") = "Lunch"
            Range("P4:S4") = ""
            Range("D4:E4") = "*"
        End If
        If Range("AM4").Value = "ASH" Then
            Range("D4:AK4").Value = "ASH"
            Range("C4").Value = "True"
    End If
    
    Next N
End Sub
 
Upvote 0
Perhaps.
Code:
Dim I As Long

    For I = 4 To 43

        Select Case I

            Case 8, 9, 19, 20, 27, 28, 40, 41
                ' do nothing

            Case Else
                If Range("AM" & I).Value = "E" Then
                    Range("P" & I).Resize(, 4).Value = "Lunch"
                    Range("T" & I).Resize(, 4).Value = ""
                    Range("AH" & I).Resize(, 4).Value = "*"
                End If

                If Range("AM" & I).Value = "MS" Then
                    Range("R" & I).Resize(, 4).Value = "Lunch"
                    Range("P" & I).Resize(, 2).Value = ""
                    Range("V" & I).Resize(, 2).Value = ""
                    Range("D" & I).Value = "*"
                    Range("AJ" & I).Resize(, 2).Value = "*"
                End If

                If Range("AM" & I).Value = "L" Then
                    Range("T" & I).Resize(, 4).Value = "Lunch"
                    Range("P" & I).Resize(, 4).Value = ""
                    Range("D" & I).Resize(, 2).Value = "*"
                End If

                If Range("AM" & I).Value = "ASH" Then
                    Range("D" & I).Resize(, 34).Value = "ASH"
                    Range("C" & I).Value = "True"
                End If

        End Select

    Next I
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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