简体   繁体   中英

How to change the source data of an Excel pivot table

Aim to achieve : I want to change the source data for my pivot table in Excel using VB.Net to a named range.

I have :

table.ChangePivotCache(wb.PivotCaches.Create(SourceType:=Excel.XlPivotTableSourceType.xlDatabase, SourceData:=sheet.Names("name_of_NamedRange").RefersToRange))

Exception :

The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

您确定sheet.Names("name_of_NamedRange").Value指向范围吗?

SourceData needs to be a string that contains the address, including the sheet name, like 'My Sheet'!A1:B10. Try this:

table.ChangePivotCache(wb.PivotCaches.Create(SourceType:=Excel.XlPivotTableSourceType.xlDatabase, SourceData:=sheet.Names("name_of_NamedRange").RefersToRange.Address(External:=True)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM