Dynamic variables

rhinhart

New Member
Joined
Jan 22, 2014
Messages
2
hi all, I am working on a project which I wrote repeatedly "Sheets(x).Cells(i,n)" and "Sheets(y).Cells(j,m)"

where x, y, n and m are constants, while i and j are loop variables.

After a while, I am lost on which is which. Hereby I want to create two variables, preferably named "targetCell" and "curCell" where
targetCell = Sheets(x).Cells(i,n)
curCell = Sheets(y).Cells(j,m)

Is there anyway I can define those outside the loop so targetCell and curCell values changes whenever i and j changes?

I tried to search the forum with keyword "dynamic variables", but maybe I use the wrong keyword and did not found any good help. Please pardon me if this is a duplicate question and if anyone can provide a link to the post it will be great.

Thank you in advance for your help.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
hi all, I am working on a project which I wrote repeatedly "Sheets(x).Cells(i,n)" and "Sheets(y).Cells(j,m)"

where x, y, n and m are constants, while i and j are loop variables.

After a while, I am lost on which is which. Hereby I want to create two variables, preferably named "targetCell" and "curCell" where
targetCell = Sheets(x).Cells(i,n)
curCell = Sheets(y).Cells(j,m)

Is there anyway I can define those outside the loop so targetCell and curCell values changes whenever i and j changes?

I tried to search the forum with keyword "dynamic variables", but maybe I use the wrong keyword and did not found any good help. Please pardon me if this is a duplicate question and if anyone can provide a link to the post it will be great.

Thank you in advance for your help.

They have to be defined after the values they contain are initialized, otherwise they will be empty. So I would say they need to be defined inside the loop.
 
Upvote 0
They have to be defined after the values they contain are initialized, otherwise they will be empty. So I would say they need to be defined inside the loop.

Thank you for your information.
Is there anyway to define it as a pointer or something else? There is actually much more than those two I mentioned above.
 
Upvote 0
Thank you for your information.
Is there anyway to define it as a pointer or something else? There is actually much more than those two I mentioned above.

I'm not sure exactly what you are trying to do. Have you looked at using a public variable? It can change value and still be used throughout the code, but it gets a little tricky if you are not keeping a close watch on how you write the code. The programmer has to know what the public variable is when they use it in the code. Otherwise, you get some undesired results.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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