+ Reply to Thread
Results 1 to 11 of 11

copying sheet issue caused by hidden sheets in fhile

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,677

    copying sheet issue caused by hidden sheets in fhile

    hi,

    i am having a problem. I have a file (see enclosed). THere are 5 sheets in the file.

    sheets labeled: tstsht1, tstsht2, tstsht3, main, tstsht5

    I have "hidden" tstsht3 and tstsht5.

    What i am trying to do is copy the "main" sheet and paste into a new sheet into the workbook. Once macro is done i should have 5 new sheets labeled "1_1", "1_2", "1_3", "1_4", and "1_5". This is indeed what i get when ALL sheets NOT hidden. However, when i hide "tstsht3" and "tstsht5" the macro does not work correctly. The new sheets created are "main (2)", "main (3)", "main (4)", "main (5)", "main (6)".

    Obviously, the problem has something to do with hiding the sheets "tstsht3" and "tstsht5" but i dont know why and i dont know how to fix the problem other than run the macro and then hide all the sheets at the end but i would rather not do this.

    Can someone help?
    Attached Files Attached Files

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,501

    Re: copying sheet issue caused by hidden sheets in fhile

    Hi welchs101

    This code will copy the main sheet and rename even if sheets are hidden...Edited from Tomach's code
    Option Explicit
    
    Sub CopySheet()
        Dim MySheetName As String
        Dim i As Integer
        MySheetName = "1_"
        For i = 1 To 5
        Sheets("Main").Copy After:=Sheets(Sheets.Count)
        ActiveSheet.Name = MySheetName & i
        Next i
    End Sub
    Last edited by Sintek; 04-17-2017 at 07:48 AM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,677

    Re: copying sheet issue caused by hidden sheets in fhile

    yours does work.........wonder why mine does not? any ideas on why mine does not work.........mine and yours are very similar.........

  4. #4
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,501

    Re: copying sheet issue caused by hidden sheets in fhile

    Not sure. Here's another way:
    Sub test()
    Dim shArray() As Variant
    Dim i As Long
    shArray = Array("1_1", "1_2", "1_3", "1_4", "1_5")
    For i = LBound(shArray) To UBound(shArray)
        Sheets("Main").Copy After:=Sheets(Sheets.Count)
        Sheets("Main (2)").Name = shArray(i)
    Next i
    End Sub

  5. #5
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,677

    Re: copying sheet issue caused by hidden sheets in fhile

    thanks again for the code. i will try it out.

    However, if someone can tell me why my code does not work i would appreciate it. I am missing some fundamental thing i think.

  6. #6
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,677

    Re: copying sheet issue caused by hidden sheets in fhile

    anyone know why my code does not work?

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: copying sheet issue caused by hidden sheets in fhile

    Hi,

    Your code doesn't work because Excel actually copies the sheet after the last visible sheet, which puts each new sheet before the hidden sheet tstsht5. It is that last sheet that gets renamed, not the new sheets.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  8. #8
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,677

    Re: copying sheet issue caused by hidden sheets in fhile

    hi,

    yes i see that the wrong sheet gets renamed but i dont know why.

    i thought i was specifying the correct sheet when i use the following:

    wrksht_main_sht.Copy After:=wb_macro.Sheets(initial_sht_count + LC1 - 1)
    wb_macro.Sheets(initial_sht_count + LC1).Name = shtarray(LC1)
    obviously this is not working which tells me referencing the "index" number of the sheet is not correct but why ....

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: copying sheet issue caused by hidden sheets in fhile

    The index is wrong for the reason I mentioned- the code copies the sheet to after the last visible sheet but that is before the last hidden one. This means that the index in your code is referring to the last sheet (the hidden one) and not the copy you just created, which is the one before the hidden one.

  10. #10
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,677

    Re: copying sheet issue caused by hidden sheets in fhile

    thanks. i understand what your saying thanks. just seems like this is "wrong" for excel vba to do........when you tell it to add a sheet after a hidden sheet the indexes should still work but i guess not.

    How would you handle this situation?

  11. #11
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: copying sheet issue caused by hidden sheets in fhile

    Using the approach that sintek mentioned in post #2.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Selecting a sheet from a range - issue with hidden sheets.
    By Barking_Mad in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-08-2013, 09:38 AM
  2. 3D Referencing
    By Chax in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 11-22-2012, 12:54 PM
  3. Copying formulas on hidden sheet
    By solnajeff in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-28-2011, 09:23 AM
  4. Issue with Sheet Hidden
    By mari_hitz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-27-2011, 04:09 PM
  5. copying hidden cells and paste to non hidden cells on same sheet
    By 2newguy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-11-2007, 02:39 PM
  6. [SOLVED] "With Sheets" Issue - macro on one sheet to affect hidden rows on other sheets
    By Punsterr in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-21-2006, 12:10 AM
  7. Copying Data to a hidden Sheet
    By tags in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-12-2005, 01:08 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1