简体   繁体   中英

How to create Pivot from selection, in VBA, Excel

I tried analyzing how Excel creates a pivot from a selection. It seems pretty simple. I select the entire range in my document and hit Create Pivot with that range. That produces the following code:

Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "Keyword report!R2C1:R643841C25", Version:=xlPivotTableVersion15). _
    CreatePivotTable TableDestination:="Sheet38!R3C1", TableName:="PivotTable2" _
    , DefaultVersion:=xlPivotTableVersion15

I want to use this code in the future, but instead of using a fixed reference to a particular range, I want Excel to draw that data from the Selection. For some reason, that's not included by the Macro Record. I have no idea what "R2C1:R643841C25" is or how it is derived. My selection is: A1:Y643841.

The big question: How do I replace R2C1:R643841C25 in the code with the "Selection"?

了解R1C1表示法将有助于您回答这一问题。

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