Any way of simplifying this code?

klarowe

Active Member
Joined
Mar 28, 2011
Messages
389
Ok so I lied before, I have one more item I need some assistance with. Everything is working properly, but on the index page that I have each index item has a checkbox (about 30 boxes in total) that determines the visibility of the corresponding sheet. If the box is checked, the sheet is visible; if not, then its not visible. Each checkbox is also set to run the index renumbering macro/application as well as the sequential sheet renumbering macro/application. Then there is also a bottom checkbox that is a select/deselect all option for the index.

The problem I am having is that since each checkbox is doing multiple tasks, it is taking quite a bit of time to complete. And when I use the select/deselect all check box it can take upwards of 30 seconds or more to finish all the operations.

Its not a huge deal, but if there is a way to simplify the code to help it run a little faster it would be much better.

Here are the codes :

Checkbox code (one for each of the 30 or so check boxes):
Code:
Private Sub CheckBox1_Click()
    Sheets("1st Stg Impeller").Visible = CheckBox1.Value
    Application.Run "IndexNumber"
    Application.Run "SequentiallyNumberVisiblePagesOnly"
End Sub

Sheet renumbering code:
Code:
Sub SequentiallyNumberVisiblePagesOnly()
    Dim lx As Long
    Dim lVisibleSheets As Long
    'Dim lVisibleCount As Long
   ' For lx = 1 To Worksheets.Count
       'If Worksheets(lx).Visible = True Then lVisibleSheets = lVisibleSheets + 1
   ' Next
    For lx = 1 To Worksheets.Count
        If Worksheets(lx).Visible = True Then lVisibleCount = lVisibleCount + 1
        With Worksheets(lx).PageSetup
            .RightFooter = lVisibleCount
        End With
    Next
End Sub

Index Renumbering Code:
Code:
Sub IndexNumber()
    Dim lx As Long
    Dim sIndex As String
    Dim sIndexStartRange As String
    Dim lVisibleCount As Long
 
    sIndex = "Index"
    sIndexStartRange = "A7"
    
    Worksheets(sIndex).Range("A7:A60").Cells.ClearContents
    With Worksheets(sIndex).Range("A7")
 
        For lx = 1 To Worksheets.Count
            Select Case Worksheets(lx).Name
            'uncomment next line to skip Index line
            'Case sIndex
                'do nothing
            Case Else
                If Worksheets(lx).Visible = True Then
                    lVisibleCount = lVisibleCount + 1
                    'uncomment next 2 lines to show only visible sheets/number in index
                    '.Offset(lVisibleCount, 0) = lVisibleCount
                    '.Offset(lVisibleCount, 1) = Worksheets(lX).Name
                End If
                'uncomment next 2 lines show all sheet names, only visible numbers
                If Worksheets(lx).Visible = True Then .Offset(lx - 1, 0) = lVisibleCount
                '.Offset(lx - 1, 1) = Worksheets(lx).Name '<-- prints sheet name
            End Select
        Next
    End With
End Sub

Any ideas?
And again, thanks to everyone who has helped provide me the above codes to get this project finished... you have all been awesome!!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Shouldn't you have the processing be that all the sheets are hidden/unhidden as per the checkboxes first, and then a single call to each of "IndexNumber" and "SequentiallyNumberVisiblePagesOnly" be done?
 
Upvote 0
I guess I'm not 100% sure what you mean. I do not want to add another button to renumber everything after the boxes are checked/unchecked so that is why I added the command to each and every box. That way as soon as a box is checked/unchecked, it will renumber the pages and the index.
 
Upvote 0

Forum statistics

Threads
1,224,542
Messages
6,179,424
Members
452,914
Latest member
echoix

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