+ Reply to Thread
Results 1 to 7 of 7

Need help deleting sheets and moving existing sheets down in order

Hybrid View

  1. #1
    Registered User
    Join Date
    05-16-2004
    Posts
    22

    Need help deleting sheets and moving existing sheets down in order

    Hello,

    I have really coded myself into a corner and need help getting out. The first issue is that my spreadsheet is too large to post here, so I will post screenshots and code segments and anything else I can to help describe the issue.

    The MAIN issue is that once the spreadsheet is set up and in use, if a sheet has to be deleted it destroys the whole spreadsheet. To clarify, amongst other sheets are 25 sheets in a row that contain the data of 25 people being logged. It looks like this:

    Attachment 195916

    Everything works perfect until someone leaves your department or is fired. If you delete their sheet, it messes up how several of the other sheets display their data. Here are some images to see why:

    Attachment 195917

    Attachment 195918


    Here is some important info: I do have VBA already coded to HIDE sheets if there are less than 25 people being tracked. The code looks like this:

    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Address = "$I$4" Then HideRows4
    
    End Sub
    
    
    AND THEN
    
    
    Sub HideRows4()
    
        Sheet13.Visible = True
        Sheet14.Visible = True
        Sheet15.Visible = True
        Sheet16.Visible = True
        Sheet32.Visible = True
        Sheet17.Visible = True
        Sheet18.Visible = True
        Sheet19.Visible = True
        Sheet20.Visible = True
        Sheet23.Visible = True
        Sheet24.Visible = True
        Sheet33.Visible = True
        Sheet25.Visible = True
        Sheet26.Visible = True
        Sheet27.Visible = True
    
        With Sheets("GPS")
            .Rows("15:30").EntireRow.Hidden = False
    
            If .Range("I4").Value = 0 Then
                .Rows("15:30").EntireRow.Hidden = False
            Else
                .Rows(Range("I4").Value + 5 & ":30").EntireRow.Hidden = True
            End If
            
            Sheet13.Visible = .Range("I4").Value > 10
            Sheet14.Visible = .Range("I4").Value > 11
            Sheet15.Visible = .Range("I4").Value > 12
            Sheet16.Visible = .Range("I4").Value > 13
            Sheet32.Visible = .Range("I4").Value > 14
            Sheet17.Visible = .Range("I4").Value > 15
            Sheet18.Visible = .Range("I4").Value > 16
            Sheet19.Visible = .Range("I4").Value > 17
            Sheet20.Visible = .Range("I4").Value > 18
            Sheet23.Visible = .Range("I4").Value > 19
            Sheet24.Visible = .Range("I4").Value > 20
            Sheet33.Visible = .Range("I4").Value > 21
            Sheet25.Visible = .Range("I4").Value > 22
            Sheet26.Visible = .Range("I4").Value > 23
            Sheet27.Visible = .Range("I4").Value > 24
            Sheet27.Visible = .Range("I4").Value = 25
        End With
        
        With Sheets("Summary")
            .Rows("14:29").EntireRow.Hidden = False
    
            If Sheets("GPS").Range("I4").Value = 0 Then
                .Rows("14:29").EntireRow.Hidden = False
            Else
                .Rows(Sheets("GPS").Range("I4").Value + 4 & ":29").EntireRow.Hidden = True
            End If
            
            Sheet13.Visible = Sheets("GPS").Range("I4").Value > 10
            Sheet14.Visible = Sheets("GPS").Range("I4").Value > 11
            Sheet15.Visible = Sheets("GPS").Range("I4").Value > 12
            Sheet16.Visible = Sheets("GPS").Range("I4").Value > 13
            Sheet32.Visible = Sheets("GPS").Range("I4").Value > 14
            Sheet17.Visible = Sheets("GPS").Range("I4").Value > 15
            Sheet18.Visible = Sheets("GPS").Range("I4").Value > 16
            Sheet19.Visible = Sheets("GPS").Range("I4").Value > 17
            Sheet20.Visible = Sheets("GPS").Range("I4").Value > 18
            Sheet23.Visible = Sheets("GPS").Range("I4").Value > 19
            Sheet24.Visible = Sheets("GPS").Range("I4").Value > 20
            Sheet33.Visible = Sheets("GPS").Range("I4").Value > 21
            Sheet25.Visible = Sheets("GPS").Range("I4").Value > 22
            Sheet26.Visible = Sheets("GPS").Range("I4").Value > 23
            Sheet27.Visible = Sheets("GPS").Range("I4").Value > 24
            Sheet27.Visible = Sheets("GPS").Range("I4").Value = 25
        End With
        
        With Sheets("INFO")
            .Rows("12:27").EntireRow.Hidden = False
    
            If Sheets("GPS").Range("I4").Value = 0 Then
                .Rows("12:27").EntireRow.Hidden = False
            Else
                .Rows(Sheets("GPS").Range("I4").Value + 2 & ":27").EntireRow.Hidden = True
            End If
                    
            Sheet13.Visible = Sheets("GPS").Range("I4").Value > 10
            Sheet14.Visible = Sheets("GPS").Range("I4").Value > 11
            Sheet15.Visible = Sheets("GPS").Range("I4").Value > 12
            Sheet16.Visible = Sheets("GPS").Range("I4").Value > 13
            Sheet32.Visible = Sheets("GPS").Range("I4").Value > 14
            Sheet17.Visible = Sheets("GPS").Range("I4").Value > 15
            Sheet18.Visible = Sheets("GPS").Range("I4").Value > 16
            Sheet19.Visible = Sheets("GPS").Range("I4").Value > 17
            Sheet20.Visible = Sheets("GPS").Range("I4").Value > 18
            Sheet23.Visible = Sheets("GPS").Range("I4").Value > 19
            Sheet24.Visible = Sheets("GPS").Range("I4").Value > 20
            Sheet33.Visible = Sheets("GPS").Range("I4").Value > 21
            Sheet25.Visible = Sheets("GPS").Range("I4").Value > 22
            Sheet26.Visible = Sheets("GPS").Range("I4").Value > 23
            Sheet27.Visible = Sheets("GPS").Range("I4").Value > 24
            Sheet27.Visible = Sheets("GPS").Range("I4").Value = 25
        End With
            
        Calculate
    
    End Sub
    The end result is that when you initially set up the spreadsheet with 25 or 20 or 18 people, whatever number, you simply need to put that number of people in the box on the first page and the spreadsheet only shows information and calculates for that number of people.

    Now- once you’ve been using this spreadsheet for a while and someone you have been logging moves to another department or is separated- THERE IS NO EASY WAY TO GET RID OF THEIR SHEET! If you hide the sheet manually, the data still shows up on all the other summary sheets. If you delete the sheet is throws errors into everything.What does work is as follows:

    The only that works is a laborious process involving renaming every sheet after the one you want to remove, and then overwriting each sheet with the sheet after its data and renaming it accordingly. That sounds very confusing but I will try to explain better. If you have the sheets named: A, B, C, D, E, and F but no longer need to log for sheet B- the current process is to rename sheet C to C., D to D., E to E., and F to F. THEN copy the data from C over Sheet B (the one no longer needed) and rename sheet B to C (which is why you had to first rename C to C.). Then you have to copy sheet D and paste it over the data in sheet C and rename sheet C to D. Then you have to copy the data from sheet E over the… etc. Good grief! It takes forever.

    So what happens at the end now that you have a sheet you were using but now don’t need due to the domino effect. That non needed sheet at the end is named with number. If you had 25 people (a full spreadsheet) and now have one not needed sheet at the end, that sheet would become named 25 and be hidden when the cell on the first page is set to show only 24 people now. If you start with 22 people sheets and are getting rid of one, the now not needed sheet at the end would need to be named 22 and would be hidden behind the sheets already hidden and named 23, 24, and 25.

    Here is I think what I need- but perhaps there is a much better idea out there:

    I need some VBA Code (which this is above my skill level) that if a user enters the current name of the sheet to be removed, the VBA goes through the above process of renaming all the sheets after the one entered, copies the data from the sheet after to the sheet before for every sheet after the entered one, and then renames all the sheets to correctly correspond with their new data, and lastly for the new not needed sheet at the end name it whatever number from 25 it now actually is.

    The above paragraph is really what I’m after. I am happy to send any further detail, code, screenshots, and whatever I can to help out with this. The spreadsheet is too large to post on this site but I do have a dropbox account I think I can put it on if that is allowed.

    I could truly use your help. Thank you for reading this ridiculously long post and your much valued time.

    —Seth

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,761

    Re: Need help deleting sheets and moving existing sheets down in order

    I'm trying to understand but not getting it very well. First off, your attached images give me the following error message:
    Quote Originally Posted by Excel Forum
    Invalid Attachment specified. If you followed a valid link, please notify the administrator
    Second, I think a fundamental part of your problem is that you are writing code for a workbook with worksheets that can be added and deleted, but you are hard-coding the names of those worksheets. I don't see why you have to rename all the sheets and moves all that data around just because you add a sheet. I suspect it is because in your code you refer to Sheet13, for example. And if you insert a sheet, Sheet13 goes from being the 13th sheet to the 14th sheet. That reference to a worksheet is call the code name, and it doesn't change. I think you need to use indexing, like Worksheets(2). For example
    ' change this code
            Sheet13.Visible = Sheets("GPS").Range("I4").Value > 10
            Sheet14.Visible = Sheets("GPS").Range("I4").Value > 11
            Sheet15.Visible = Sheets("GPS").Range("I4").Value > 12
            Sheet16.Visible = Sheets("GPS").Range("I4").Value > 13
            Sheet32.Visible = Sheets("GPS").Range("I4").Value > 14
            Sheet17.Visible = Sheets("GPS").Range("I4").Value > 15
            Sheet18.Visible = Sheets("GPS").Range("I4").Value > 16
            Sheet19.Visible = Sheets("GPS").Range("I4").Value > 17
            Sheet20.Visible = Sheets("GPS").Range("I4").Value > 18
            Sheet23.Visible = Sheets("GPS").Range("I4").Value > 19
            Sheet24.Visible = Sheets("GPS").Range("I4").Value > 20
            Sheet33.Visible = Sheets("GPS").Range("I4").Value > 21
            Sheet25.Visible = Sheets("GPS").Range("I4").Value > 22
            Sheet26.Visible = Sheets("GPS").Range("I4").Value > 23
            Sheet27.Visible = Sheets("GPS").Range("I4").Value > 24
            Sheet27.Visible = Sheets("GPS").Range("I4").Value = 25
    
    ' to this code
       Dim i As Long
       For i = 2 To Worksheets.Count
          Worksheets(i).Visible = Sheets("GPS").Range("I4").Value > i + 8
       Next i
    I'm not sure if this example does quite what you're trying to do here but it illustrates the concept of using indexes instead of the code name for the sheet.

    Ultimately I need to understand you data layout and the relationship of data in one sheet to data in any other sheet. When you remove a person, you should not be hiding the sheet and jumping through hoops to move the data around. You should delete the sheet (if you don't need their data anymore!) and remove their data from the summary sheets.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    05-16-2004
    Posts
    22

    Re: Need help deleting sheets and moving existing sheets down in order

    Hello 6StringJazzer,

    Here is a link to a sample file from dropbox.

    http://dl.dropbox.com/u/64034102/Tra...le%20File.xlsm

    Hopefully that will help explain.

    Unfortunately I need to have all 25 sheets available to be shown and named at all times because the general users of this spreadsheet would not be able to add the sheet and all the links that go to it. Each sheet has many formulas on it that are referred to by other sheets in the workbook. Because of this, actually deleting and adding sheets becomes an issue, and instead they need to remain in order and just be renamed.

    I believe I am using the indexes you referenced, however please check out the sample workbook I posted and see if I am making any sense. The code is on the first sheet.

    Thank you for your help!

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,761

    Re: Need help deleting sheets and moving existing sheets down in order

    I just cannot figure out what your code is doing without a lot more study.

    1. I've modified your code in Module1; your code worked but did not use best practices.
    2. One of your Subs does not compile
    3. I've put comments in your code in GPS with minor changes. It will not be hard to find my comments because there were no comments to start with. This is another issue; I recommend using comments even if they just as reminders to yourself. You'll read this code in 6 months and won't remember what you were trying to do.
    4. I also recommend you go to Tools, Options, and check Require variable declarations.


    Your file is too big for me to post back but here is the code.

    Module1
    Option Explicit
    
    ' Clear all checkboxes in all worksheets
    Sub CodeForEachSheet()
    
       Dim ws As Worksheet
       
       Application.ScreenUpdating = False
       For Each ws In Worksheets
          ClrChk2 ws
       Next ws
       
       Worksheets(1).Activate
       Application.ScreenUpdating = True
       Calculate
       
    End Sub
                        
    ' Clear all checkboxes in worksheet ws
    Sub ClrChk2(ws As Worksheet)
    
       Dim myBox As CheckBox
       For Each myBox In ws.CheckBoxes
           myBox.Value = False
       Next myBox
        
    End Sub
    GPS, only the first two Subs. The comments and techniques shown here apply to the rest of your code in this module as well.
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
       ' You are always doing this if the number of advisors changes
       ' but it is not at all clear why
       If Target.Address = "$I$4" Then HideRows4
    
    End Sub
    
    
    Sub HideRows3()
    
       ' I cannot see a pattern as to which sheets you are choosing to show here
       ' 13-20, 23-27, 32-33
       ' There is almost certainly a more robust way to do this if you could
       ' define the logic behind why you need to show these
        Sheet13.Visible = True
        Sheet14.Visible = True
        Sheet15.Visible = True
        Sheet16.Visible = True
        Sheet32.Visible = True
        Sheet17.Visible = True
        Sheet18.Visible = True
        Sheet19.Visible = True
        Sheet20.Visible = True
        Sheet23.Visible = True
        Sheet24.Visible = True
        Sheet33.Visible = True
        Sheet25.Visible = True
        Sheet26.Visible = True
        Sheet27.Visible = True
    
        'With Sheets("GPS") ' this code is in GPS so you don't need this With. References
                            ' to worksheet attributes default to the sheet that the code is in.
                            ' Also omit the leading dot.
            Rows("15:29").EntireRow.Hidden = False ' why are you starting on row 15?
    
            If Range("I4").Value = 0 Then
                Rows("15:29").EntireRow.Hidden = False ' you already did this in the previous line of code
                                                       ' so this line doesn't change anything
            Else
                Rows(Range("I4").Value + 5 & ":29").EntireRow.Hidden = True
            End If
    
             ' I4 is the number of advisors so I can only guess that you are trying to
             ' hide all the sheets that are above the number of advisors, implying that
             ' there is an order to the sheets.
    '        Sheet13.Visible = Range("I4").Value > 10
    '        Sheet14.Visible = Range("I4").Value > 11
    '        Sheet15.Visible = Range("I4").Value > 12
    '        Sheet16.Visible = Range("I4").Value > 13
    '        Sheet32.Visible = Range("I4").Value > 14
    '        Sheet17.Visible = Range("I4").Value > 15
    '        Sheet18.Visible = Range("I4").Value > 16
    '        Sheet19.Visible = Range("I4").Value > 17
    '        Sheet20.Visible = Range("I4").Value > 18
    '        Sheet23.Visible = Range("I4").Value > 19
    '        Sheet24.Visible = Range("I4").Value > 20
    '        Sheet33.Visible = Range("I4").Value > 21
    '        Sheet25.Visible = Range("I4").Value > 22
    '        Sheet26.Visible = Range("I4").Value > 23
    '        Sheet27.Visible = Range("I4").Value > 24
    
            ' Because the names in column A are the same as the sheet names, I would replace the above code with the following code BUT only
            ' if that naming convention is deliberate and dependable!
            ' This is what I meant by indexing the worksheets. The code below links the worksheets to the corresponding rows
            ' on GPS, so that it is insulated from changes in what sheets are in what order, or their code names are.
            Dim c As Range
            For Each c In Range(Cells(5 + Range("I4").Value, 1), Cells(29, 1))
                Worksheets(c.Value).Visible = False
            Next c
        'End With
    
        Calculate ' this should happen automatically unless you've disabled calculation
    
    End Sub
    Also, in GPS this is preventing your code from compiling
    If Range("I4").Value >= 0 Then Rows("15:29").EntireRow.Hidden = False ' your If statement ends here
        Sheet13.Visible = True
        Sheet14.Visible = True
        Sheet15.Visible = True
        Sheet16.Visible = True
        Sheet32.Visible = True
        Sheet17.Visible = True
        Sheet18.Visible = True
        Sheet19.Visible = True
        Sheet20.Visible = True
        Sheet23.Visible = True
        Sheet24.Visible = True
        Sheet33.Visible = True
        Sheet25.Visible = True
        Sheet26.Visible = True
        Sheet27.Visible = True
    End If ' so this End If has no If to match against, causing a compile error
    VBA If is a little eccentric in that if you have

    If <condition> Then <statement>

    all on one line, there is no "End If". If you want the statement after the Then to be included in the code executed when the condition is true, simply move it to the next line.

  5. #5
    Registered User
    Join Date
    05-16-2004
    Posts
    22

    Re: Need help deleting sheets and moving existing sheets down in order

    Thank you for your help cleaning up the code. The reason that specific sheets are called is because those are the sheets with the employee data and in turn, are the only ones that need to be hidden or shown.

    Do you think it is possible to achieve what I originally posted about? What further information can I provide to help clarify the need?

    I could make a video showing the problem if you want?

    Any and all help is appreciated.

    Thank you.

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,761

    Re: Need help deleting sheets and moving existing sheets down in order

    I recommend you rethink your coding approach to make the code not care about what order the worksheets are in. You need to tie the worksheets to the list of employees. Your original post said
    I need some VBA Code (which this is above my skill level) that if a user enters the current name of the sheet to be removed, the VBA goes through the above process of renaming all the sheets after the one entered, copies the data from the sheet after to the sheet before for every sheet after the entered one, and then renames all the sheets to correctly correspond with their new data, and lastly for the new not needed sheet at the end name it whatever number from 25 it now actually is.
    and I am trying to persuade you that this is a very complicated way to manage your data and indicates a design problem. I've shown the general approach for dealing with sheets by indexing them to the employee name, not sure how I can help other than rewriting all the code from scratch.

  7. #7
    Registered User
    Join Date
    05-16-2004
    Posts
    22

    Re: Need help deleting sheets and moving existing sheets down in order

    Thank you for your help!

    :-)
    —Seth

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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