vba cell reference using named column

Ralph Gregory

Board Regular
Joined
Aug 14, 2003
Messages
125
hi I am trying to get a value from a cell in a named column can anyone help with the correct format?

a=8
range("D2") = Range("Myname" & a) * Range("X1")

this returns an error
what am I doing wrong?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
This is unclear. Possibly

Code:
Range("D2").Value = Range("Myname" & a * Range("X1")).Value
 
Upvote 0
This is unclear. Possibly

Code:
Range("D2").Value = Range("Myname" & a * Range("X1")).Value

no still gives an error
to clarify range myname refers to $B:$B which is a complete column
the reason I want to name the column is so that if I add another column later
I dont have to redo all the cell references in my formulas.
so in vba
a = 2
Range("D2") = Range("B" & a) * Range("X1") works fine
but if I add a column before B my data has moved and the formula is useless
so how do I include the named column information in the formula?
 
Upvote 0
no still gives an error
to clarify range myname refers to $B:$B which is a complete column
the reason I want to name the column is so that if I add another column later
I dont have to redo all the cell references in my formulas.
so in vba
a = 2
Range("D2") = Range("B" & a) * Range("X1") works fine
but if I add a column before B my data has moved and the formula is useless
so how do I include the named column information in the formula?

HeHe answer to myself...worked it out after loads of trial and error
C = Range("MyColumn").Column
a = 2
Range("D2") = Cells(a, C) * Range("X1")
 
Upvote 0
The .Cells property of a range is relative to the top left cell of the range. So an alternative could avoid using the unqualified Cells (relative to the ActiveSheet by default), like this:
Code:
a = 2

Range("D2") = Range("myColumn").Cells(a, 1) * Range("X1")
 
Upvote 0
The .Cells property of a range is relative to the top left cell of the range. So an alternative could avoid using the unqualified Cells (relative to the ActiveSheet by default), like this:
Code:
a = 2

Range("D2") = Range("myColumn").Cells(a, 1) * Range("X1")

Thanks Mikerson that's very elegant and gives me a bit more understanding as to how it works.
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

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