Simplify my code

kamalm

New Member
Joined
Jul 30, 2018
Messages
33
Hi, I use this code to insert values at Column J based on Column B. This code works perfectly fine but I think this is too long. Perhaps there are others ways to simplify my code.


Code:
 Dim LastRow As Long
    Dim z As Long
    LastRow = Range("B" & Rows.Count).End(xlUp).Row
    For z = 2 To LastRow
        If Range("B" & z).Value = "A-01" Then
            Range("J" & z).Value = "Min. Material Level"
        ElseIf Range("B" & z).Value = "A-02" Then
            Range("J" & z).Value = "Min. Material Level"
        ElseIf Range("B" & z).Value = "A-03" Then
            Range("J" & z).Value = "Min. Material Level"
        ElseIf Range("B" & z).Value = "A-04" Then
            Range("J" & z).Value = "Min. Material Level"
        ElseIf Range("B" & z).Value = "A-05" Then
            Range("J" & z).Value = "Min. Material Level"
        ElseIf Range("B" & z).Value = "A-06" Then
            Range("J" & z).Value = "Min. Material Level"
        ElseIf Range("B" & z).Value = "A-58" Then
            Range("J" & z).Value = "Min. Material Level"
        ElseIf Range("B" & z).Value = "A-59" Then
            Range("J" & z).Value = "Min. Material Level"
        
        ElseIf Range("B" & z).Value = "A-07" Then
            Range("J" & z).Value = "Material Flow"
        ElseIf Range("B" & z).Value = "A-08" Then
            Range("J" & z).Value = "Material Flow"
        ElseIf Range("B" & z).Value = "A-09" Then
            Range("J" & z).Value = "Material Flow"
        ElseIf Range("B" & z).Value = "A-10" Then
            Range("J" & z).Value = "Material Flow"
        ElseIf Range("B" & z).Value = "A-11" Then
            Range("J" & z).Value = "Material Flow"
        ElseIf Range("B" & z).Value = "A-12" Then
            Range("J" & z).Value = "Material Flow"
        ElseIf Range("B" & z).Value = "A-60" Then
            Range("J" & z).Value = "Material Flow"
        ElseIf Range("B" & z).Value = "A-61" Then
            Range("J" & z).Value = "Material Flow"
            
        ElseIf Range("B" & z).Value = "A-13" Then
            Range("J" & z).Value = "Doser Deviation"
         ElseIf Range("B" & z).Value = "A-14" Then
            Range("J" & z).Value = "Doser Deviation"
         ElseIf Range("B" & z).Value = "A-15" Then
            Range("J" & z).Value = "Doser Deviation"
         ElseIf Range("B" & z).Value = "A-16" Then
            Range("J" & z).Value = "Doser Deviation"
          ElseIf Range("B" & z).Value = "A-17" Then
            Range("J" & z).Value = "Doser Deviation"
         ElseIf Range("B" & z).Value = "A-18" Then
            Range("J" & z).Value = "Doser Deviation"
         ElseIf Range("B" & z).Value = "A-62" Then
            Range("J" & z).Value = "Doser Deviation"
         ElseIf Range("B" & z).Value = "A-63" Then
            Range("J" & z).Value = "Doser Deviation"
            
         ElseIf Range("B" & z).Value = "A-24" Then
            Range("J" & z).Value = "SAF.SW.Tripped Or No Comp. Air"
         
         End If
    Next z
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi,
untested but one way maybe

Code:
Option Base 1
Sub simplify_my_code()
    Dim LastRow As Long, z As Long
    Dim i As Integer
    Dim arr As Variant, m As Variant
    
    arr = Array(Array("A-01", "A-02", "A-03", "A-04", "A-05", "A-06", "A-58", "A-59"), _
                Array("A-07", "A-08", "A-09", "A-10", "A-11", "A-12", "A-60", "A-61"), _
                Array("A-13", "A-14", "A-15", "A-16", "A-17", "A-18", "A-62", "A-63"), _
                Array("A-24"))
                
    LastRow = Range("B" & Rows.Count).End(xlUp).Row
    
    For z = 2 To LastRow
        For i = 1 To 4
            m = Application.Match(Range("B" & z).Value, arr(i), 0)
            If Not IsError(m) Then Range("J" & z).Value = _
            Choose(i, "Min. Material Level", "Material Flow", _
                      "Doser Deviation", "SAF.SW.Tripped Or No Comp. Air"): Exit For
        Next i
    Next z
End Sub

Note Option Base 1 statement - This MUST sit at VERY TOP of your code page OUTSIDE any procedure.

Dave
 
Upvote 0
Code:
Option Base 1
Sub simplify_my_code()
    Dim LastRow As Long, z As Long
    Dim i As Integer
    Dim arr As Variant, m As Variant
    
    arr = Array(Array("A-01", "A-02", "A-03", "A-04", "A-05", "A-06", "A-58", "A-59"), _
                Array("A-07", "A-08", "A-09", "A-10", "A-11", "A-12", "A-60", "A-61"), _
                Array("A-13", "A-14", "A-15", "A-16", "A-17", "A-18", "A-62", "A-63"), _
                Array("A-24"))
                
    LastRow = Range("B" & Rows.Count).End(xlUp).Row
    
    For z = 2 To LastRow
        For i = 1 To 4
            m = Application.Match(Range("B" & z).Value, arr(i), 0)
            If Not IsError(m) Then Range("J" & z).Value = _
            Choose(i, "Min. Material Level", "Material Flow", _
                      "Doser Deviation", "SAF.SW.Tripped Or No Comp. Air"): Exit For
        Next i
    Next z
End Sub

Note Option Base 1 statement - This MUST sit at VERY TOP of your code page OUTSIDE any procedure.

Dave
hey thanks mate. your code perfectly working! God bless you
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,210
Members
453,023
Latest member
alabaz

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