Run-time error: '1004': The syntax of this name isn't correct

Derick63

Board Regular
Joined
Apr 6, 2018
Messages
71
Hello all.

I've been banging my head against the wall on this one and I finally threw my hands into the air. I definitely need more brain power than I obviously have going on now.

My VBA code Creates a new worksheet, names it via an Inputbox function and "supposed" to create three new named ranges. Everything goes well until naming the ranges. I have stepped through the macro (F8) and that's where I get the 1004 error code. I think the error is happening at the "RefersTo:=" section.

I know this is well above my level of VBA.

Any help is greatly appreciated.

I removed non-pertinent sheet formatting code for clarity.

Code:
Code:
Sub Macro9()


'Get effective date of new contribution rates
    Dim Message, Title, Default, RateDate, NewWsName As Variant

    Message = "Enter Effective Date of Contribution Rate Chart."
    Title = "Contribution Rates Chart Effective Date"
    Default = "YYYY-MM-DD"
    RateDate = InputBox(Message, Title, Default)
    
'Add new sheet and rename it to NIS-[RateDate]
    ActiveWorkbook.Sheets.Add after:=Worksheets(Worksheets.Count) 'Last tab
    ActiveSheet.Name = "NIS-" & RateDate
    
    NewWsName = ActiveSheet.Name 'used below to create named ranges
    
'New Named Ranges for NIS formula to be replaced
    Dim NISLow, NISHigh, NisContr As Range
    Dim RangeNameLow, RangeNameHigh, RangeNameContr As String
    
    Set NISLow = Sheets(NewWsName).Range("A4:A19")
    Set NISHigh = Sheets(NewWsName).Range("B4:B19")
    Set NisContr = Sheets(NewWsName).Range("C4:C19")
    
    RangeNameLow = "NISLow" & RateDate
    RangeNameHigh = "NISHigh" & RateDate
    RangeNameContr = "NISContr" & RateDate
    
    ThisWorkbook.Names.Add Name:=RangeNameLow, RefersTo:=NISLow
    ThisWorkbook.Names.Add Name:=RangeNameHigh, RefersTo:=NISHigh
    ThisWorkbook.Names.Add Name:=RangeNameContr, RefersTo:=NisContr
    
    Range("A3").Select

End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I think RateDate is your problem...
The name box won't allow dashes / slashes

remove ratedate from the "RangeNameLow = "NISLow" & RateDate" lines and it will work
 
Upvote 0
You might need to format ratedate, if it contains any slashes.

Code:
Sub Macro9()




'Get effective date of new contribution rates
    Dim Message, Title, Default, RateDate, NewWsName As Variant
    Dim ratedate2 As String


    Message = "Enter Effective Date of Contribution Rate Chart."
    Title = "Contribution Rates Chart Effective Date"
    Default = "YYYY-MM-DD"
    RateDate = InputBox(Message, Title, Default)
    ratedate2 = Format(RateDate, "ddmmyyyy")
    
'Add new sheet and rename it to NIS-[RateDate]
    ActiveWorkbook.Sheets.Add after:=Worksheets(Worksheets.Count) 'Last tab
    ActiveSheet.Name = "NIS-" & RateDate
    
    NewWsName = ActiveSheet.Name 'used below to create named ranges
    
'New Named Ranges for NIS formula to be replaced
    Dim NISLow, NISHigh, NisContr As Range
    Dim RangeNameLow, RangeNameHigh, RangeNameContr As String
    
    Set NISLow = Sheets(NewWsName).Range("A4:A19")
    Set NISHigh = Sheets(NewWsName).Range("B4:B19")
    Set NisContr = Sheets(NewWsName).Range("C4:C19")
    
    RangeNameLow = "NISLow" & RateDate
    RangeNameHigh = "NISHigh" & RateDate
    RangeNameContr = "NISContr" & RateDate
    
    ThisWorkbook.Names.Add Name:=RangeNameLow, RefersTo:=NISLow
    ThisWorkbook.Names.Add Name:=RangeNameHigh, RefersTo:=NISHigh
    ThisWorkbook.Names.Add Name:=RangeNameContr, RefersTo:=NisContr
    
    Range("A3").Select


End Sub
 
Upvote 0
Michael, Thanks for the reply. It was a simple fix. I just removed all instances of a "-" (dash/hyphen) and it flew through the macro. Can't believe it was that simple..... Duhhh on me...
 
Upvote 0
mrshl9898, Thanks so much for the reply. I like what you did there. I just removed all instances of a "-" (dash/hyphen) and it flew through the macro.
 
Upvote 0

Forum statistics

Threads
1,215,748
Messages
6,126,654
Members
449,326
Latest member
asp123

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