Hi @yogeshc99 could you please show your input and output for the final object. I am having trouble replicating the work you did..
Thanks a lot
Thanks a lot
Thank you for all the help guys. Here is final code. You can have pivot and source data in different sheets.
Const xlDown as long = -4121
Const xlToRight as long = -4161
Dim sBook As Object
Dim dsSheet, dpSheet As Object
Dim rng As String
Dim pvtTbl As Object
Dim srcData As String
Dim excel, sheet As Object
Try
sBook = GetWorkbook(Handle, SourceWorkbook)
dsSheet = sBook.Worksheets(DataSourceWorksheet)
dpSheet = sBook.Worksheets(PivotWorksheet)
sBook.Activate()
dsSheet.Activate()
excel = sBook.Application
sheet = excel.ActiveSheet
rng = sheet.Range("A1:C12").Address(ReferenceStyle:=-4150)
rng = dsSheet.Name & "!" & rng
dpSheet.Activate()
sheet = excel.ActiveSheet
sheet.PivotTables(PivotTableName).SourceData = rng
Success = True
oRange = Cstr(rng)
Catch e As Exception
Success = False
Message = e.Message
Finally
sBook = Nothing
dsSheet = Nothing
dpSheet = Nothing
End Try
Thanks a lot @yogeshc99
However I can't seem to make it work. I am using the code you posted in #99 but without results, and I am etting an exception all the time.
The code compiler throws the next errors:
Description: Compiler warning at line 2: Unused local constant: 'xlToRight'.
Description: Compiler warning at line 1: Unused local constant: 'xlDown'.
Description: Compiler warning at line 7: Unused local variable: 'pvtTbl'.
Description: Compiler warning at line 8: Unused local variable: 'srcData'.
Also, where do I input the new Source data range. I am not an expert in VBS but it seems to me you don't use an input for that, is that so?
Thanks a lot for your help, I spent yesterday quite a lot of hours with this
Thank you for all the help guys. Here is final code. You can have pivot and source data in different sheets.
Const xlDown as long = -4121
Const xlToRight as long = -4161
Dim sBook As Object
Dim dsSheet, dpSheet As Object
Dim rng As String
Dim pvtTbl As Object
Dim srcData As String
Dim excel, sheet As Object
Try
sBook = GetWorkbook(Handle, SourceWorkbook)
dsSheet = sBook.Worksheets(DataSourceWorksheet)
dpSheet = sBook.Worksheets(PivotWorksheet)
sBook.Activate()
dsSheet.Activate()
excel = sBook.Application
sheet = excel.ActiveSheet
rng = sheet.Range("A1:C12").Address(ReferenceStyle:=-4150)
rng = dsSheet.Name & "!" & rng
dpSheet.Activate()
sheet = excel.ActiveSheet
sheet.PivotTables(PivotTableName).SourceData = rng
Success = True
oRange = Cstr(rng)
Catch e As Exception
Success = False
Message = e.Message
Finally
sBook = Nothing
dsSheet = Nothing
dpSheet = Nothing
End Try
hi i am also getting same error please any leads helpThis is the error that I get with this solution - attached screenshot