Cleaner VBA Code

rzacek87

New Member
Joined
Jun 4, 2024
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I was wondering if there is a cleaner way to code this loop. Thanks in advance.

VBA Code:
Sub A()
    Dim myLastRow As Long
    Dim i As Long
    Dim InvD As Date
        
    InvD = InputBox("Invoice Date mm/dd/yy")
        
    myLastRow = Cells(Rows.Count, "B").End(xlUp).Row
    
    For i = 2 To myLastRow
        If Range("I" & i).Value Like "Sub-Producer" & "*" Then
            Range("I" & i).Value = "Sub-Producer Payment - " & Format(DateAdd("M", -1, Now), "MMMM YYYY")
            Range("V" & i).Value = 23140
            Range("E" & i).Value = InvD
            Range("F" & i).Value = "=R[0]C[-1]+45"
        End If
        If Range("I" & i).Value Like "Royalty" & "*" And InStr(1, Range("D" & i).Value, 7) = 7 Then
            Range("I" & i).Value = "Royalty Guarantee - " & Format(DateAdd("M", 0, Now), "MMMM YYYY")
            Range("V" & i).Value = 23150
            Range("E" & i).Value = InvD
            Range("F" & i).Value = "=R[0]C[-1]+45"
        End If
        If Range("I" & i).Value Like "Royalty" & "*" And InStr(1, Range("D" & i).Value, 7) <> 7 Then
            Range("I" & i).Value = "Royalty Payment - " & Format(DateAdd("M", 0, Now), "MMMM YYYY")
            Range("V" & i).Value = 23150
            Range("E" & i).Value = InvD
            Range("F" & i).Value = "=R[0]C[-1]+45)"
        End If
    Next
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Can column I ever have a value that does not start with "Sub-Producer" or "Royalty"?

If "Yes", I think your code looks pretty good. If "No", then you could probably simplify to:
VBA Code:
Sub A()
    Dim myLastRow As Long
    Dim i As Long
    Dim InvD As Date
        
    InvD = InputBox("Invoice Date mm/dd/yy")
        
    myLastRow = Cells(Rows.Count, "B").End(xlUp).Row
    
    For i = 2 To myLastRow
        Range("E" & i).Value = InvD
        Range("F" & i).Value = "=R[0]C[-1]+45"
        If Range("I" & i).Value Like "Sub-Producer" & "*" Then
            Range("I" & i).Value = "Sub-Producer Payment - " & Format(DateAdd("M", -1, Now), "MMMM YYYY")
            Range("V" & i).Value = 23140
        Else
            Range("V" & i).Value = 23150
            If Range("I" & i).Value Like "Royalty" & "*" And InStr(1, Range("D" & i).Value, 7) = 7 Then
                Range("I" & i).Value = "Royalty Guarantee - " & Format(DateAdd("M", 0, Now), "MMMM YYYY")
            Else
                Range("I" & i).Value = "Royalty Payment - " & Format(DateAdd("M", 0, Now), "MMMM YYYY")
            End If
        End If
    Next
End Sub
 
Upvote 0
Solution
Added error checking, factored out commonality for Royalty, made ElseIf since conditions are mutually exclusive
VBA Code:
Sub A()

   Dim myLastRow As Long
   Dim i As Long
   Dim InvD As Date
  
   On Error GoTo BadDate
   InvD = InputBox("Invoice Date mm/dd/yy")
   On Error GoTo 0
  
   myLastRow = Cells(Rows.Count, "B").End(xlUp).Row
  
   For i = 2 To myLastRow
  
      If Range("I" & i).Value Like "Sub-Producer" & "*" Then
     
         Range("I" & i).Value = "Sub-Producer Payment - " & Format(DateAdd("M", -1, Now), "MMMM YYYY")
         Range("V" & i).Value = 23140
         Range("E" & i).Value = InvD
         Range("F" & i).Value = "=R[0]C[-1]+45"
        
      ElseIf Range("I" & i).Value Like "Royalty" & "*" Then
     
         Range("V" & i).Value = 23150
         Range("E" & i).Value = InvD
         Range("F" & i).Value = "=R[0]C[-1]+45"
        
         If InStr(1, Range("D" & i).Value, 7) = 7 Then
            Range("I" & i).Value = "Royalty Guarantee - " & Format(DateAdd("M", 0, Now), "MMMM YYYY")
         Else
            Range("I" & i).Value = "Royalty Payment - " & Format(DateAdd("M", 0, Now), "MMMM YYYY")
         End If
     
      End If
  
   Next i
  
   Exit Sub
  
BadDate:
   MsgBox "Enter a valid date in the form mm/dd/yy"
   
End Sub
 
Upvote 0
Thanks. I knew there would be way to combine the royalty one together but I always have issues with if statements in VBA.
 
Upvote 0
Thanks. I knew there would be way to combine the royalty one together but I always have issues with if statements in VBA.
Did you take a look at the code I posted in the very first reply? I think it is even shorter, as I removed a few more redundancies.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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