Quicker way to clear a range of cells?

niall91

New Member
Joined
Jul 21, 2020
Messages
45
Office Version
  1. 2019
Platform
  1. Windows
Hi guys

This code works but its as slow as a wet week. Could anyone help write this so it runs much quicker.
Thank you


VBA Code:
Sub OptimizeVBA(isOn As Boolean)
    Application.Calculation = IIf(isOn, xlCalculationManual, xlCalculationAutomatic)
    Application.EnableEvents = Not (isOn)
    Application.ScreenUpdating = Not (isOn) 
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 trow = Target.Row
 OptimizeVBA True
Dim emp As Range
Set emp = Cells(trow, 13)

If Target.Column = 13 Then
    If IsEmpty(emp) = True Then
        With Sheets("Frame Sets")
        .Range(.Cells(trow, 18), .Cells(trow, 37)) = ""
        End With
    End If
End If
 
 OptimizeVBA False
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I can't see any reason for your code being noticeably slow, this is how I would have done the same thing but I would expect performance to be almost identical. I would expect that any major performance issues are caused by formulas calculating when the code finishes running.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim trow As Long
If Target.Column = 13 Then
    trow = Target.Row
    OptimizeVBA True
    If IsEmpty(Cells(trow, 13)) Then
        With Sheets("Frame Sets")
            .Range(.Cells(trow, 18), .Cells(trow, 37)).ClearContents
        End With
    End If
    OptimizeVBA False
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,094
Latest member
teemeren

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