Sheet Copy/Rename VBA Error

agentkramr

Board Regular
Joined
Dec 27, 2021
Messages
95
Platform
  1. Windows
i have a workbook that populates data from an Oracle database.
With my VBA i am trying to refresh the workbook , copy that page to a network workbook and then name the tab in the network workbook to todays date
i am using AI1 to hold my value of todays date using a custom format of (mmm Dd)

VBA Code:
Sub CB_Copy()
'Refresh Workbook
Workbooks("My Workbook").RefreshAll
'Open a workbook

  'Open method requires full file path to be referenced.
  Workbooks.Open \\server\Shares\Folder\Sales\_Nightly Reports\2022 Nightly Reports\2022 2 Week Counts\2022 2 Week Count1.xlsx
 
  Workbooks("My Workbook.xlsm").Activate
 
  Sheets("2018 1 Hour Counts").Copy After:=Workbooks("2022 2 Week Count1.xlsx").Sheets(Workbooks("2022 2 Week Count1.xlsx").Sheets.Count)
    ActiveSheet.Range("A1:AC84").Copy
    ActiveSheet.Range("A1:AC84").PasteSpecial xlPasteValues
    'ActiveSheet.Shapes("Rectangle: Rounded Corners 1").Delete
    'ActiveSheet.Shapes("Rectangle: Rounded Corners 2").Delete
    Dim Val As String
    Val = Sheets("2018 1 Hour Counts").Range("AI1").Value
        ActiveSheet.Name = Val
    'Columns("AF:AI").Delete

    ActiveWorkbook.Save
    ActiveWorkbook.Close
End Sub

I get an error
"You typed an invalid name for a sheet or chart. Make sure that:
  • The name that you type does not exceed 31 charaters
  • The name does not contain any of the following characters: \ / ? * [ or ]
  • You dod not leave the name blank
if it hit debug it goes to the line ActiveSheet.Namer = Val

any help would be greatly appreciated
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Firstly, "Val" is a reserved word (words of existing functions, methods, properties, etc).
You should NEVER used reserved words like "Val" as the names of your variables, procedures, or functions, as that can lead to errors and unexpected results.

So try this update:
VBA Code:
    Dim MyVal As String
    MyVal = Sheets("2018 1 Hour Counts").Range("AI1").Value
    MsgBox MyVal
    ActiveSheet.Name = MyVal
If you still get the error, tell us exactly what the MsgBox returns.
If it works now, you can get rid of the MsgBox.
 
Upvote 0
Firstly, "Val" is a reserved word (words of existing functions, methods, properties, etc).
You should NEVER used reserved words like "Val" as the names of your variables, procedures, or functions, as that can lead to errors and unexpected results.

So try this update:
VBA Code:
    Dim MyVal As String
    MyVal = Sheets("2018 1 Hour Counts").Range("AI1").Value
    MsgBox MyVal
    ActiveSheet.Name = MyVal
If you still get the error, tell us exactly what the MsgBox returns.
If it works now, you can get rid of the MsgBox.
i had no idea about the val was "reserved" thank you for the insight. i am sort of new to the whole macro thing so still learning the ins and outs.
here is a screen cap of the message box
 

Attachments

  • MSGBOX.PNG
    MSGBOX.PNG
    1.7 KB · Views: 6
Upvote 0
Firstly, "Val" is a reserved word (words of existing functions, methods, properties, etc).
You should NEVER used reserved words like "Val" as the names of your variables, procedures, or functions, as that can lead to errors and unexpected results.

So try this update:
VBA Code:
    Dim MyVal As String
    MyVal = Sheets("2018 1 Hour Counts").Range("AI1").Value
    MsgBox MyVal
    ActiveSheet.Name = MyVal
If you still get the error, tell us exactly what the MsgBox returns.
If it works now, you can get rid of the MsgBox.
its is still erroring out with the previous error as well
 
Upvote 0
Its exactly what the error message is telling you. You cannot name your sheet "3/10/2022", as "/" are invalid characters for worksheet names.

Might I suggest using "-" instead of "/", so the sheet is named "3-10-2022"?
To do that, change this line:
VBA Code:
    MyVal = Sheets("2018 1 Hour Counts").Range("AI1").Value
to this:
VBA Code:
    MyVal = Format(Sheets("2018 1 Hour Counts").Range("AI1"),"m-d-yyyy")
 
Upvote 0
Solution
Its exactly what the error message is telling you. You cannot name your sheet "3/10/2022", as "/" are invalid characters for worksheet names.

Might I suggest using "-" instead of "/", so the sheet is named "3-10-2022"?
To do that, change this line:
VBA Code:
    MyVal = Sheets("2018 1 Hour Counts").Range("AI1").Value
to this:
VBA Code:
    MyVal = Format(Sheets("2018 1 Hour Counts").Range("AI1"),"m-d-yyyy")
Works like a charm i changed the date format to mmm dd since i want 3 char abbrev and day. thank you so much !
 
Upvote 0
You are welcome!
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,428
Members
449,083
Latest member
Ava19

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