-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton_RunPivot.vb
205 lines (129 loc) · 6.22 KB
/
Button_RunPivot.vb
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
Public Sub Button_RunPivot(xlWrkSht_Main As Excel.Worksheet)
'Excel Objects
Dim xlWrkBk_Forecast As Excel.Workbook
Dim xlWrkSht As Excel.Worksheet
Dim xlCellSelect As Excel.Range
Dim rngShowReport As Excel.Range
'Local Variables
Dim sWorksheet_Name As String
Dim dStartDate As Date
Dim iWeeks_Ahead As Integer
Dim sCboWeeksAhead_ReturnValue As String
Dim sShowReport As String
On Error GoTo ProcErr
'Turn Worksheet Protection OFF
FN_Public_UnProtect_Workbook
'Turn Screen Updating OFF
With Application
.ScreenUpdating = False
End With
'Set Workbook object from Worksheet object
Set xlWrkBk_Forecast = xlWrkSht_Main.Parent
'Select worksheet after RunReport
xlWrkSht_Main.Calculate
Set rngShowReport = xlWrkBk_Forecast.Names("ShowReport").RefersToRange
'Get the Worksheet name from the ShowReport combobox from the Main page
sShowReport = rngShowReport.Value
' *** Get Parameters ***
'Set Value for the Start Date and the number of weeks ahead to report
' dStartDate = Sheet1.CboListDates.Value
dStartDate = xlWrkBk_Forecast.Names("vbParam_Select_StartDate").RefersToRange.Value
'sCboWeeksAhead_ReturnValue = Sheet1.cboWeeksAhead.Value
sCboWeeksAhead_ReturnValue = xlWrkBk_Forecast.Names("vbParam_Weeks_Ahead").RefersToRange.Value
'Calculate the number of weeks ahead base on the Text String pulled from the name range vbParam_Weeks_Ahead"
If sCboWeeksAhead_ReturnValue = "Current Week" Then
iWeeks_Ahead = 0
Else
iWeeks_Ahead = CInt(Left(sCboWeeksAhead_ReturnValue, 1))
End If
'>>>>>>>>>>>>>>>>>> RUN Pivot on tblEmployee_Opportunity <<<<<<<<<<<<<<<<<<<
'---------------------------- RUN Pivot Report ----------------------------------
If aaCreate_SQL_Join_ADODB_Recordset(xlWrkSht_Main, _
dStartDate, _
False, _
iWeeks_Ahead, _
"Pivot", _
"Pivot_WeeklyHours", _
"Pivot_Hours") = False Then
' Debug.Print "Pivot aaCreate_SQL_Join_ADODB_Recordset Error"
GoTo ProcExit
End If
'------------------------- RUN Data Forecast Report -------------------------------
If aaCreate_SQL_Join_ADODB_Recordset(xlWrkSht_Main, _
dStartDate, _
True, _
iWeeks_Ahead, _
"Data Forecast", _
"Data Forecast", _
"Data_Forecast") = False Then
' Debug.Print "Pivot aaCreate_SQL_Join_ADODB_Recordset Error"
GoTo ProcExit
End If
'------------------------- RUN Issue and Risks Report -------------------------------
If aaCreate_SQL_Join_ADODB_Recordset(xlWrkSht_Main, _
dStartDate, _
False, _
iWeeks_Ahead, _
"Issue and Risks", _
"Issue and Risks", _
"Issue_Risks") = False Then
' Debug.Print "Pivot aaCreate_SQL_Join_ADODB_Recordset Error"
GoTo ProcExit
End If
'---------------------------------------------------------------------
' Select worksheet after report runs
' NOTE: See if the worksheet David L Report can be found/listed.
' If it is found then select that worksheet
For Each xlWrkSht In xlWrkBk_Forecast.Worksheets
'Get the worksheet name and and set it variable sWorksheet_Name
sWorksheet_Name = xlWrkSht.Name
If sWorksheet_Name <> "Issue and Risks" And sWorksheet_Name <> "Data Forecast" And sWorksheet_Name <> "Pivot_WeeklyHours" Then
'If the worksheet name exist in the workbook then set the cursor to cell
If xlWrkSht.Name = sShowReport Then
'Activate worksheet
xlWrkSht.Activate
'Select cell in worksheet to set cursor
Set xlCellSelect = xlWrkSht.Cells(1, 4)
xlCellSelect.Select
Exit For
End If
Else
'If the worksheet name is Issue and Risks or Data Forecast or Pivot_WeeklyHours
'don't set a cell cursor
If xlWrkSht.Name = sShowReport Then
'Activate worksheet
xlWrkSht.Activate
Exit For
End If
End If
Next
ProcExit:
'Turn Worksheet Protection ON
FN_Public_Protect_Workbook
'Turn Screen Updating ON
With Application
.ScreenUpdating = True
End With
'Refresh all pivot tables
xlWrkBk_Forecast.RefreshAll
Exit Sub
ProcErr:
Select Case Err.Number
Case 91, 424 'Object not found Note: This occurs on the rsTrackChanges close statement
'Debug.Print " The error # is " & Err.Number & vbCrLf & "Description " & Err.Description & vbCrLf & vbCrLf & " The source " & Err.Source, vbCritical
Resume Next
Case 94 'Parameter not found
MsgBox "Description " & Err.Description & vbCrLf & "The error # is " & Err.Number & vbCrLf & "The source " & Err.Source, vbExclamation & vbCrLf & vbCrLf
Debug.Print " The error # is " & Err.Number & vbCrLf & "Description " & Err.Description & vbCrLf & vbCrLf & " The source " & Err.Source, vbCritical
Stop
Resume Next
'Resume ProcExit
Case 3704 'Recordset empty End program to stop more errors
Resume Next
Case Else
Debug.Print " The error # is " & Err.Number & vbCrLf & "Description " & Err.Description & vbCrLf & vbCrLf & " The source " & Err.Source, vbCritical
MsgBox "Description " & Err.Description & vbCrLf & "The error # is " & Err.Number & vbCrLf & "The source " & Err.Source, vbCritical
Stop
Resume ProcExit
End Select
End Sub