Copy + Rename from Hidden Sheet

rspalding

Active Member
Joined
Sep 4, 2009
Messages
295
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm using the code below to copy and rename worksheets. However i what the sheets that I copy from to be hidden. When I hide those sheets and try to copy i get an error. What code needs to be added?

Code:
Sub AutoAddSheet()
    Dim MyCell As Range, MyRange As Range
    Set MyRange = Sheets("Master").Range("B17")
    Set MyRange = Range(MyRange, MyRange.End(xlDown))


    For Each MyCell In MyRange
        Sheets(MyCell.Value).Copy After:=Sheets(Sheets.Count) 'Create a new worksheet as a copy of Sheet number
        Sheets(Sheets.Count).Name = MyCell.Offset(0, -1).Value 'Renames the new worksheets
    Next MyCell
    Worksheets("Master").Activate
End Sub [cpde]

Thanks for the help!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try:
Code:
Sub AutoAddSheet()
    Application.ScreenUpdating = False
    Dim LastRow As Long, MyCell As Range
    LastRow = Sheets("Master").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each MyCell In Sheets("Master").Range("B17:B" & LastRow)
        Sheets(MyCell.Value).Visible = True
        Sheets(MyCell.Value).Copy After:=Sheets(Sheets.Count) 'Create a new worksheet as a copy of Sheet number
        Sheets(Sheets.Count).Name = MyCell.Offset(0, -1).Value 'Renames the new worksheets
        Sheets(MyCell.Value).Visible = False
    Next MyCell
    Worksheets("Master").Activate
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Hi,

Got a "Subscript out of range". It did copy the sheets but didn't rename.

Thanks....
 
Upvote 0
Which line of code was highlighted when you clicked 'Debug'? Are there values in column A to be used as the sheet name?
 
Upvote 0
Yes there are values in column A. Does not give me the chance to Debug, just shows the error.

Thanks,
 
Upvote 0
I think that it would be easier to help and test possible solutions if I could work with your actual file which includes any macros you are currently using. Perhaps you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
Try
Code:
Sub AutoAddSheet()
    Application.ScreenUpdating = False
    Dim LastRow As Long, MyCell As Range
    LastRow = Sheets("Master").Cells.Find("*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each MyCell In Sheets("Master").Range("B17:B" & LastRow)
      If Not Evaluate("isref('" & MyCell.Offset(, -1).Value & "'!A1)") Then
        Sheets(MyCell.Value).Visible = True
        Sheets(MyCell.Value).Copy After:=Sheets(Sheets.Count) 'Create a new worksheet as a copy of Sheet number
        ActiveSheet.Name = MyCell.Offset(0, -1).Value 'Renames the new worksheets
        Sheets(MyCell.Value).Visible = False
      End If
    Next MyCell
    Worksheets("Master").Activate
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Fluff,

Your code worked but only copied down 3 rows. It skipped the first row, B17 and then /stopped at B21.

Thanks,
 
Upvote 0
Fluff,

My bad. it works just fine. I must have done something wrong.

THANK YOU!!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
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