Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to refer a named cell in Excel with VBA? 3

Status
Not open for further replies.

gzhong

Programmer
Apr 11, 2001
2
US
I am trying to avoid using Sheet.cells(col,row) by defining names to those cells I am going to use later on. Is there a way to use the value of named cells by calling their names in VBA?

Thanks,

Gregory
 
If you are simply want to use the VALUE of the cell, then use the following:

Range("RangeName").Value

Examples:

1) To set the value to a variable
varname = Range("RangeName").Value

2) To use the value in a calculation
newval = Range("RangeName").Value * 1.175

3) To incement the cell's value
Range("RangeName").Value = Range("RangeName").Value + 1

Hope this helps.
 
If you are just naming cells, it is much easier than that. Let's say you name cell B13 as 'ProductID'. The usage depends on how the cell is formatted. To use this value in VBA:
Code:
Dim sVal As String
Dim dVal As Double
Dim iVal As Integer

'echo original value
MsgBox [ProductID]

'store value (formatted as text)
sVal = [ProductID]

'store value (formatted as number)
dVal = CDbl([ProductID])

'store value (formatted as a whole number)
iVal = CInt([ProductID])
 
Thanks all for your help. For my application, DSI's code works perfect.

Gregory
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top