
Originally Posted by
Micron
Possibly there is something lacking in a code variable or parameter when the user isn't logged on that you're overlooking. Or an external file/macro/code is not running/is held up because of the same reason? Or the app isn't getting a valid token from Windows because they're not logged on? Maybe post the module code for the failing operation?
Could be? The thing that is lacking here is what I'm trying to determine, if it can be corrected so that this code can run when the user isn't logged on. This computer is on a corporate domain and group policy sets when it logs out for security reasons.
Here is the code in question. Maybe I'm going about this the wrong way (I'm no pro at this), so if there's another way to go about looping through the sheets and creating pivots on a workbook, I'm all ears!
Code:
Option Explicit
Private objExcel As Excel.Application
Private wb As Excel.Workbook
Private ws As Excel.Worksheet
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Function create_pt()
Dim strFile As String
Dim LastRow As Long, LastRange As Long, x As Long
Dim SourceRange As Range
Dim i As Integer
strFile = "\\server\path\file.xlsb"
Set objExcel = Excel.Application
objExcel.Visible = False
Set wb = objExcel.Workbooks.Open(strFile)
i = Round(((wb.Worksheets.Count + 1) / 2), 0)
For Each ws In wb.Worksheets
If ws.Index > 1 And ws.Index <= i Then
With ws
LastRange = .Range("A" & Rows.Count).End(xlUp).Row
Set SourceRange = .Range("A1:N" & LastRange)
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A" & LastRow + 3).Formula = ws.Name
wb.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SourceRange).CreatePivotTable _
TableDestination:=Worksheets("Summary").Range("A" & LastRow + 4), _
TableName:="PivotTable" & ws.Index
Sheets("Summary").Select
With ActiveSheet.PivotTables("PivotTable" & ws.Index).PivotFields("Facility")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable" & ws.Index).AddDataField ActiveSheet.PivotTables("PivotTable" & ws.Index).PivotFields("Charges"), "Count of Charges", xlCount
ActiveSheet.PivotTables("PivotTable" & ws.Index).PivotFields("Count of Charges").NumberFormat = "#,##0"
ActiveSheet.PivotTables("PivotTable" & ws.Index).AddDataField ActiveSheet.PivotTables("PivotTable" & ws.Index).PivotFields("Charges"), "Sum of Charges", xlSum
ActiveSheet.PivotTables("PivotTable" & ws.Index).PivotFields("Sum of Charges").NumberFormat = "_($* #,##0_);_($* (#,##0);_($* ""-""_);_(@_)"
End With
ElseIf ws.Index > i Then
With ws
LastRange = .Range("A" & Rows.Count).End(xlUp).Row
Set SourceRange = .Range("A1:N" & LastRange)
LastRow = Range("E" & Rows.Count).End(xlUp).Row
Range("E" & LastRow + 3).Formula = ws.Name
wb.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SourceRange).CreatePivotTable _
TableDestination:=Worksheets("Summary").Range("E" & LastRow + 4), _
TableName:="PivotTable" & ws.Index
Sheets("Summary").Select
With ActiveSheet.PivotTables("PivotTable" & ws.Index).PivotFields("Facility")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable" & ws.Index).AddDataField ActiveSheet.PivotTables("PivotTable" & ws.Index).PivotFields("Charges"), "Count of Charges", xlCount
ActiveSheet.PivotTables("PivotTable" & ws.Index).PivotFields("Count of Charges").NumberFormat = "#,##0"
ActiveSheet.PivotTables("PivotTable" & ws.Index).AddDataField ActiveSheet.PivotTables("PivotTable" & ws.Index).PivotFields("Charges"), "Sum of Charges", xlSum
ActiveSheet.PivotTables("PivotTable" & ws.Index).PivotFields("Sum of Charges").NumberFormat = "_($* #,##0_);_($* (#,##0);_($* ""-""_);_(@_)"
End With
Else:
End If
Next ws
For x = 1 To Worksheets.Count
With Sheets(x)
If .Name <> "Summary" Then
.Columns("T:AQ").Columns.Group
.Outline.ShowLevels rowlevels:=0, columnlevels:=1
.Select
.Range("A1").Select
End If
End With
Next x
Sheets("Summary").Select
Range("A1").Select
wb.Close savechanges:=True
objExcel.Quit
Set objExcel = Nothing
End Function