Code lags for a few seconds before computing

DavideXL

New Member
Joined
Feb 24, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi there
I have created the code below finding bits and pieces from this forum. I have done coding before but I am relatively new to VBA. Code was working relatively fine until I have introduced this section.

VBA Code:
If .Range("Volt").Value = "DC" Then
            .Range("pf").Formula = "1"  
End If

What I want to achieve is to have a fixed value (1) when "DC" is selected, and manual entry the value when is not. Code delivers the output but looks like computations are overloaded as it takes around 5s for Excel to revive from the trauma of reading the code :). Any advice on why is this happening and how to improve the above section or the code in general. Any answers will be much appreciated. Cheers

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    
    With Sheets("Input & Results")
        
        '-----Change visibility of check boxes--------------'
        
        If .Range("CorTyp").Value = "Multicore" Then
            .CheckBoxes("CheckBoxSc").Visible = True
        Else
            .CheckBoxes("CheckBoxSc").Visible = False
        End If
        
        If (.Range("CorTyp").Value = "Multicore") Or (.Range("InsCon").Value = "Unenclosed - Spaced from surface") Or (.Range("Volt").Value = "DC") Then
            .CheckBoxes("CheckBoxSpa").Visible = False
            .CheckBoxes("CheckBoxSpa").Value = False
        Else
            .CheckBoxes("CheckBoxSpa").Visible = True
        End If
        
        If .Range("CorMat").Value = "Al" Then
            .CheckBoxes("CheckBoxTC").Visible = False
            .CheckBoxes("CheckBoxTC").Value = False
        Else
            .CheckBoxes("CheckBoxTC").Visible = True
            '.CheckBoxes("CheckBoxTC").Value = True
        End If
        
        '-------Assign a fix value to pf cell in when Voltage is DC-----------'
        
        [B]If .Range("Volt").Value = "DC" Then
            .Range("pf").Formula = "1"
        End If[/B]
        
        '-------Clear cells if dropdown values are changed-----------'
        
        If Not Application.Intersect(Target, Range("Volt")) Is Nothing Then
            If Target.Validation.Type = 3 Then
                Application.EnableEvents = False
                .Range("SupVol").ClearContents
                .Range("ConCon").ClearContents
            End If
        End If
        
        If Not Application.Intersect(Target, Range("CorTyp")) Is Nothing Then
            If Target.Validation.Type = 3 Then
                Application.EnableEvents = False
                .Range("InsCon").ClearContents
                .Range("ArrCir").ClearContents
                .Range("ConCon").ClearContents
            End If
        End If
        
        If Not Application.Intersect(Target, Range("InsCon")) Is Nothing Then
            If Target.Validation.Type = 3 Then
                Application.EnableEvents = False
                .Range("ArrCir").ClearContents
                .Range("NoC").ClearContents
            End If
        End If
        
        If Not Application.Intersect(Target, Range("ArrCir")) Is Nothing Then
            If Target.Validation.Type = 3 Then
                Application.EnableEvents = False
                .Range("NoC").ClearContents
            End If
        End If
        
exitHandler:
        Application.EnableEvents = True
        Exit Sub
End Sub        
    End With
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi, it's not clear in which sheet this code resides, but you probably want to disable events with Application.EnableEvents = False before you make that change to the sheet (like you have done elsewhere in the code) to prevent the change event firing itself.

If it were me, I'd consider disabling events only once before any potential sheet changes occur and re-enabling, like you have already done, in the exithandler.
 
Upvote 0
Hi, it's not clear in which sheet this code resides, but you probably want to disable events with Application.EnableEvents = False before you make that change to the sheet (like you have done elsewhere in the code) to prevent the change event firing itself.

If it were me, I'd consider disabling events only once before any potential sheet changes occur and re-enabling, like you have already done, in the exithandler.
Thanks @FormR, the code now works smoothly. This is what happens when u have a piece of code there and you don't even know what it was doing.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,688
Members
448,978
Latest member
rrauni

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