From 144c0f85dba0ac3cd8099272eae5da1464b39094 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Wed, 16 Jan 2019 13:48:18 -0500 Subject: [PATCH] start work on building an sql filter for a pivot table selection --- pivot.bas | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 pivot.bas diff --git a/pivot.bas b/pivot.bas new file mode 100644 index 0000000..f442cc3 --- /dev/null +++ b/pivot.bas @@ -0,0 +1,51 @@ +Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) + + Cancel = True + + Dim i As Long + Dim j As Long + Dim k As Long + + Dim ri As PivotItemList + Dim ci As PivotItemList + Dim df As Object + Dim rd As Object + Dim cd As Object + Dim dd As Object + + Set ri = Target.Cells.PivotCell.RowItems + Set ci = Target.Cells.PivotCell.ColumnItems + Set df = Target.Cells.PivotCell.DataField + + Set rd = Target.Cells.PivotTable.RowFields + Set cd = Target.Cells.PivotTable.ColumnFields + + Dim sql As String + + For i = 1 To ri.Count + If i <> 1 Then sql = sql & vbCrLf & "AND " + sql = sql & rd(piv_pos(rd, i)).Name & " = '" & ri(i).Name & "'" + Next i + + For i = 1 To ci.Count + sql = sql & vbCrLf & "AND " + sql = sql & cd(piv_pos(cd, i)).Name & " = '" & ci(i).Name & "'" + Next i + + MsgBox (sql) + +End Sub + +Function piv_pos(list As Object, target_pos As Long) As Long + Dim i As Long + + For i = 1 To list.Count + If list(i).Position = target_pos Then + piv_pos = i + Exit Function + End If + Next i + 'should not get to this point + + +End Function