Optimising code to make it run faster by eliminating sheet access

Jimbola

Board Regular
Joined
Jun 23, 2010
Messages
60
Hi all,

I have some code that generates essentially a table of contents;

Code:
    lnRow = 6
    Set wbBook = ActiveWorkbook
    'Iterate through the worksheets in the workbook and create
    'sheetnames, add hyperlink and count & write the running number
    'of pages to be printed for each sheet on the TOC sheet.
    For Each wsSheet In wbBook.Worksheets
        If wsSheet.Name <> wsActive.Name And wsSheet.Name <> "New Invoices" And wsSheet.Name <> "Template" And wsSheet.Name <> "Help" Then
            wsSheet.Activate
            With wsActive
                
                'Sheet name
                .Hyperlinks.Add .Cells(lnRow, 1), "", _
                SubAddress:="'" & wsSheet.Name & "'!A1", _
                TextToDisplay:=wsSheet.Name
                
                'Person ID
                .Hyperlinks.Add .Cells(lnRow, 2), "", _
                SubAddress:="'" & wsSheet.Name & "'!A1", _
                TextToDisplay:="=INDIRECT(""'"" & $A$" & lnRow & " & ""'!B4"")"
            
                'First Name
                .Hyperlinks.Add .Cells(lnRow, 3), "", _
                SubAddress:="'" & wsSheet.Name & "'!A1", _
                TextToDisplay:="=INDIRECT(""'"" & $A$" & lnRow & " & ""'!A1"")"
                
                'Surname Name
                .Hyperlinks.Add .Cells(lnRow, 4), "", _
                SubAddress:="'" & wsSheet.Name & "'!A1", _
                TextToDisplay:="=INDIRECT(""'"" & $A$" & lnRow & " & ""'!B1"")"
                
                'Provider
                .Hyperlinks.Add .Cells(lnRow, 5), "", _
                SubAddress:="'" & wsSheet.Name & "'!A1", _
                TextToDisplay:="=INDIRECT(""'"" & $A$" & lnRow & " & ""'!B8"")"
            
                'Purchase Order
                .Hyperlinks.Add .Cells(lnRow, 6), "", _
                SubAddress:="'" & wsSheet.Name & "'!A1", _
                TextToDisplay:="=INDIRECT(""'"" & $A$" & lnRow & " & ""'!B6"")"
                
                'Service Type
                .Hyperlinks.Add .Cells(lnRow, 7), "", _
                SubAddress:="'" & wsSheet.Name & "'!A1", _
                TextToDisplay:="=INDIRECT(""'"" & $A$" & lnRow & " & ""'!D8"")"
                
                'Service Dates
                .Hyperlinks.Add .Cells(lnRow, 8), "", _
                SubAddress:="'" & wsSheet.Name & "'!A1", _
                TextToDisplay:="=INDIRECT(""'"" & $A$" & lnRow & " & ""'!D6"")"
                
                'Budget
                .Hyperlinks.Add .Cells(lnRow, 9), "", _
                SubAddress:="'" & wsSheet.Name & "'!A1", _
                TextToDisplay:="=INDIRECT(""'"" & $A$" & lnRow & " & ""'!F8"")"
                
                'Paid to Date
                .Hyperlinks.Add .Cells(lnRow, 10), "", _
                SubAddress:="'" & wsSheet.Name & "'!A1", _
                TextToDisplay:="=sum(INDIRECT(""'"" & $A$" & lnRow & " & ""'!F12:F1000""))"
                
                'Balance Remaining
                .Hyperlinks.Add .Cells(lnRow, 11), "", _
                SubAddress:="'" & wsSheet.Name & "'!A1", _
                TextToDisplay:="=INDIRECT(""'"" & $A$" & lnRow & " & ""'!F8"")-sum(INDIRECT(""'"" & $A$" & lnRow & " & ""'!F12:F1000""))"
            
            End With
            
            lnRow = lnRow + 1
            
        End If
        
    Next wsSheet

But as the number of sheets increases the code starts to slow down, the main slowdown happens when each sheet is activated in the loop. Is there a way to make the code run faster by eliminating the need to have the sheets accessed or any other way.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

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