For Loop A to E?? Is there away to loop variables?

Chrisjd2

Board Regular
Joined
Mar 1, 2016
Messages
61
Hi chaps.

I have the following code, that I want to loop through 5 variables (A to E). This then populates an address ready to process into word.

But it doesn't work, probably because a For loop has to loop numerically. I just wondered if there is a way to do what I want it to do??

Code:
Sub Macro1()Dim A As String
Dim B As String
Dim C As String
Dim D As String
Dim E As String


Addrs = ""


A = "1ST LINE"
B = "2ND LINE"
C = "3RD LINE"
D = "City"
E = "Postcode"


For i = A To E
    If i <> "" Then
        Addrs = Addrs & Chr(13) & Chr(9) & Chr(9) & Chr(9) & Chr(9) & Chr(9) & Chr(9) & i
    End If
Next i


End Sub

So the end product would be... Addrs =

1st Line
2nd Line
3rd Line
City
Postcode

or

1st Line
3rd Line
City
Postcode

if B = ""

Cheers
Chris
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Not really sure how you plan on using this, but maybe the following will help.
You can store all the entries in an array, and then loop through the array.

Here is a simple example, that just lists each member of the array out in separate message boxes:
Code:
Sub MyTest()

    Dim adds As Variant
    Dim i As Long
    
'   Populate array with values
    adds = Array("1ST LINE", "2ND LINE", "3RD LINE", "CITY", "POSTCODE")
    
'   Loop through array
    For i = LBound(adds) To UBound(adds)
        MsgBox adds(i)
    Next i
    
End Sub
 
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