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!
 
Glad we could help & thanks for the feedback.

On your test file you had a hidden sheet called Form 5, which was causing the initial problems and may also be the reason that it appeared to be missing some sheets.
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
For what it's worth:
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)
        If MyCell <> "" Then
            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
        End If
    Next MyCell
    Worksheets("Master").Activate
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
@mumps
In the OPs file the last sheet was hidden, therefore even though you have
Code:
Sheets(MyCell.Value).Copy After:=Sheets(Sheets.Count)
It will actually put the new sheet as the penultimate sheet not the last sheet.
 
Upvote 0
@Fluff: Thank you for pointing that out. I hadn't seen your Post #11 . I'll have to be a little more diligent in the future. :)
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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