Assigning values to variables in a loop

jardenp

Active Member
Joined
May 12, 2009
Messages
373
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
Platform
  1. Windows
I am not well versed in loops, so please bear with me.

In A1:A20 there is a list of PO numbers of variable count, i.e., there could be one PO, twenty POs, or any number inbetween.

I have 20 variables declared. PO1, PO2, ... PO20. I also have POCount declared and assigned the count of populated cells in A1:A20 (using a simple xlEnd function). All are Variants, though I suppose POCount could be Integer if needed.

There should never be a blank cell with a non-blank cell below it.

I would like a VBA loop to start in A1 and then loop through to A&POCount and in each cell assign the cell value to the variable PO&row number. So value from A4 is assigned to PO4, A7 to PO7, etc.

I can't figure out how to construct a variable name in the loop and use it (basically how to get it to recognize "PO" & i as a single variable name.

Thanks,

Josh in IN
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
This isnt quite what you ask for but its the way id go:

Code:
PO = Range("A1:A" & POCount)

This puts the values into an array. Then refer to them:

Code:
PO(1,1)

as an example (or PO(2,1), PO(3,1) etc)
 
Upvote 0
Thanks. I'm assuming I would need to declare PO as a Range?
 
Upvote 0
Thanks! I appreciate you taking the time to share your knowledge. This board is great.
 
Upvote 0

Forum statistics

Threads
1,216,741
Messages
6,132,448
Members
449,728
Latest member
teodora bocarski

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