Excel VBA - 使用 3 个行元素和 1 个列元素从数据透视表中获取数据

标签 excel vba pivot-table

我有一个包含 11,000 多行数据和 53 列的数据透视表。我需要一段代码,它可以非常有效地从数据透视表中的特定单元格中获取数据。
这是数据透视表的样子:
Pivot Table
我需要能够使用左侧的三 (3) 列从特定单元格中获取数据,作为通过使用顶部的周数 1-50 选择正确行和偏移到正确列的条件。
Fetching the data
这是我当前代码的一个片段(它可以工作,但可能效率很低,因为我现在需要这个查询大约 48 次。

Dim oWB As Workbook: Set oWB = Workbooks.Open(tWB.Path & "\Telledata egeneide YTD.xlsx", ReadOnly:=True)
Dim oWS As Worksheet: Set oWS = oWB.Sheets("Telledata")
Dim pivotdata As Integer
Dim storeNo As Integer = 1101 'Example only - this can be anything between 1101 and 1199
Dim storeLoc As String = "0002" 'Example only - this value can be anything between 0001 and 0003
Dim materialGroup As Integer = 1120 'Example only - this can be anything between 1110 and 1899
Dim week as Integer = 11 'Example only - this can be anything between 1 and 50

pivotdata = Application.WorksheetFunction.CountIfs(oWS.Range("A:A"), storeNo, _
                                    oWS.Range("B:B"), storeLoc, _
                                    oWS.Range("C:C"), materialGroup, _
                                    oWS.Range("C:C").Offset(0, week), ">4")
我正在寻找高于 4 的值。如前所述,由于代码片段中的变量发生变化,最后的查询需要按当前状态运行多达 48 次。
示例查询:
storeNo: 1101
storeLoc: 0001
materialGroup: 1141, 1142, 1143, 1410, 1420, 1451, 1220, 1260, 1270, 1710, 1720, 1730
week: 11, 12

storeNo: 1101
storeLoc: 0002
materialGroup: 1141, 1142, 1143, 1410, 1420, 1451, 1220, 1260, 1270, 1710, 1720, 1730
week: 11, 12
如您所见,所有查询都必须运行两次(对于每个 storeLoc),而且我必须检查两个 WEEK 列(选择的一周和之后的一周)。
我一直在考虑一个 For/For Each 循环,但不知道如何去做......

最佳答案

这是使用基于 3 个查找列的唯一组合的字典查找的一种方法:
(未经测试)

Sub Tester()
    
    Dim pt As PivotTable, rngRows As Range, rngData As Range
    Dim dict As Object, arrRows, arrData, r As Long, k
    
    Set pt = ActiveSheet.PivotTables(1)
    
    'https://peltiertech.com/referencing-pivot-table-ranges-in-vba/
    Set rngRows = pt.RowRange   'get the rows range (includes headers)
    Set rngRows = rngRows.Offset(1, 0).Resize(rngRows.Rows.Count - 1) 'exclude headers
    arrRows = rngRows.Value     '... get as 2D array
    
    'create lookup based on the 3 columns
    Set dict = CreateObject("scripting.dictionary")
    For r = 1 To UBound(arrRows, 1)
        k = Key(arrRows(r, 1), arrRows(r, 2), arrRows(r, 3))
        dict(k) = r 'link the key to the row number
    Next r
    
    Set rngData = pt.DataBodyRange    'the data from the table
    arrData = rngData.Value           '... as 2D array
    
    
    'now you can use the lookup to quickly locate the data you want
    k = Key(storeNo, storeLoc, materialGroup)
    If dict.exists(k) Then
        weekVal = arrData(dict(k), WeekNum)
        If weekNum < UBound(arrData, 2) Then 
            nextWeekVal = arrData(dict(k), WeekNum + 1)
        End If
    Else
        'no match found
    End If
    
End Sub

'create key by concatenating the values with "|"
Function Key(v1, v2, v3)
    Key = Join(Array(v1, v2, v3), "|")
End Function

关于Excel VBA - 使用 3 个行元素和 1 个列元素从数据透视表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65352434/

相关文章:

string - 在VBA函数中返回带有上标字符的字符串

vba - 在 SQL 查询中使用单元格引用

Mysql销售表按客户分组按月列显示透视查询

vba - 如何在Excel工作表上显示vba宏的状态?

VBA:对照列表检查日期

excel-2007 - 使用数据透视表时如何显示 Excel 2007 生成的 MDX?

python - 使用pivot_table时将分类数据与数值数据相结合

c# - []、get_Item() 和 Item[] 之间的 Excel Interop 区别

r - 使用命令行时无法通过 openxlsx 包将绘图插入 XLSX

vba - 在vba中循环具有特定功能的工作表