Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 100
Default Naming a Range for use in a Non-Formula

Greetings. I have the following "UseNames" procedure in which I am trying to
give a range a meaningful name such that the name can be used in a
non-formula:

Sub UseNames()

' If I have the following code:

Dim Month As String

If Range("$G$1") = 28 Then
Month = "February"
End If

' In order to make the code more explanatory I could also code this as:

Dim vNrDays As Long
vNrDays = Range("$G$1")

If vNrDays = 28 Then
Month = "February"
End If

' which requires an extra statement and extra memory and runs more slowly.

' I thought I could also do the following:
Range("$G$1").Name = ActiveSheet.Name & "!NrDays"

If NrDays = 28 Then
Month = "February"
End If

' but when I execute this, VBE tells me: "Compile Error. Variable not
defined."
' I then recalled that named ranges can only be used in formulas, and I was
' indeed able to use the name NrDays in a formula.
' Is there any way (other than storing the range in a variable as above)
' of giving the range, Range("$G$1"), a meaningful name such that it can be
' used in non-formula logic, like in the IF-THEN code below:

If Range("$G$1") = 28 Then
Month = "February"
End If

' NrDays is so much more explanatory and meaningful than Range("$G$1")

End Sub
--
May you have a most blessed day!

Sincerely,

Michael Fitzpatrick
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,420
Default Naming a Range for use in a Non-Formula

Like so

If Range("NrDays") = 28 Then
Month = "February"
End If


--
__________________________________
HTH

Bob

"MichaelDavid" wrote in message
...
Greetings. I have the following "UseNames" procedure in which I am trying
to
give a range a meaningful name such that the name can be used in a
non-formula:

Sub UseNames()

' If I have the following code:

Dim Month As String

If Range("$G$1") = 28 Then
Month = "February"
End If

' In order to make the code more explanatory I could also code this as:

Dim vNrDays As Long
vNrDays = Range("$G$1")

If vNrDays = 28 Then
Month = "February"
End If

' which requires an extra statement and extra memory and runs more slowly.

' I thought I could also do the following:
Range("$G$1").Name = ActiveSheet.Name & "!NrDays"

If NrDays = 28 Then
Month = "February"
End If

' but when I execute this, VBE tells me: "Compile Error. Variable not
defined."
' I then recalled that named ranges can only be used in formulas, and I
was
' indeed able to use the name NrDays in a formula.
' Is there any way (other than storing the range in a variable as above)
' of giving the range, Range("$G$1"), a meaningful name such that it can
be
' used in non-formula logic, like in the IF-THEN code below:

If Range("$G$1") = 28 Then
Month = "February"
End If

' NrDays is so much more explanatory and meaningful than Range("$G$1")

End Sub
--
May you have a most blessed day!

Sincerely,

Michael Fitzpatrick



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 100
Default Naming a Range for use in a Non-Formula

Hi Bob:
Thanks for your help. Your "Like So" is perfect. None of the Excel VBA
books I have (and I have about 6) gives this usage.
--
May you have a most blessed day!

Sincerely,

Michael Fitzpatrick


"Bob Phillips" wrote:

Like so

If Range("NrDays") = 28 Then
Month = "February"
End If


--
__________________________________
HTH

Bob

"MichaelDavid" wrote in message
...
Greetings. I have the following "UseNames" procedure in which I am trying
to
give a range a meaningful name such that the name can be used in a
non-formula:

Sub UseNames()

' If I have the following code:

Dim Month As String

If Range("$G$1") = 28 Then
Month = "February"
End If

' In order to make the code more explanatory I could also code this as:

Dim vNrDays As Long
vNrDays = Range("$G$1")

If vNrDays = 28 Then
Month = "February"
End If

' which requires an extra statement and extra memory and runs more slowly.

' I thought I could also do the following:
Range("$G$1").Name = ActiveSheet.Name & "!NrDays"

If NrDays = 28 Then
Month = "February"
End If

' but when I execute this, VBE tells me: "Compile Error. Variable not
defined."
' I then recalled that named ranges can only be used in formulas, and I
was
' indeed able to use the name NrDays in a formula.
' Is there any way (other than storing the range in a variable as above)
' of giving the range, Range("$G$1"), a meaningful name such that it can
be
' used in non-formula logic, like in the IF-THEN code below:

If Range("$G$1") = 28 Then
Month = "February"
End If

' NrDays is so much more explanatory and meaningful than Range("$G$1")

End Sub
--
May you have a most blessed day!

Sincerely,

Michael Fitzpatrick




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Range naming Squeaky Excel Discussion (Misc queries) 2 December 29th 06 09:10 PM
Naming a range versus formula in Excel Jos Vens Excel Programming 4 July 14th 04 01:26 PM
naming a range Jo[_6_] Excel Programming 1 June 24th 04 11:02 PM
Naming a range Andrew B[_2_] Excel Programming 2 May 31st 04 01:34 AM
VB Code Naming a Range (range changes each time) krazylain Excel Programming 4 May 15th 04 12:41 PM


All times are GMT +1. The time now is 09:45 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"