I have a macro that exists in an empty workbook. Every day data will be pasted in and the macro will then be ran. One part of the macro is that it updates the data source in a pivot table and refreshes. When I paste the data in and run the macro it throws the error message of a blank column heading. The dynamic element doesn't seem to be working correctly.
Function PivotTable()
Dim Data_sht As Worksheet
Dim Pivot_sht As Worksheet
Dim StartPoint As range
Dim DataRange As range
Dim PivotName As String
Dim NewRange As String
'Set Variables Equal to Data Sheet and Pivot Sheet
Set Data_sht = ThisWorkbook.Worksheets("Sheet1")
Set Pivot_sht = ThisWorkbook.Worksheets("Pivot Table")
'Enter in Pivot Table Name
PivotName = "PivotTable"
'Dynamically Retrieve Range Address of Data
Set StartPoint = Data_sht.range("A1")
Set DataRange = Data_sht.range(StartPoint, StartPoint.SpecialCells(xlLastCell))
NewRange = Data_sht.Name & "!" & _
DataRange.Address(ReferenceStyle:=xlR1C1)
'Make sure every column in data set has a heading and is not blank (error prevention)
If WorksheetFunction.CountBlank(DataRange.Rows(1)) > 0 Then
MsgBox "One of your data columns has a blank heading." & vbNewLine _
& "Please fix and re-run!.", vbCritical, "Column Heading Missing!"
End If
'Change Pivot Table Data Source Range Address
Pivot_sht.PivotTables(PivotName).ChangePivotCache _
ThisWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=NewRange)
'Ensure Pivot Table is Refreshed
Pivot_sht.PivotTables(PivotName).RefreshTable
End Function
CountBlankcheck should haveEndif the clause is triggered, since I'm guessing you don't want to execute the rest of the code if there's a blank column header. Have you doneDebug.Printto ensure theDataRangeand theNewRangestring are being set correctly?