Can it be possible to make “SourceData:= _” variable

谁说胖子不能爱 提交于 2020-01-16 18:24:12

问题


I have the code below came from a registered macro for creating a pivot table, my wich is to this code available for next row that will be present in the sheet "DONNEES", columns will not increment only, It's look like I can set it like "DONNEES!R1C1:R65500C16" but can it be more proper, with a variable that I can include,

`ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "DONNEES!R1C1:R1553C16", Version:=xlPivotTableVersion10).CreatePivotTable _
    TableDestination:="Sheet2!R1C1", TableName:="PivotTable1", DefaultVersion _
    :=xlPivotTableVersion10`

Thank's in advance, Regards,


回答1:


Dim rng as Range

Set rng = ActiveWorkbook.Sheets("DONNEES").Range("A1:P1553")

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= rng, _
    Version:=xlPivotTableVersion10).CreatePivotTable _
    TableDestination:="Sheet2!R1C1", TableName:="PivotTable1", _
    DefaultVersion:=xlPivotTableVersion10



回答2:


I think you are misunderstanding what "_" does. In VB "_" means the line of code is continued on the line below. The is the same statement with them removed:

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="DONNEES!R1C1:R1553C16", Version:=xlPivotTableVersion10).CreatePivotTable TableDestination:="Sheet2!R1C1", TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10

Also when you see something like "SourceType:=" this is VB's way of allowing non-strict parameter order, where normally a function has its parameters in a specific order and you must pass them in this order, but the "X:=Y" layout allows you to tell VB "this parameter value of Y belongs to parameter named X". Order disregarded. SourceType:="DONNEES!R1C1:R1553C16" really just says the parameter named "SourceType" will receive the value "DONNEES!R1C1:R1553C16"

And yes, you could use a variable in the value's place

MyVariable="DONNEES!R1C1:R1553C16"
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=MyVariable, Version:=xlPivotTableVersion10).CreatePivotTable TableDestination:="Sheet2!R1C1", TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10

However you cannot dynamically change the SourceType:= piece.




回答3:


it's a bit late in time, but I stumbled with a similar problem and maybe my solution is helpful for others.

First, microsoft's documentation says:

"When passing a Range, it is recommended to either use a string to specify the workbook, worksheet, and cell range, or set up a named range and pass the name as a string. Passing a Range object may cause "type mismatch" errors unexpectedly."

So in my experience, passing a range object works OK most of the times, but sometimes a type mismatch is returned. The "unexpectedly" ending is what made me go for a workaround, which is very simple. I found that the method will accept rng.Address only if the activesheet is the data sheet, so I do:

rng.Worksheet.Activate()
cache = ActiveWorkbook.PivotCaches().Create(1, rng.Address, 6)
pivot = cache.CreatePivotTable(destination, name, True, 6)

Please, note that I'm doing this in python by using pywintypes, so in VBA would probably be ".Activate" without parentheses, and object variables declared and set, etc.

"destination" in the CreatePivotTable method is a range object and didn't have any problem so far.



来源:https://stackoverflow.com/questions/21006692/can-it-be-possible-to-make-sourcedata-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!