Excel VBA validation not working with blanks

CRUTHERFORD

Board Regular
Joined
Jul 10, 2014
Messages
107
Hi,

I have the below code that filters a pivot table based on a cell value but if the cell value is blank it errors out, not sure what I'm doing wrong as it seems simple - any help appreciated.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


' Filters Pivots table on Market Name
If Target.Row = 2 And Target.Column = 6 Then
If Target.Value = "All Markets" Then
Sheets("Pivots").PivotTables("PivotTable1").PivotFields("MarketName").CurrentPage = "All"
Else
Sheets("Pivots").PivotTables("PivotTable1").PivotFields("MarketName").CurrentPage = Target.Value
End If
End If


' Filters Pivots table on Channel
If Target.Row = 2 And Target.Column = 18 Then
If Target.Value <> "All Channels" Then
Sheets("Pivots").PivotTables("PivotTable1").PivotFields("Channel").CurrentPage = Target.Value
Else
Sheets("Pivots").PivotTables("PivotTable1").PivotFields("Channel").CurrentPage = "All"
End If
End If


' Filters Pivots table on Year
If Target.Row = 2 And Target.Column = 30 Then
If Target.Value <> "All" Then
Sheets("Pivots").PivotTables("PivotTable1").PivotFields("Publish_Year").CurrentPage = Target.Value
Else
Sheets("Pivots").PivotTables("PivotTable1").PivotFields("Publish_Year").CurrentPage = "All"
End If
End If


' Filters Pivots table on Month
If Target.Row = 2 And Target.Column = 42 Then
If Target.Value <> "All" Then
Sheets("Pivots").PivotTables("PivotTable1").PivotFields("Publish_Month").CurrentPage = Target.Value
Else
Sheets("Pivots").PivotTables("PivotTable1").PivotFields("Publish_Month").CurrentPage = "All"
End If
End If




End Sub

Thanks
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi

You cannot select a Null for any of the pivot fields.

You need to add a further condition to each of your tests like
Code:
[COLOR=#333333]If Target.Row = 2 And Target.Column = 6 And Target <>"" Then
[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,215,507
Messages
6,125,201
Members
449,214
Latest member
mr_ordinaryboy

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