macro does not like $

ndendrinos

Well-known Member
Joined
Jan 17, 2003
Messages
1,694
This code (restores named ranges in case they get in trouble) errors like this
Code:
Compile error/Invalid character,syntax error
& the $ sign of $A$2,0,0 is highlighted

Code:
Sub RestoreNamedRanges()
Dim Rng1 As Range     
Set Rng1 =OFFSET(Customers!$A$2,0,0,COUNTA(Customers!$A:$A),1)
ActiveWorkbook.Names.Add Name:="Customers", RefersTo:=Rng1 
End Sub

What am I doing wrong again?
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You want a string not a range here (being the difference betwee a formula that returns a range, and the range it refers to).

Code:
Sub RestoreNamedRanges()
Dim sFormula As String     
sFormula = "=OFFSET(Customers!$A$2,0,0,COUNTA(Customers!$A:$A),1)"
ActiveWorkbook.Names.Add Name:="Customers", RefersTo:=sFormula 
End Sub

I think you could evaluate the formula to get the range reference itself, but this would not be a dynamic range any more, but a hard coded one:
Code:
Sub RestoreNamedRanges()
Dim rng1 As Range     
Set rng1 = WorksheetFunction.OFFSET(Range("Customers!$A$2"),0,0,WorksheetFunction.COUNTA(Range("Customers!$A:$A")),1)
rng1.Name = "Customers"
End Sub

Not tested in either case ... Hope it works.
ξ
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

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