Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel 2010 - This piece of code seems slow, can it be improved?

Status
Not open for further replies.

PWD

Technical User
Jul 12, 2002
823
GB
Code:
Dim PDLBV As String
Dim JRTBV As String
PDLBV = PersonalDetailsListBox.Value
JRTBV = JobRoleTextBox.Value

Application.ScreenUpdating = False

 EvidenceRowsCount = Range("Evidence").Rows.Count
 
        For Each wrksht In ThisWorkbook.Worksheets
            If wrksht.Index > 1 And wrksht.Index <> ThisWorkbook.Worksheets.Count _
            And wrksht.Index <> ThisWorkbook.Worksheets.Count - 1 Then

    Application.StatusBar = "Updating Worksheet " & wrksht.Name

                wrksht.Unprotect ("cpd")

                If wrksht.Index = 2 Then
                wrksht.Range("D3").Value = PDLBV ''Name
                wrksht.Range("D5").Value = JRTBV ''Job Role
                End If

If I just step through the code (F8) you can see it thinking about adding the values in each of D3 & D5. How should I be doing this differently?

Many thanks,
D€$
 
If it's due to recalculation of the workbook, try to switch off it:
[tt]Application.Calculation = xlCalculationManual
' your code
Application.Calculation = xlCalculationAutomatic[/tt]
However. I have a macro that does not like some code in other workbook with no event procedures, just a simple Sub... It executes very slowly when the other is opened.


combo
 
Code:
If wrksht.Index > 1 And wrksht.Index <> ThisWorkbook.Worksheets.Count _
And wrksht.Index <> ThisWorkbook.Worksheets.Count - 1 Then

can be reduced to
Code:
If wrksht.Index > 1 And wrksht.Index < ThisWorkbook.Worksheets.Count - 1 Then
 

Use the With...End With construct
Code:
    For Each wrksht In ThisWorkbook.Worksheets  '[b]
        With wrksht                             '[/b]
            If .Index > 1 And .Index < ThisWorkbook.Worksheets.Count - 1 Then
    
                Application.StatusBar = "Updating Worksheet " & .Name
    
                .Unprotect ("cpd")
    
                If .Index = 2 Then
                    .Range("D3").Value = PDLBV ''Name
                    .Range("D5").Value = JRTBV ''Job Role
                End If
            End If
        End With
    Next
 

Also to add to the calculation issue that combo suggested:

If you do have NECESSARY calculations on a sheet, meaning that the calculated values/ranges on the sheet are referenced by other cells in your code, then if you have used named ranges, you can calculate those ranges explicitly when appropriate. It means that you must have documented and understand how your workbook works.

But then, of course, you must recalculate the entire workbook before it is saved.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top