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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

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