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

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.
The code works happily for me.
Run this
Code:
Sub AutoAddSheet()
   Application.ScreenUpdating = False
   Dim LastRow As Long, MyCell As Range
   Dim Msg As String
   LastRow = Sheets("Info Entry").Cells.find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
   For Each MyCell In Sheets("Info Entry").Range("N26:N" & LastRow)
      If Not Evaluate("isref('" & MyCell.Value & "'!A1)") Then
         Msg = Msg & vbLf & MyCell.Value
      End If
   Next MyCell
   Worksheets("Info Entry").Activate
   Application.ScreenUpdating = True
   If Not Msg = "" Then MsgBox Msg
End Sub
Do you get a message box appear at the end?
 
Upvote 0
This still doesn't work. Any ideas from anyone?

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
[code]

Thanks,
 
Upvote 0
Have you tried the code I supplied in post#12?
If so did you get a message box appear?
 
Upvote 0
When I tried this macro on a dummy workbook, I believe it worked as you requested unless I misinterpreted what you want to do. 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. Include a detailed explanation of what you would like to do using a few examples from your data and referring to specific cells, rows, columns and worksheets. If the workbook contains confidential information, you could replace it with generic data.
Code:
Sub AutoAddSheet()
    Application.ScreenUpdating = False
    Dim LastRow As Long, MyCell As Range, ws As Worksheet
    LastRow = Sheets("Info Entry").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each ws In Sheets
        ws.Visible = True
    Next ws
    For Each MyCell In Sheets("Info Entry").Range("N2:N" & LastRow)
        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("Info Entry").Activate
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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