Problems with name-range or group

espenskeie

Well-known Member
Joined
Mar 30, 2009
Messages
636
Office Version
  1. 2016
Platform
  1. Windows
Hi

I try to create a dynamic name group, but i am stuck.

This my code, or what I have tried to write:

Code:
ActiveWorkbook.Names.Add Name:="A2" & "_Exchange", RefersToR1C1:= _
        "=" & "A2" & "!R2C1:R" & lr2 & "C1"
    ActiveWorkbook.Names((ActiveSheet.Range("A2")" & "Exchange").Comment = ""

This line is bugging:

ActiveWorkbook.Names((ActiveSheet.Range("A2")" & "Exchange").Comment = ""

And this is the code without dynamic try:

Code:
ActiveWorkbook.Names.Add Name:="Year", RefersToR1C1:= _
        "=" & "A2" & "!R2C4:R" & lr3 & "C4"
    ActiveWorkbook.Names("Year").Comment = ""

Regards
Espen
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Espen

What name are you trying to use for the range?

The first part of the code would make a range called A2_Exchange which refers to a range on a worksheet called 'A2'.

Here's what the range looks like when I use 100 for lr2.

='A2'!$A$2:$A$100
 
Upvote 0
Hi Norie

I think I just found a workaround, I'm just about to finish the writing, so far it looks good:
Code:
Name1 = ActiveSheet.Range("A2") & "_Exchange"
    
''' Lager et "Name" for gruppen Exchange slik at den blir dynamisk
    ActiveWorkbook.Names.Add Name:=Name1, RefersToR1C1:= _
        "=" & "A2" & "!R2C1:R" & lr2 & "C1"
    ActiveWorkbook.Names(Name1).Comment = ""

Regards
Espen
 
Upvote 0
Try something like this...

Code:
[color=darkblue]Dim[/color] cell [color=darkblue]As[/color] Range

ActiveWorkbook.Names.Add Name:=Range("A2").Value & "_Exchange", RefersToR1C1:= _
        "=" & Range("A2").Value & "!R2C1:R" & lr2 & "C1"
        
    [color=darkblue]For[/color] [color=darkblue]Each[/color] cell [color=darkblue]In[/color] Range(Range("A2").Value & "_Exchange")
        [color=darkblue]If[/color] [color=darkblue]Not[/color] (cell.Comment [color=darkblue]Is[/color] [color=darkblue]Nothing[/color]) [color=darkblue]Then[/color] cell.Comment.Text " "
    [color=darkblue]Next[/color] cell
    
ActiveWorkbook.Names.Add Name:="Year", RefersToR1C1:= _
        "=" & Range("A2").Value & "!R2C4:R" & lr3 & "C4"
        
    [color=darkblue]For[/color] [color=darkblue]Each[/color] cell [color=darkblue]In[/color] Range("Year")
        [color=darkblue]If[/color] [color=darkblue]Not[/color] (cell.Comment [color=darkblue]Is[/color] [color=darkblue]Nothing[/color]) [color=darkblue]Then[/color] cell.Comment.Text " "
    [color=darkblue]Next[/color] cell
 
Upvote 0
Thanks AlphaFrog

I found something that did work halfway:

Code:
Name1 = ActiveSheet.Range("A2") & "_Exchange"
    Name2 = ActiveSheet.Range("A2") & "_Year"
    Name3 = ActiveSheet.Range("A2") & "_Month"
    Name4 = ActiveSheet.Range("A2") & "_Day"
    Name5 = ActiveSheet.Range("A2") & "_Frequence"

    ActiveWorkbook.Names.Add Name:=Name1, RefersToR1C1:= _
        "=" & "A2" & "!R2C1:R" & lr2 & "C1"
    ActiveWorkbook.Names(Name1).Comment = ""
    
''' Lager et "Name" for gruppen Year slik at den blir dynamisk
    ActiveWorkbook.Names.Add Name:=Name2, RefersToR1C1:= _
        "=" & "A2" & "!R2C4:R" & lr3 & "C4"
    ActiveWorkbook.Names(Name2).Comment = ""
    
''' Lager et "Name" for gruppen Month slik at den blir dynamisk
    ActiveWorkbook.Names.Add Name:=Name3, RefersToR1C1:= _
        "=" & "A2" & "!R2C5:R" & lr4 & "C5"
    ActiveWorkbook.Names(Name3).Comment = ""
  
    ActiveWorkbook.Names.Add Name:=Name4, RefersToR1C1:= _
        "=" & "A2" & "!R2C6:R" & lr5 & "C6"
    ActiveWorkbook.Names(Name4).Comment = ""
    
    ActiveWorkbook.Names.Add Name:=Name5, RefersToR1C1:= _
        "=" & "A2" & "!R2C7:R" & lr6 & "C7"
    ActiveWorkbook.Names(Name5).Comment = ""

Right now I have two workbooks, AMEX and NYSE. When this code is done, I only have names for NYSE.

I ran it manually, and stopped when AMEX was done, and checked "Namemanager". And all the names was there.

It seems like the code overwrites the second time. Even though the names are different....

Regards
Espen
 
Upvote 0
Hmm, I just wanted to tell you that I messed up the code on my own. I found the error....

I forgot to put the "delete all names" code outside of the loop. Thats why I allways ended up with only names for the last loop.

I'll take 100 pushup as penalty...
 
Upvote 0

Forum statistics

Threads
1,206,920
Messages
6,075,575
Members
446,147
Latest member
homedecortips

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