VBA works on One Sheet I need it to work on All Sheets

Myproblem

Board Regular
Joined
May 24, 2010
Messages
198
I have VBA code under which find all errors on sheet, the Code is Working OK.
I need to modify the Same code to work on all Sheets in one workbook, OR to get list of all errors in all sheets, OR to get list of all errors in all sheets with another VBA code
any idea?

ps original code from ozgrid

Sub All_Errors()
Dim rCcells As Range, rFcells As Range
Dim rAcells As Range

'Set variable to all used cells
Set rAcells = ActiveSheet.UsedRange
On Error Resume Next 'In case of no numeric formula or constants.
'Set variable to all numeric constants
Set rCcells = rAcells.SpecialCells(xlCellTypeConstants, xlErrors)
'Set variable to all numeric formulas
Set rFcells = rAcells.SpecialCells(xlCellTypeFormulas, xlErrors)
'Determine which type of numeric data (formulas, constants or none)
If rCcells Is Nothing And rFcells Is Nothing Then
MsgBox "You Worksheet contains no numbers"
End
ElseIf rCcells Is Nothing Then
Set rAcells = rFcells 'formulas
ElseIf rFcells Is Nothing Then
Set rAcells = rCcells 'constants
Else
Set rAcells = Application.Union(rFcells, rCcells) 'Both
End If
On Error GoTo 0
rAcells.Select
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Myproblem,

Give the following a try:
Code:
Sub All_Errors()
    
    Dim ws As Worksheet
    Dim rngConst As Range
    Dim rngFrmla As Range
    
    On Error Resume Next
    For Each ws In ActiveWorkbook.Sheets
        
        Set rngConst = Nothing
        Set rngFrmla = Nothing
            
        Set rngConst = ws.UsedRange.SpecialCells(xlCellTypeConstants, xlErrors)
        Set rngFrmla = ws.UsedRange.SpecialCells(xlCellTypeFormulas, xlErrors)
        
        If rngConst Is Nothing And rngFrmla Is Nothing Then
            MsgBox ws.Name & " contains no numbers"
        ElseIf rngConst Is Nothing Then
            rngFrmla.Select
        ElseIf rngFrmla Is Nothing Then
            rngConst.Select
        Else
            Union(rngConst, rngFrmla).Select
        End If
    Next ws
    
End Sub



Hope that helps,
~tigeravatar
 
Upvote 0
Myproblem,

I ran it on a test workbook, and it ran successfuly for me for all sheets. The code selects error cells and gives a message for each worksheet that does not contain any errors. So when you go through each worksheet after the code has run, the cells with errors are selected. Would you prefer something more obvious, like highlighting error cells red?
 
Upvote 0
Myproblem,

I ran it on a test workbook, and it ran successfuly for me for all sheets. The code selects error cells and gives a message for each worksheet that does not contain any errors. So when you go through each worksheet after the code has run, the cells with errors are selected. Would you prefer something more obvious, like highlighting error cells red?

dear tiger,
you are right, it does everything you said i would prefer some kind of list on new sheet with all errors, and their cell address:rolleyes:
 
Upvote 0
Myproblem,

Updated code to create a new worksheet, and list all errors and their cell addresses in the new worksheet:
Code:
Sub All_Errors()
    
    Static wsNew As Worksheet: Set wsNew = Sheets.Add(After:=Sheets(Sheets.Count))
    
    Dim ws As Worksheet
    Dim arrRslt() As Variant
    Dim arrIndex As Long
    Dim rngDest As Range
    Dim rngConst As Range, rngFrmla As Range
    Dim rngError As Range, ErrorCell As Range
    
    ReDim arrRslt(1 To 2, 1 To 2): arrRslt(1, 1) = "Sheet: "
                                   arrRslt(1, 2) = "Error"
                                   arrRslt(2, 2) = "Cell"
    
    On Error Resume Next
    For Each ws In ActiveWorkbook.Sheets
        If ws.Name <> wsNew.Name Then
            arrRslt(2, 1) = ws.Name
            arrIndex = 2
            Set rngConst = Nothing
            Set rngFrmla = Nothing
            Set rngError = Nothing
            Set rngDest = wsNew.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 2)
            
            Set rngConst = ws.UsedRange.SpecialCells(xlCellTypeConstants, xlErrors)
            Set rngFrmla = ws.UsedRange.SpecialCells(xlCellTypeFormulas, xlErrors)
            
            If rngConst Is Nothing And rngFrmla Is Nothing Then
                rngDest.Resize(1, 2).Value = Array(ws.Name, "No error cells found")
            ElseIf rngConst Is Nothing Then
                Set rngError = rngFrmla
            ElseIf rngFrmla Is Nothing Then
                Set rngError = rngConst
            Else
                Set rngError = Union(rngConst, rngFrmla)
            End If
            
            If rngError Is Nothing Then
                ReDim Preserve arrRslt(1 To 2, 1 To 3)
                arrRslt(1, 3) = "No Errors"
                arrRslt(2, 3) = "No error cells found"
            Else
                ReDim Preserve arrRslt(1 To 2, 1 To rngError.Cells.Count + 2)
                For Each ErrorCell In rngError
                    arrIndex = arrIndex + 1
                    arrRslt(1, arrIndex) = ErrorCell.Text
                    arrRslt(2, arrIndex) = ErrorCell.Address
                Next ErrorCell
            End If
            rngDest.Resize(UBound(arrRslt, 2), 2).Value = WorksheetFunction.Transpose(arrRslt)
        End If
    Next ws
    
    wsNew.[A:B].Delete
    wsNew.UsedRange.EntireColumn.AutoFit
    
End Sub



Hope that helps,
~tigeravatar
 
Upvote 0
man, you are my man
it rocks, thanks tiger, it is what i mean to get:biggrin:

wish you nice day or succesfull night
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,704
Members
452,938
Latest member
babeneker

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