VBA Case NamedRange.value

Kamolga

Well-known Member
Joined
Jan 28, 2015
Messages
1,185
Hi,

I build a template in multiple languages. I therefore set up a named range "Language" that can be "Fr", "Eng" or "Nl" (those values are not stored in any cell but through a userform when opening the template). All my titles are looking up on this value so everything in the forms changes accordingly.

Now that I want to change sheet names and chart titles with VBA, nothing is happening with my case selection

If I type
Code:
Debug.Print ActiveWorkbook.Names("Language").Value
I get
Code:
="Nl"
in my immediate window

If I use
Code:
[LEFT][COLOR=#222222][FONT=Verdana]ShChart.name="Tabel"[/FONT][/COLOR][/LEFT]
my sheet changes name perfectly
but if I type

Code:
 Select Case ActiveWorkbook.Names("Language").Value
             Case "Nl"
              ShChart.name="Tabel"
End select

nothing happens. I have tried Case is ="Nl" without success
 
Last edited:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I would recommend you to be more specific when referencing objects.
How have you set the ShChart name ? Which is actually the ActiveWorkbook?

Try to debug. Put these lines just before Select Case:
Code:
Debug.print "language", ActiveWorkbook.Names("Language").Value
Debug.print "Workbook", ActiveWorkbook.Name,  ActiveWorkbook.path
Debug.print "Chart", ShChart.Name,"parentWB",ShChart.Parent.Name
and see if there is anything not in order.
 
Upvote 0
Hi

First, this is not a named range, this name does not refer to a range. This is a named constant.

2 options

1 -
You posted that the name value is

="Nl"

in this case you have to use:

Code:
Select Case ActiveWorkbook.Names("Language").Value
    Case "=""Nl"""
        Sheet8.Name = "Tabel"
End select

2 -
You evaluate the name to get the constant it refers to.

In this case the code is simpler:

Code:
Select Case Evaluate(ActiveWorkbook.Names("Language").Value)
    Case "Nl"
        Sheet8.Name = "Tabel"
End Select

Please try
 
Upvote 0
Hi,

Thank you for the answer. Evaluate is working perfectly and would have never thaught about it.

No success with 1st option. I even tried to change
.Value with .RefersToR1C1 and "=""Nl""" (which is what I get if I change the constant value while recording) without success.
 
Upvote 0
I'm glad it works OK.


No success with 1st option.

Both should work.

What do you get when you run

Code:
Sub test()

' if name "Language" exists delete it
On Error Resume Next
ActiveWorkbook.Names("Language").Delete
On Error GoTo 0

' add the name "Language" referring to the constant "Nl"
ActiveWorkbook.Names.Add Name:="Language", RefersTo:="=""Nl"""

Select Case ActiveWorkbook.Names("Language").Value
    Case "=""Nl""": MsgBox "OK1"
End Select

Select Case Evaluate(ActiveWorkbook.Names("Language").Value)
    Case "Nl": MsgBox "OK2"
End Select

End Sub
 
Upvote 0
I just realised the success depended if I was already on the sheet or not :banghead:, not the value "=""Nl""" or evaluate()
. Indeed I receive Ok1 and OK2 and both work now :laugh:<strike></strike>
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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