UserForm VBA // Repeating code in a smart way

Young Grasshopper

Board Regular
Joined
Dec 9, 2022
Messages
58
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi World!

I have this code in a vba userform. I need every referance cell to increase by one (D1, D2, D3 etc.) for every "IF Then".
My original plan was just to do it manually, as started, but as the code needs to be repeated 100 times, there is probably a faster way to do it..

VBA Code:
Private Sub Testbtn_Click()
        
        If Me.ComboBox1.Text = Worksheets("Macrodata").Range("D1").Text Then
            Headline.Value = Worksheets("MacroData").Range("AD2").Value
                Description.Value = Worksheets("MacroData").Range("AE2").Value
                    URL.Value = Worksheets("Responsive Search Ads HUB").Range("D11").Value
         End If
         
         If Me.ComboBox1.Text = Worksheets("Macrodata").Range("D2").Text Then
            Headline.Value = Worksheets("MacroData").Range("AD3").Value
                Description.Value = Worksheets("MacroData").Range("AE3").Value
                    URL.Value = Worksheets("Responsive Search Ads HUB").Range("D12").Value
         End If
         
          If Me.ComboBox1.Text = Worksheets("Macrodata").Range("D3").Text Then
            Headline.Value = Worksheets("MacroData").Range("AD4").Value
                Description.Value = Worksheets("MacroData").Range("AE4").Value
                    URL.Value = Worksheets("Responsive Search Ads HUB").Range("D13").Value
         End If
         
         If Me.ComboBox1.Text = Worksheets("Macrodata").Range("D4").Text Then
            Headline.Value = Worksheets("MacroData").Range("AD5").Value
                Description.Value = Worksheets("MacroData").Range("AE5").Value
                    URL.Value = Worksheets("Responsive Search Ads HUB").Range("D14").Value
         End If
         

End Sub

Any suggestions?
Would appreciate any help:)
 

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.
Try:
VBA Code:
Private Sub Testbtn_Click()
Dim i As Long

With Worksheets("Macrodata")
        For i = 0 To 3
            If Me.ComboBox1.Text = .Range("D1").Offset(i).Text Then
                Headline.Value = .Range("AD2").Offset(i).Value
                    Description.Value = .Range("AE2").Offset(i).Value
                        Url.Value = Worksheets("Responsive Search Ads HUB").Range("D11").Offset(i).Value
                Exit For  'once the criteria are met, no need to check the rest, so exit the loop
            End If
        Next
End With
         
End Sub
 
Upvote 0
Solution
You're a lifesaver, Akuini.. Works perfect and saves me hours of work!
If i would like the code to go over 100 rows i can just tweak it to "For i = 0 To 99", right?
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,839
Members
449,051
Latest member
excelquestion515

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