Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Change Defined Name Constant String

I'm trying to change the defined name string constant using VBA. Is it
possible? Here is my code:

Sub ChangeNameString()

Dim Next_String As String

' this works ok, defined name shows "First_String" (with quotes) (not a
variable)
ActiveWorkbook.Names.Add Name:="MyString", _
RefersToR1C1:="=""First_String"""

Next_String = "Second_String"

' doesn't work when attempting to change name with Next_String varible
(no quotes)
ActiveWorkbook.Names.Add Name:="MyString", _
RefersToR1C1:="=" & Next_String

End Sub

I've tried several variations but nothing works. Any ideas?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 811
Default Change Defined Name Constant String


The line of code where you use the string variable to create the name
should look like this:

ActiveWorkbook.Names.Add Name:="MyString", _
RefersToR1C1:="=""" & Next_String & """"

Note the embedded double quotes that are required for a named constant of
type String.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm

"Ronster" wrote in message
ups.com...
I'm trying to change the defined name string constant using VBA. Is it
possible? Here is my code:

Sub ChangeNameString()

Dim Next_String As String

' this works ok, defined name shows "First_String" (with quotes) (not a
variable)
ActiveWorkbook.Names.Add Name:="MyString", _
RefersToR1C1:="=""First_String"""

Next_String = "Second_String"

' doesn't work when attempting to change name with Next_String varible
(no quotes)
ActiveWorkbook.Names.Add Name:="MyString", _
RefersToR1C1:="=" & Next_String

End Sub

I've tried several variations but nothing works. Any ideas?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Change Defined Name Constant String

' doesn't work when attempting to change name with Next_String varible
(no quotes)


I assume you mean you can define the name OK but you can't use it in the way
you intend, to return a string in a cell formula.

That's because you've defined a name that expects to refer to another Name
named Second_String, which presumably doesn't exist. Try instead -

Next_String = "Second_String"

ActiveWorkbook.Names.Add Name:="MyString", _
RefersToR1C1:="=" & Chr(34) & Next_String & Chr(34)

Regards,
Peter T


"Ronster" wrote in message
ups.com...
I'm trying to change the defined name string constant using VBA. Is it
possible? Here is my code:

Sub ChangeNameString()

Dim Next_String As String

' this works ok, defined name shows "First_String" (with quotes) (not a
variable)
ActiveWorkbook.Names.Add Name:="MyString", _
RefersToR1C1:="=""First_String"""

Next_String = "Second_String"

' doesn't work when attempting to change name with Next_String varible
(no quotes)
ActiveWorkbook.Names.Add Name:="MyString", _
RefersToR1C1:="=" & Next_String

End Sub

I've tried several variations but nothing works. Any ideas?



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Change Defined Name Constant String


Peter T wrote:
' doesn't work when attempting to change name with Next_String varible
(no quotes)


I assume you mean you can define the name OK but you can't use it in the way
you intend, to return a string in a cell formula.

That's because you've defined a name that expects to refer to another Name
named Second_String, which presumably doesn't exist. Try instead -

Next_String = "Second_String"

ActiveWorkbook.Names.Add Name:="MyString", _
RefersToR1C1:="=" & Chr(34) & Next_String & Chr(34)

Regards,
Peter T



Both seem to work great. Thanks!
"Ronster" wrote in message
ups.com...
I'm trying to change the defined name string constant using VBA. Is it
possible? Here is my code:

Sub ChangeNameString()

Dim Next_String As String

' this works ok, defined name shows "First_String" (with quotes) (not a
variable)
ActiveWorkbook.Names.Add Name:="MyString", _
RefersToR1C1:="=""First_String"""

Next_String = "Second_String"

' doesn't work when attempting to change name with Next_String varible
(no quotes)
ActiveWorkbook.Names.Add Name:="MyString", _
RefersToR1C1:="=" & Next_String

End Sub

I've tried several variations but nothing works. Any ideas?


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
Change 3 letter text string to a number string Pete Excel Discussion (Misc queries) 3 December 31st 07 07:47 PM
How can I change the tab but keep the row and column constant when MG Excel Worksheet Functions 25 June 20th 07 01:51 PM
Change a constant from VB ccarmock[_7_] Excel Programming 2 May 6th 06 02:48 AM
How to replace defined portion of string ExcelMonkey Excel Programming 2 July 19th 05 12:21 PM
How will I do Excel user-defined function to extract letters from string Wavit11 Excel Programming 1 April 30th 04 04:56 AM


All times are GMT +1. The time now is 10:42 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"