First,
My apologies if this is posted somewhere else with a solution but all my searches have come up with no specific solution(s). 2nd, I am not an expert programmer but I know enough to do some automation with the basics. I have been able to create a pivot table in Excel 2016 using vba with the source data in SQL. When I try to refresh the pivot cache/pivot table I get the 1004 Error "Problems Obtaining Data". I am able to dump the data in a worksheet with no issues so it's not a record count issue. I assume my vba code is incorrect somewhere and would prefer to know that prior to just dumping the data in a table with a pivot linked to it. Below is my code. Most of the answers I found said there was an issue with Excel and I could do some Registry work but before that I want to make sure it's nothing dumb. I've been married 3 times so being yelled at and verbally abused for things being messed up are ok with me. Any help would be outstanding - Thank you.
Code fails here: ActiveSheet.PivotTables("TBACTUALS").PivotCache.Refresh
I toggled the .EnableRefresh = True/False and it still fails....
Sub RefreshADOPivot()
Dim pvtT As PivotTable
Dim pvtC As PivotCache
Dim intindex As Integer
My apologies if this is posted somewhere else with a solution but all my searches have come up with no specific solution(s). 2nd, I am not an expert programmer but I know enough to do some automation with the basics. I have been able to create a pivot table in Excel 2016 using vba with the source data in SQL. When I try to refresh the pivot cache/pivot table I get the 1004 Error "Problems Obtaining Data". I am able to dump the data in a worksheet with no issues so it's not a record count issue. I assume my vba code is incorrect somewhere and would prefer to know that prior to just dumping the data in a table with a pivot linked to it. Below is my code. Most of the answers I found said there was an issue with Excel and I could do some Registry work but before that I want to make sure it's nothing dumb. I've been married 3 times so being yelled at and verbally abused for things being messed up are ok with me. Any help would be outstanding - Thank you.
Code fails here: ActiveSheet.PivotTables("TBACTUALS").PivotCache.Refresh
I toggled the .EnableRefresh = True/False and it still fails....
Sub RefreshADOPivot()
Dim pvtT As PivotTable
Dim pvtC As PivotCache
Dim intindex As Integer
Code:
Sheets("Test").Activate
Set pvtT = ActiveSheet.PivotTables("TBACTUALS")
If cnt.State = adStateClosed Then
cnt.Open strConn
End If
intindex = ActiveSheet.PivotTables("TBACTUALS").PivotCache.Index
CalYr = CInt(Mid(Range("CREATESCENARIO")Value, 5, 4))
pd = CInt(Mid(Range("CREATESCENARIO").Value, 10, 2)) - 1
bua = Range("BUA").Value
If cnt.State = adStateClosed Then
cnt.Open strConn
End If
With cmd
.ActiveConnection = strConn
.CommandType = adCmdStoredProc
.CommandText = "usp_TBActuals"
.Parameters.Append .CreateParameter("@calyr", adInteger, adParamInput, 4, CalYr)
.Parameters.Append .CreateParameter("@pd", adInteger, adParamInput, 2, pd)
.Parameters.Append .CreateParameter("@bua", adVarChar, adParamInput, 4, bua)
Set rst = .Execute
End With
If Not (rst.EOF Or rst.BOF) Then
pvtT.PivotCache.EnableRefresh = True
Set ActiveWorkbook.PivotCaches(intindex).Recordset = rst
[COLOR=#ff0000]ActiveSheet.PivotTables("TBACTUALS").PivotCache.Refresh[/COLOR]
End If
If cnt.State = adStateOpen Then
rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing
End If
End Sub
Last edited: