Quick help needed: variable within a variable...

bapcki1

New Member
Joined
Nov 12, 2011
Messages
39
Hi Guys!

I would like to declare a variable that uses a value from another variable. (is this known as dynamic variables?)

The offending line is:

Code:
Wbq = ThisWorkbook.Sheets("Sheet 1").Cells(NumEntry, 2).Value

where NumEntry is the variable within another variable, Wbq.

It looks like this in the whole code text:

Code:
Sub Collation()

ThisWorkbook.Sheets("Collation").Range("B2:F12").ClearContents

Dim DiffcalcWb As Integer
Dim Wborig As Integer
Dim Wbq As Integer

NumEntry = ThisWorkbook.Sheets("Collation").Range("L2").Value
Wborig = ThisWorkbook.Sheets("Sheet 1").Range("B2").Value
Wbq = ThisWorkbook.Sheets("Sheet 1").Cells(NumEntry, 2).Value
DiffcalcWb = Wborig - Wbq
Sheets("Collation").Select
Range("B2").Value = DiffcalcWb

Can anyone help me please?


Paddy
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi Dryver,

Thanks, but the problem of naming the variable within a variable still remains..

Any ideas?


paddy
 
Upvote 0
this should not cause a problem assuming numentry is a number

what is numentry returning?
 
Upvote 0
Hi Shg,

Sorry I was typing out the code manually, and I made a couple of mistakes but the naming a variable within a variable problem still isn't working...

I would have declared NumEntry:
Code:
Dim NumEntry As Integer
 
Upvote 0
Hi Dryver,

when I type

Code:
? NumEntry

into the immediate window and hit return, nothing happens. NumEntry should equal 3.


Thanks for your help - I really appreciate it.

Paddy
 
Upvote 0
*Correcting the left out lines*

Code:
Sub Collation()

ThisWorkbook.Sheets("Collation").Range("B2:F12").ClearContents

Dim DiffcalcWb As Integer
Dim Wborig As Integer
Dim Wbq As Integer
Dim NumEntry As Integer

NumEntry = ThisWorkbook.Sheets("Collation").Range("L2").Value
Wborig = ThisWorkbook.Sheets("Sheet1").Range("B2").Value
Wbq = ThisWorkbook.Sheets("Sheet1").Cells(NumEntry, 2).Value
DiffcalcWb = Wborig - Wbq
Sheets("Collation").Select
Range("B2").Value = DiffcalcWb
 
Upvote 0
this is what the immediate window comes up with:

Code:
?ThisWorkbook.Sheets("Collation").Range("J2").Value
 3 
?NumEntry = ThisWorkbook.Sheets("Collation").Range("J2").Value
False
?NumEntry

I have declared the variable as:

Code:
Sub AmazonCollation()
Dim NumEntry As Integer
NumEntry = ThisWorkbook.Sheets("Collation").Range("J2").Value

?????

so confused, please help..


Paddy
 
Upvote 0
You're not "naming a variable within another variable". I don't even know what that's supposed to mean. You're using a variable in very standard way.

Post your code as it exists or you risk getting an incorrect answer. We don't want to be guessing that you have variable scope correct, for example (ie declared within the same sub).

Typing ?variable into the immediate window after the procedure has run will only return a value if the variable still has scope - ie has been declared at the module level which you are indicating is not the case

add this line

Code:
NumEntry = ThisWorkbook.Sheets("Collation").Range("L2").Value
[B]debug.print NumEntry[/B]

will return the actual value at runtime.

Also specify the error you're receiving.

My guess would be that "L2" contains a zero or zero equivalent value.
 
Upvote 0

Forum statistics

Threads
1,223,108
Messages
6,170,152
Members
452,306
Latest member
chenhi131

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