Check to see if sheet exists and rename it

gingerbreadgrl

New Member
Joined
Aug 19, 2019
Messages
48
Hi All,

I would like to run a macro to check to see if a sheet name exists, if it does exist, I would like to rename that sheet to be the contents of whatever is in cell B1 of that sheet. So far I have not had any luck with the using the following code, seems like it would be simple enough but I'm having trouble. Any thoughts would be much appreciated! Thanks, Gingerbreadgrl

Code:
Sub SummaryReportMacro()


Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
         If ws.Name <> "Record 2" Then
            ws.Name = ws.Range("B1").Value
        End If
    Next ws


End Sub

I've also tried:

Code:
Sub SummaryReportMacro()

Dim sht As Worksheet
For Each sht In Sheets
If sht.Name = "Record 2" Then
Sheets("Record 2") = Range("B1").Value
End If

End Sub

This didn't work either:

Code:
Sub SummaryReportMacro()

'If Sheets("Record 2").Name Then Sheets("Record 2").Name = Range(B1).Value

End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
How about
Code:
Sub gingerbreadgirl()
   Dim ShtName As String
   
   ShtName = "Record 2"
   If Evaluate("Isref('" & ShtName & "'!A1)") Then
      With Sheets(ShtName)
         .Name = .Range("B1").Value
      End With
   End If
End Sub
 
Upvote 0
Hi Fluff,

This is fantastic! I was really spinning my wheels with this. Can you just give me a very brief description of how it works, just so I understand a little bit better? Especially the if Evaluate part, I've never seen anything like that before and I'm trying to learn as much as I can. :rolleyes:

Best,
Gingerbreadgrl
 
Upvote 0
The Evaluate is calculating a worksheet formula, so if you put
=ISREF('Record 2'!A1)
into a cell it returns TRUE (assuming you have a sheet called Record 2)
So it's just checking if the sheet exists & if it does it will then rename it.

HTH
 
Upvote 0
Thanks Fluff! That makes a lot of sense now. I know that this is a slightly different, but now that I have the sheet name renamed to the value in B1 can I reference that name to save it? The sheet number that was renamed is sheet number 11. I just need to know how to reference the sheet name in the file pathway. I'm so close in making this code work:

Code:
ShtName = "Record 2"
   If Evaluate("Isref('" & ShtName & "'!A1)") Then
      With Sheets(ShtName)
         .Name = .Range("B1").Value
      End With
      
      Sheets(11).Copy
      With activeworkook
      .SaveAs Filename:="G:\My Drive\Gingerbreadgrl\[COLOR=#ff0000]Sheets(11).Name[/COLOR]", FileFormat:=xlOpenXMLWorkbook
      .Close savechanges:=False
      End With
      
   End If
 
Upvote 0
Try
Code:
   Dim ShtName As String
   
   ShtName = "Record 2"
   If Evaluate("Isref('" & ShtName & "'!A1)") Then
      With Sheets(ShtName)
         .Name = .Range("B1").Value
         .Copy
      End With
      With ActiveWorkbook
         .SaveAs "G:\My Drive\Gingerbreadgrl\" & Sheets(1).Name, 52
         .Close savechanges:=False
      End With
   End If
 
Upvote 0
Hi Fluff,

This works great for the first sheet that is renamed, but I have matching code below and it errors out on the second file pathway.

Code:
   ShtName = "Record 2"
   If Evaluate("Isref('" & ShtName & "'!A1)") Then
      With Sheets(ShtName)
         .Name = .Range("B1").Value
         .Copy
      End With
      With ActiveWorkbook
         .SaveAs "G:\My Drive\Gingerbreadgrl\" & Sheets(1).Name, 52
         .Close savechanges:=False
      End With
   End If

   ShtName = "Record 3"
   If Evaluate("Isref('" & ShtName & "'!A1)") Then
      With Sheets(ShtName)
         .Name = .Range("B1").Value
         .Copy
      End With
      With ActiveWorkbook
[COLOR=#ff0000]         .SaveAs "G:\My Drive\Gingerbreadgrl\" & Sheets(1).Name, 52[/COLOR]
         .Close savechanges:=False
      End With
   End If
 
Upvote 0
Hmm I just tried it again and it worked just find I realized that I had an space between the end with and the with, would that have caused the error?

Code:
ShtName = "Record 3"
   If Evaluate("Isref('" & ShtName & "'!A1)") Then
      With Sheets(ShtName)
         .Name = .Range("B1").Value
         .Copy
      [COLOR=#ff0000]End With[/COLOR]


[COLOR=#ff0000]      With ActiveWorkbook[/COLOR]
         .SaveAs "G:\My Drive\Gingerbreadgrl\" & Sheets(1).Name, 52
         .Close savechanges:=False
      End With
   End If
 
Upvote 0
Having extra blank lines shouldn't cause any problems. How many of these do you have to do?
 
Upvote 0
Nope it didn't just tested with and without the space, must have been a fluke! Anywho, what does the 52 mean after the Sheets(1).Name?

Thanks so much!! I really appreciate your expertise!

Best,
Gingerbreadgrl
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,242
Members
448,951
Latest member
jennlynn

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