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

Derick63

Board Regular
Joined
Apr 6, 2018
Messages
76
Office Version
  1. 365
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

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
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,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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