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
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,562
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Glad we could help.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,144,389
Messages
5,724,073
Members
422,534
Latest member
ThiccNugg

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
Top