1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
|
Public Sub DataButton_Click()
Dim wrk As DAO.Workspace
Dim dbconn As DAO.Database
Dim rs As DAO.Recordset
Dim fld As DAO.Field
Dim msgoption As Long
Dim x As Integer
Dim xlws As Excel.Worksheet
Dim xlws2 As Excel.Worksheet
Dim xlrng As Excel.Range
Set xlws = ActiveSheet
Set wrk = DAO.CreateWorkspace("myworkspace", "admin", "")
Set dbconn = work.OpenDatabase("\\adpsfs\Accounting\Sondra\VerisignTransactions.accdb")
Set rs = dbconn.OpenRecordset("Select * from [T_TestTable]")
msgoption = MsgBox("Do you want a PivotTable?", vbYesNoCancel, "Report Type")
Select Case msgoption
Case vbYes
Set xlring = xlws.Cells(4, 1)
On Error Resume Next
xlrng.RemoveSubtotal
x = 1
Next
Set xlrng = slws.Cells(5, 1)
xlrng.CopyFromRecordset rs
slws.Columns.AutoFit
Set xlrng = xlws.Columns("D:D")
xlrng.NumberFormat = "$#,##0.00"
Set xlrng = xlws.Range(xlws.Cells(4, 1), xlws.Cells(rs.RecordCount + 4, rs.Fields.Count))
Set xlws2 = ActiveWorkbook.Sheets.Add
xlws2.Name = "PivotTable"
xlws2.PivotTableWizard xlDatabase, xlrng, xlws2.Cells(3, 1), "Test Table", False, True, True, True, False, , True, True, , , True
xlws2.Cells(3, 1).Select
xlws2.PivotTables("TestTable").AddFields RowFields:="ProductName", ColumnFields:="CategoryName"
With xlws2.PivotTables("SalesByCategory").PivotFields("ProductSales")
.Orientation = xlDataField
.NumberFormat = "$#,##0.00"
End With
ActiveWorkbook.ShowPivotTableFieldList = False
Set xlws2 = Nothing
Case vbNo
Set xlring = xlws.Cells(4, 1)
On Error Resume Next
xlrng.RemoveSubtotal
x = 1
Next
Set xlrng = slws.Cells(5, 1)
xlrng.CopyFromRecordset rs
slws.Columns.AutoFit
Set xlrng = xlws.Columns("D:D")
xlrng.NumberFormat = "$#,##0.00"
Set xlrng = xlws.Range(xlws.Cells(4, 1), xlws.Cells(rs.RecordCount + 4, rs.Fields.Count))
xlrng.Subtotal 2, xlSum, 4, True, False, xlSummaryAbove
xlws.Outline.ShowLevels 2
Case vbCancel
GoTo ExitStuff
End Select
ExitStuff:
Set xlws = Nothing
Set xlws2 = Nothing
Set xlrng = Nothing
rs.Close
Set fld = Nothing
Set rs = Nothing
dbconn.Close
Set dbconn = Nothing
Set wrk = Nothing
End Sub
|