clear contents of range if either of two cells meets a condition

surlybiker

New Member
Joined
Jul 1, 2011
Messages
14
In a sample spreadsheet, beginning at row 5, I need to clear the contents of the range E:K if either cell in column G or J is blank or contains a zero.

The following code works, but it is extremely slowly (about a second per row in the actual spreadsheet which contains up to 4000 rows of data across hundreds of columns):

Sub clear_ranges()
Dim ws As Worksheet
Dim x As Integer

Set ws = ThisWorkbook.Sheets("Sheet1")

For x = 5 To ws.Range("A" & Rows.Count).End(xlUp).Row
If (ws.Range("G" & x).Value = 0 Or ws.Range("G" & x).Value = "") And (ws.Range("J" & x).Value = 0 Or ws.Range("J" & x).Value = "") Then
ws.Range("E" & x & ":" & "K" & x).Clear
End If
Next x
End Sub

Is there a more efficient way to perform this task?

Thanks so much!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi

You should get some speed increases by modifying your procedure like so:

Code:
Sub clear_ranges()
    Dim ws As Worksheet
    Dim x As Long
    Dim lCalcMode As Long

    'Get the current calculation mode so that it can be set back later
    lCalcMode = Application.Calculation

    'These two lines are standard ways of improving macro performance
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual


    Set ws = ThisWorkbook.Sheets("Sheet1")

    For x = 5 To ws.Range("A" & Rows.Count).End(xlUp).Row
        If (ws.Range("G" & x).Value = 0 Or ws.Range("G" & x).Value = "") And (ws.Range("J" & x).Value = 0 Or ws.Range("J" & x).Value = "") Then
            ws.Range("E" & x & ":" & "K" & x).Clear
        End If
    Next x


    Application.ScreenUpdating = True
    Application.Calculation = lCalcMode


End Sub

Note that I changed the declaration of x from Integer to Long - an integer can only store numbers up to 32,767 which is less than the number of rows on a spreadsheet so you would end up with an overflow error if you have more than this many rows of data. Using a long is always the best way to deal with row numbers.

Please see if this code works any better for you.

HTH
DK
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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