0

这是一个宏的开始,它成功地根据指定工作表中的单元格创建数据透视表:

aray = Array("'Sheet1'!R1C5:R102C10", "'Sheet2'!R1C5:R102C10", "'Sheet3'!R1C5:R102C10", "'Sheet4'!R1C5:R102C10")

ActiveWorkbook.PivotCaches.Create(SourceType:=xlConsolidation, SourceData:= aray, _
    Version:= _
    xlPivotTableVersion15).CreatePivotTable TableDestination:= _
    "'[Filename.xlsm]sheet5'!R1C1", TableName:= _
    "RatingSumPiv", DefaultVersion:=xlPivotTableVersion15

我的问题是我不知道床单的名称或数量。它们的名称将在名为“UnitNumbers”的范围(ListObject)中。

如何自动将工作表名称加载到数组中?

4

1 回答 1

1
Sub Sample()
Dim loSheetNames As ListObject
Dim rngCurrentSheetName As Range
Dim toprowoflo As Long
Dim lngCurrentIndex As Long

Set loSheetNames = ActiveWorkbook.Worksheets("Sheet1").ListObjects("listob1")
toprowoflo = loSheetNames.Range.Row
ReDim aray(loSheetNames.Rows.Count)

For Each rngCurrentSheetName In loSheetNames.Range
    lngCurrentIndex = rngCurrentSheetName.Row - toprowoflo + 1
    aray(lngCurrentIndex) = rngCurrentSheetName & "!R1C5:R102C10"
Next rngCurrentSheetName

ActiveWorkbook.PivotCaches.Create(SourceType:=xlConsolidation, SourceData:=aray, _
    Version:= _
    xlPivotTableVersion15).CreatePivotTable TableDestination:= _
    "'[Filename.xlsm]sheet5'!R1C1", TableName:= _
    "RatingSumPiv", DefaultVersion:=xlPivotTableVersion15

End Sub
于 2013-09-26T22:33:42.333 回答