ActiveSheet.UsedRange missing cells

orangehenry

New Member
Joined
Oct 26, 2015
Messages
7
Hi,
I have a vba script that is clearing the contents of all cells that meet a certain formatting condition (ColourIndex). It does this by cycling through all worksheets and looking at the ActiveSheet.UsedRange
However, I am finding that this is missing random cells in my worksheets. I understand that UsedRange can be a bit buggy but I'm unclear how to integrate some of the workarounds I've seen on the forum into my script.

Can anyone suggest the fix so that my script won't miss cells in my workbook?

SQL:
Private Sub CommandButton1_Click()
'Updateby Extendoffice
    Dim rngCl As range
    Dim xRows As Long
    Dim xCol As Long
    Dim colorLg As Long
    Dim ws As Worksheet, a As range
        
    colorLg = 40
    Application.ScreenUpdating = False
    For Each ws In ActiveWorkbook.Worksheets
    ws.Activate
    With ActiveSheet.UsedRange
        For xRows = .Rows.Count To 1 Step -1
            For xCol = 1 To .Columns.Count
                           If .Cells(xRows, xCol).Interior.ColorIndex = colorLg Then
                    .Cells(xRows, xCol).ClearContents
                    Exit For
                End If
            Next xCol
        Next xRows
    End With
    Next
    Sheets("Detailed Stock Schedule").range("A4:C400").ClearContents
    Sheets("Detailed Stock Schedule").range("E4:J400").ClearContents
    Sheets("Detailed Stock Schedule").range("L4:O400").ClearContents
    Application.ScreenUpdating = True
    Unload UserForm2
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Forum statistics

Threads
1,214,795
Messages
6,121,624
Members
449,041
Latest member
Postman24

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