Macro Error message showing even if no error?

NormChart55

New Member
Joined
Feb 22, 2022
Messages
42
Office Version
  1. 2016
Platform
  1. Windows
Hello All,

I am working at getting messages included in some templates to clean up any errors that may occur and help the user. I have this which is providing the message prompt, however it does it even if the SKU is available and there is no error? basically this is copying and pasting a SKU into a pivot and sometimes they may have already had it filtered incorrectly to where the SKU number is not in the pivot. I am getting the message box in all scenarios. I am sure I have something slightly off. Thoughts?

VBA Code:
Dim a As String
Dim pt As PivotTable

ThisWorkbook.Worksheets("Template").Activate

a = Worksheets("Template").Cells(668, 4).Value

On Error Resume Next

For Each pt In ActiveSheet.PivotTables
    With pt.PivotFields("SKU")
    .ClearAllFilters
    .CurrentPage = a
   
    End With
   
Next

MsgBox "SKU Not available in the Pivot. Check if you selected the SKU correctly and if any items are already filtered out of the pivot. If the device is new, make sure its included in the source data.", vbCritical
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
That is because you have no conditions on that MsgBox and that line of code will be hit EVERY time.
You don't want to ignore the error, you want to add error handling -- something like this:
VBA Code:
Sub MyMacro()

    Dim a As String
    Dim pt As PivotTable

    ThisWorkbook.Worksheets("Template").Activate

    a = Worksheets("Template").Cells(668, 4).Value

    On Error GoTo err_msg

    For Each pt In ActiveSheet.PivotTables
        With pt.PivotFields("SKU")
            .ClearAllFilters
            .CurrentPage = a
        End With
    Next

    On Error GoTo 0

    Exit Sub

err_msg:
    MsgBox "SKU Not available in the Pivot. Check if you selected the SKU correctly and if any items are already filtered out of the pivot. If the device is new, make sure its included in the source data.", vbCritical
 
End Sub
 
Upvote 0
Solution
That is because you have no conditions on that MsgBox and that line of code will be hit EVERY time.
You don't want to ignore the error, you want to add error handling -- something like this:
VBA Code:
Sub MyMacro()

    Dim a As String
    Dim pt As PivotTable

    ThisWorkbook.Worksheets("Template").Activate

    a = Worksheets("Template").Cells(668, 4).Value

    On Error GoTo err_msg

    For Each pt In ActiveSheet.PivotTables
        With pt.PivotFields("SKU")
            .ClearAllFilters
            .CurrentPage = a
        End With
    Next

    On Error GoTo 0

    Exit Sub

err_msg:
    MsgBox "SKU Not available in the Pivot. Check if you selected the SKU correctly and if any items are already filtered out of the pivot. If the device is new, make sure its included in the source data.", vbCritical
 
End Sub
thanks, makes sense. I appreciate the quick response. It is now working as intended.
 
Upvote 0
Hi @NormChart55
however it does it even if the SKU is available and there is no error?
Using the On Error statement will make it jump to the next statement regardless of the type of error, it can be a data error or it can be a code error.
Putting an error handler as suggested by Joe is recommended in some cases when you know what error is going to occur, but it also has its drawbacks because it will also skip any type of error and jump directly to the error message.

Ideally, try to control the error with code, whenever possible.
I show you an example of how it could be done.

Other details, do you have several pivot tables in the sheet?
If you only have one pivot table, then it could be like this:


VBA Code:
Sub MyMacro_v2()
  Dim a As String
  Dim pt As PivotTable
  Dim itm As PivotItem
  Dim exists As Boolean
  
  ThisWorkbook.Worksheets("Template").Activate
  
  a = Worksheets("Template").Cells(668, 4).Value
  Set pt = ActiveSheet.PivotTables(1)
  pt.PivotFields("SKU").ClearAllFilters
  For Each itm In pt.PivotFields("SKU").PivotItems
    If itm = a Then
      pt.PivotFields("SKU").CurrentPage = a
      exists = True
      Exit For
    End If
  Next

  If exists = False Then
    MsgBox "SKU Not available in the Pivot. " & _
           "Check if you selected the SKU correctly and " & _
           "if any items are already filtered out of the pivot. " & _
           "If the device is new, make sure its included in the source data.", vbCritical
  End If
End Sub
 
Upvote 0
Your welcome.
Glad we were able to help!
 
Upvote 0

Forum statistics

Threads
1,215,170
Messages
6,123,416
Members
449,099
Latest member
COOT

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