Copy and Rename Sheet based on cell data

Revanancer

New Member
Joined
Oct 26, 2024
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I have data in A7-A26 and B7-B27. Column A is the new name for the sheet and column B is the sheet to copy. I would like to make a Macro that looks at the column B range, if there is the name of a sheet it makes a copy of it and renames it to the name in column A.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hi and welcome to MrExcel

In this line fit with the name of sheet with sheet names
Set sh1 = Sheets("Sheet1")


VBA Code:
Sub copysheets()
  Dim sh1 As Worksheet
  Dim c As Range
  
  Set sh1 = Sheets("Sheet1")    'fit with the name of sheet with sheet names
  
  For Each c In sh1.Range("B7:B27")
    If c.Value <> "" And c.Offset(0, -1).Value <> "" Then
      If Evaluate("ISREF('" & c.Value & "'!A1)") Then
        If Not Evaluate("ISREF('" & c.Offset(0, -1).Value & "'!A1)") Then
          Sheets(c.Value).Copy after:=Sheets(Sheets.Count)
          ActiveSheet.Name = c.Offset(0, -1).Value
        End If
      End If
    End If
  Next
End Sub

🤗
 
Upvote 0
Thank you, That Works perfectly, and much simpler than what I was trying to do.

One more thing, I would like to have the sheets hidden and only show the new copied sheets. Is this possible?
 
Upvote 0
Try:

VBA Code:
Sub copysheets()
  Dim sh1 As Worksheet
  Dim c As Range
  
  Set sh1 = Sheets("Sheet1")    'fit with the name of sheet with sheet names
  
  For Each c In sh1.Range("B7:B27")
    If c.Value <> "" And c.Offset(0, -1).Value <> "" Then
      If Evaluate("ISREF('" & c.Value & "'!A1)") Then
        If Not Evaluate("ISREF('" & c.Offset(0, -1).Value & "'!A1)") Then
          Sheets(c.Value).Copy after:=Sheets(Sheets.Count)
          Sheets(Sheets.Count).Visible = True
          Sheets(Sheets.Count).Name = c.Offset(0, -1).Value
        End If
      End If
    End If
  Next
End Sub
 
Upvote 0
I have run into a small bug. The first sheet that is copied will remain hidden and the last hidden sheet will unhide and have its name changed.
so if the hidden sheets are in brackets it will look like this

Before Macro
S1 (S2, S3, S4, S5)

After Maco CS=Copied Sheet
S1 (S2(2), S2, S3, S4) CS1, CS2, CS3
SC1 was S5 and was renamed
 
Upvote 0
Try:

VBA Code:
ub copysheets()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim c As Range
  Dim i As Long, n As Long
  Application.ScreenUpdating = True

  Set sh1 = Sheets("Sheet1")    'fit with the name of sheet with sheet names
  
  n = Sheets.Count
  ReDim b(1 To n, 1 To 2)
  For i = 1 To n
    b(i, 1) = Sheets(i).Name
    b(i, 2) = Sheets(i).Visible
    Sheets(i).Visible = -1
  Next
  
  For Each c In sh1.Range("B7:B27")
    If c.Value <> "" And c.Offset(0, -1).Value <> "" Then
      If Evaluate("ISREF('" & c.Value & "'!A1)") Then
        If Not Evaluate("ISREF('" & c.Offset(0, -1).Value & "'!A1)") Then
          Sheets(c.Value).Copy after:=Sheets(Sheets.Count)
          ActiveSheet.Name = c.Offset(0, -1).Value
        End If
      End If
    End If
  Next

  For i = 1 To n
    Sheets(b(i, 1)).Visible = b(i, 2)
  Next
  
  Application.ScreenUpdating = True
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,223,110
Messages
6,170,157
Members
452,305
Latest member
chenhi131

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