How To Resize Data Source For Pivot w/VBA

mschles4695

New Member
Joined
Mar 10, 2011
Messages
1
I have the following VBA code which on click of a button pulls data from a SQL Server and populates an excel worksheet tab (same # of columns, potentially different # of rows). Everything works EXCEPT I don't know how in code to dynamically change the Data->Change Data Source->(cell range) to match the range of the newly imported data. Currently I have just set this range to A1:Z65000 which always works but gives me blanks in the pivot tables that are based on this data. HELP PLEASE!!!!

Dim Cn As Variant
Set Cn = CreateObject("ADODB.Connection")
Dim Server_Name As String
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim rs As Variant
Set rs = CreateObject("ADODB.Recordset")
Dim pt As PivotTable
Dim ws As Worksheet

Server_Name = "xxxxxxx" ' Enter your server name here
Database_Name = "yyyyyyy" ' Enter your database name here
User_ID = "dddddddd" ' Enter your user ID here
Password = "rrrrrrrr" ' Enter your password here
SQLStr = "SELECT EM.em_id, EM.em_std, EM.em_manager, EM.comments, " & _
" RM.rm_cat, RM.rm_type, RM.rm_std, RM.area, RM.fl_id, RM.rm_id, RM.dp_id, RM.dv_id, " & _
" BL.bl_id, BL.raa_site, " & _
" S.name AS Site " & _
"FROM afm.em EM " & _
" LEFT JOIN afm.rm RM ON (EM.fl_id = RM.fl_id) AND (EM.rm_id = RM.rm_id) AND (EM.bl_id = RM.bl_id) " & _
" LEFT JOIN afm.bl BL ON (RM.bl_id = BL.bl_id) " & _
" LEFT JOIN afm.site S ON (BL.site_id = S.site_id) " & _
"WHERE RM.rm_cat ='OFFICE' AND " & _
" ( RM.rm_type Not Like 'VISIT%' And " & _
" RM.rm_type Not Like 'OPEN%' And " & _
" RM.rm_type Not Like 'FLEX%' And " & _
" RM.rm_type Not Like 'FILE%' And " & _
" RM.rm_type Not Like 'OFF-%' And " & _
" RM.rm_type Not Like 'WRKAREA%') "

Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"

rs.Open SQLStr, Cn
' Data Dump to spreadsheet
With Worksheets("Data").Range("A2:Z65000") .ClearContents
.CopyFromRecordset rs
End With
' Update Pivot Tables
For Each ws In ActiveWorkbook.Worksheets
For Each pt In ws.PivotTables

HERE IS WHERE I NEED CODE THAT RESIZES THE NEWLY IMPORTEDSOURCEDATA RANGE

pt.RefreshTable
Next pt
Next ws
'
rs.Close
Set rs = Nothing
Cn.Close
Set Cn = Nothing
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

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