if column does not contain value then do not create pivot table

florianvk

New Member
Joined
Apr 26, 2016
Messages
8
Hello dear Excel users,

I wrote a macro that is creating 3 pivot tables, all in separate sheets, from the same table (table1) in the sheet RAWDATA.
The last pivot table must be created only when at least 1 row in table1 in column K is containing the word TRUCK.
If no rows exist with the word TRUCK in column K, the macro have to step over the part creating the pivot table and just select the sheet with the first pivot table called SUMMARY.

I have tried with 'Select Case' like the belwo example but I cannot define a range of cells (for example K2:K1000) with this function only 1 cell (K2).

Can sombody help me out?

Sheets("RAWDATA").Select
Select Case Range("K2").Value
Case "TRUCK"
GoTo 1111
Case Else
GoTo 2222


1111:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Table1", Version:=xlPivotTableVersion14).CreatePivotTable TableDestination _
:="", TableName:="PivotTable3", DefaultVersion:=xlPivotTableVersion14
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Vehicle Type Code")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("N or U")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Pod Name")
.Orientation = xlRowField
.Position = 2
End With
2222:
Sheets("SUMMARY").Select
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi.
If you're simply trying to confirm the existence of the value 'truck' in Sheet "RAWDATA", column K, then try replacing:
Code:
Sheets("RAWDATA").Select
     Select Case Range("K2").Value
  Case "TRUCK"
  GoTo 1111
  Case Else
  GoTo 2222
with something like this:
Code:
If Application.WorksheetFunction.CountIf(Sheets("RAWDATA").Range("K:K"), "TRUCK") > 0 Then GoTo 1111 Else GoTo 2222

Hope it helps.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

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