VBA Code Created In Excel 2016 Running Slowly

VBNewbiwe83

New Member
Joined
Aug 29, 2018
Messages
11
Hi Guys,

I m new to VBA ode and created a tool where I have code linked to a active x control button that clears certain cells in my xslm workbook.

The problem I am having is the tool is running slowly once the button is clicked.

Code:
Private Sub CommandButton13_Click()
    If MsgBox("Do you want to clear tool ready for a new call?", vbYesNo + vbQuestion, "***WARNING***") = vbYes Then
    ActiveSheet.Unprotect "Experience"
    Range("C18").ClearContents
    Range("C20").ClearContents
    Range("C24").ClearContents
    Range("C26:C27").ClearContents
    Range("C33").ClearContents
    Range("C35").ClearContents
    Range("C37").ClearContents
    Range("C43").ClearContents
    Range("C45").ClearContents
    Range("C46").ClearContents
    Range("C48").ClearContents
    Range("C54").ClearContents
    Range("C56:C57").ClearContents
    Range("C59").ClearContents
    Range("C61").ClearContents
    Range("C63").ClearContents
    Range("C69").ClearContents
    Range("C75:C76").ClearContents
    Range("C78:C80").ClearContents
    Range("C82:C83").ClearContents
    Range("C87:C89").ClearContents
    Range("C92").ClearContents
    Range("C12").Select
    Selection.Copy
    ActiveSheet.Protect "Experience", True, True
        End If
End Sub

I also use another code to autofit row based on vlookup responses so not sure which one may be causing the problem:

Code:
Private Sub Worksheet_Calculate()
    ActiveSheet.Unprotect "Experience"
    Range("A19").Rows.AutoFit
    Range("A21").Rows.AutoFit
    Range("A25").Rows.AutoFit
    Range("A28").Rows.AutoFit
    Range("A34").Rows.AutoFit
    Range("A36").Rows.AutoFit
    Range("A38").Rows.AutoFit
    Range("A44").Rows.AutoFit
    Range("A47").Rows.AutoFit
    Range("A49").Rows.AutoFit
    Range("A55").Rows.AutoFit
    Range("A58").Rows.AutoFit
    Range("A60").Rows.AutoFit
    Range("A62").Rows.AutoFit
    Range("A64").Rows.AutoFit
    Range("A70").Rows.AutoFit
    Range("A77").Rows.AutoFit
    Range("A81").Rows.AutoFit
    Range("A84").Rows.AutoFit
    Range("A92").Rows.AutoFit
    ActiveSheet.Protect "Experience", True, True
End Sub

Thanks for any help
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hello, if code is running slow, then you may have code in worksheet_change event or workbook_change event.

If you disable code while you do it it should help. Also you can cut the code down as below: I've included error handling as is essential when disabling events.

Code:
Private Sub CommandButton13_Click()
    Dim sAddress As String
    
    On Error GoTo errHandle
    


    
    If MsgBox("Do you want to clear tool ready for a new call?", vbYesNo + vbQuestion, "***WARNING***") = vbYes Then
        ActiveSheet.Unprotect "Experience"
        
        
        sAddress = "C18,C20,C24,C26:C27,C33,C35,C37,C43,C45:c46,C48,C54,C56:C57,C59,C61,C63,C69,C75:C76,C78:C80," & _
        "C82:C83,C87:C89,C92"
        
        'diasable change events
        Application.EnableEvents = False
        
        Range(sAddress).ClearContents
        
        're-enable events
        Application.EnableEvents = True
        
        Range("C12").Select
        Selection.Copy
        
        ActiveSheet.Protect "Experience", True, True
    End If


Exit Sub
errHandle:
    'if there is an error ensure to re-enable events
    MsgBox Err.Description
    Application.EnableEvents = True
End Sub

Please note that I've only reduced the code for the first example you gave. Same can be applied to 2nd example.
 
Last edited:
Upvote 0
Hi,

Sorry to be a pain but I am getting this error "Method 'range' of object'_worksheet Failed. What am I doing wrong?

Code:
Private Sub Worksheet_Calculate()
Dim sAddress As String
On Error GoTo errHandle
    
    ActiveSheet.Unprotect "Experience"
    sAddress = "A19, A21, A25, A28, A34, A36, A38, A44, A47, A49, A55, A58, A60, A62, A64, A70, A77, A81, A84, A92,"
'disable change events
Application.EnableEvents = False
Range(sAddress).Rows.AutoFit
're-enable events
Application.EnableEvents = True
    ActiveSheet.Protect "Experience", True, True
errHandle:
'if there is an error ensure to re-enable events
MsgBox Err.Description
Application.EnableEvents = True
End Sub
[code/]

Thanks
 
Upvote 0
Thanks for your reply, I now get "object doesn't support this property or method"

Code:
Private Sub Worksheet_Calculate()
 Dim sAddress As String
 On Error GoTo errHandle
 ActiveSheet.Unprotect "Experience"
 sAddress = "A19, A21, A25, A28, A34, A36, A38, A44, A47, A49, A55, A58, A60, A62, A64, A70, A77, A81, A84, A92"
 'disable change events
 Application.EnableEvents = False
 Range(sAddress).EntireRows.AutoFit
 're-enable events
 Application.EnableEvents = True
 ActiveSheet.Protect "Experience", True, True
errHandle:
 'if there is an error ensure to re-enable events
 MsgBox Err.Description
 Application.EnableEvents = True
 End Sub

appreciate any help
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,173
Members
453,021
Latest member
Justyna P

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