Probably Simple

jtsou

New Member
Joined
Oct 28, 2014
Messages
8
I am trying to accomplish a couple things with macros. It's been a while since I used excel.

The workbook contains a sheet for every week in the year. The date in Cell A2 contains a date of every Monday in the year.

I want to run a macro that renames the worksheet to contents of A2(the date). After messing around I realize that I cannot have "/" in the name of the worksheet, so if possible I'd like the / replaced with - in the same instance.

I'd like to be able to run it on every sheet in the workbook besides the first two.

I've truly worked at this for an hour but keep running into errors whenever I research and attempt something new, namely Type Mismatch.

What I have so far-

Code:
Sub tabname()
Dim ws As Worksheet
For Each ws In Worksheets
    On Error Resume Next
    If Len(ws.Range("A2:D2")) > 0 Then
        ws.name = Replace(ws.Range("A2:D2").Value, "/", "-")
    End If
    On Error GoTo 0
    If ws.name <> Replace(ws.Range("A2:D2").Value, "/", "-") Then
        MsgBox ws.name & " Was Not renamed, the suggested name was invalid"
    End If
Next
End Sub

Thank you!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
If A2:D2 is merged, you will need to refer only to the leftmost cell.. Try
VBA Code:
Sub tabname()
Dim ws As Worksheet
For Each ws In Worksheets
    On Error Resume Next
    If Len(ws.Range("A2")) > 0 Then
        ws.Name = Replace(ws.Range("A2").Value, "/", "-")
    End If
    On Error GoTo 0
    If ws.Name <> Replace(ws.Range("A2").Value, "/", "-") Then
        MsgBox ws.Name & " Was Not renamed, the suggested name was invalid"
    End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,513
Members
448,967
Latest member
screechyboy79

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