VBA - Looping to add to a String variable.

GiantPygmy

New Member
Joined
Jul 26, 2016
Messages
6
Hello,

I'm attempting to get VBA to loop through cells on a sheet to collect information and then take that information from each cell and add it to a string variable.

so for a basic example I have a list of names in column A (the list constantly is updated and changed). I would like to be able to take a name and add it to a variable that will format it how I want, then get the next name and add it to the correct spot where I want to the same string. I would like the variable to be able to store a format like:

Name: *first name*
Name: * second name*
Name: *third name*


So on and so forth so until it has listed all the names in column A. Ultimately I am going to use this to copy it to the clipboard (which I already have worked out) but I just am not sure how to get all the elements to continue looping to collect the information and then add it to create one variable with all that information formatted how I need it to look.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Let's start by identifying all the rows that we will evaluate:

Code:
LastRow = Range("A" & Rows.Count).End(xlUp).Row

Then we will go through each used line and concatenate the string:
(I am going to add a space between the names but you can make that space a comma and you get a CSV file at the end)
Code:
StringCache = vbNullString
For RowStep = 1 to LastRow[INDENT]StringCache = StringCache & Range("A" & RowStep).Value & " "[/INDENT]
Next
 
Upvote 0

Forum statistics

Threads
1,203,352
Messages
6,054,910
Members
444,759
Latest member
TeckTeck

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