VB code to copy hidden sheets not working properly

rspalding

Active Member
Joined
Sep 4, 2009
Messages
295
Office Version
  1. 365
Platform
  1. Windows
I'm using the code below. If I only have one hidden sheet it works fine. but i have multiple hidden sheets abd at that point it no longer functions properly.

Please help. thank you,

Code:
Sub AutoAddSheet()
    Application.ScreenUpdating = False
    Dim LastRow As Long, MyCell As Range
    LastRow = Sheets("Info Entry").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each MyCell In Sheets("Info Entry").Range("N26:N" & 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, -13).Value 'Renames the new worksheets
        Sheets(MyCell.Value).Visible = False
    Next MyCell
    Worksheets("Info Entry").Activate
    Application.ScreenUpdating = True
End Sub
 
Last edited by a moderator:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
As you haven't told us what is "not working", this is a guess.
Code:
Sub AutoAddSheet()
   Application.ScreenUpdating = False
   Dim LastRow As Long, MyCell As Range
   LastRow = Sheets("Info Entry").Cells.find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
   For Each MyCell In Sheets("Info Entry").Range("N26:N" & LastRow)
      Sheets(MyCell.Value).Visible = True
      Sheets(MyCell.Value).Copy After:=Sheets(Sheets.Count) 'Create a new worksheet as a copy of Sheet number
      [COLOR=#ff0000]ActiveSheet.Name[/COLOR] = MyCell.Offset(0, -13).Value 'Renames the new worksheets
      Sheets(MyCell.Value).Visible = False
   Next MyCell
   Worksheets("Info Entry").Activate
   Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Hi Fluff,

I tried the correction that you sent. i was trying to copy 3 sheets, 2 the same and one different. It copied all 3 sheets (all correct copies). But the 3rd sheet was hidden, the first sheet was left un-hidden, it made a copy of the first sheet with (2) in the name. So

Thank you,
 
Upvote 0
Did you get any error messages?
And do any of the new sheet names already exist in the workbook?
 
Upvote 0
Hi,

No error message other than "Subscript out of range". And no names already exist.

Thanks,
 
Upvote 0
Which line of code gives that error?
 
Upvote 0
Hi,

No error message other than "Script out of range". No sheet names already exist.

Thanks,
 
Upvote 0
it's not like a regular code error where i can step into it. I say OK and then it copies the sheets but only if 1 hidden sheet.
 
Upvote 0
Subscript out of range suggest that a sheet name does not exist.
Check that the values in col N are exactly the same as the sheet names.
 
Upvote 0
Hi Fluff,

The problem appears to be the multiple hidden sheets. I will eventually have about 20 hidden sheets and copying up to about 30 sheets.

Thanks,
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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