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

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
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,215,981
Messages
6,128,085
Members
449,418
Latest member
arm56

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