@Amber
See if this helps. Sorry took so long I'v been busy all day. I fixed up your code some. Also included a function to check if the workbook is already opened. If it is then we will leave it open if not we close it after were done with it. See attachment Ver1
Sub OTReportdoi()
Const sPath As String = "C:\DMPS\" '<==== Change if needed to path of wb. Include \ at the end
Const sFileName As String = "61955cb_2.xls" '<==== Name of the wb we want to work with
Const shSource As String = "61955cb_2" '<==== Change to Source sheet name
Const EDept_1 As String = "E:E" '<==== If any columns change just change here.
Const OT_H As String = "K:K" '<
Const OT_E As String = "L:L" '<
Const shDest As String = "3" '<==== Change to the sheet name that we want the data togo to.
Dim wbDest As Workbook
Dim wbOpen As Boolean
Dim strFormula As String '<== if the file name or sheet name changes
' in source wb we change it once. So we dont have to mess with all the formulas.
Set wbDest = ThisWorkbook
wbOpen = isWorkbookOpen(sFileName)
On Error GoTo EarlyExit
Application.ScreenUpdating = False
If Not wbOpen Then
Set wb = Workbooks.Open(sPath & sFileName) '<=== Workbook not open. do our thing and close it at the bottom of code.
End If
strFormula = "'[" & sFileName & "]" & shSource & "'!"
With wbDest.Worksheets(shDest)
.Range("L46:N50").ClearContents
.Range("L46") = Evaluate("=SUMPRODUCT(--(" & strFormula & EDept_1 & ">=4000),--(" & strFormula & EDept_1 & "<=4037),--(" & strFormula & OT_E & "<>0))")
.Range("M46") = Evaluate("=SUMPRODUCT(--(" & strFormula & EDept_1 & ">=4000),--(" & strFormula & EDept_1 & "<=4037)," & strFormula & OT_H & ")")
.Range("N46") = Evaluate("=SUMPRODUCT(--(" & strFormula & EDept_1 & ">=4000),--(" & strFormula & EDept_1 & "<=4037)," & strFormula & OT_E & ")")
.Range("L47") = Evaluate("=SUMPRODUCT(--(" & strFormula & EDept_1 & ">=5010),--(" & strFormula & EDept_1 & "<=5016),--(" & strFormula & OT_E & "<>0))")
.Range("M47") = Evaluate("=SUMPRODUCT(--(" & strFormula & EDept_1 & ">=5010),--(" & strFormula & EDept_1 & "<=5016)," & strFormula & OT_H & ")")
.Range("N47") = Evaluate("=SUMPRODUCT(--(" & strFormula & EDept_1 & ">=5010),--(" & strFormula & EDept_1 & "<=5016)," & strFormula & OT_E & ")")
.Range("L48") = Evaluate("=SUMPRODUCT(--(" & strFormula & EDept_1 & "=5000),--(" & strFormula & OT_E & "<>0))")
.Range("M48") = Evaluate("=SUMPRODUCT(--(" & strFormula & EDept_1 & "=5000)," & strFormula & OT_H & ")")
.Range("N48") = Evaluate("=SUMPRODUCT(--(" & strFormula & EDept_1 & "=5000)," & strFormula & OT_E & ")")
.Range("L49") = Evaluate("=SUMPRODUCT(--(" & strFormula & EDept_1 & ">=5020),--(" & strFormula & EDept_1 & "<=5022),--(" & strFormula & OT_E & "<>0))")
.Range("M49") = Evaluate("=SUMPRODUCT(--(" & strFormula & EDept_1 & ">=5020),--(" & strFormula & EDept_1 & "<=5022)," & strFormula & OT_H & ")")
.Range("N49") = Evaluate("=SUMPRODUCT(--(" & strFormula & EDept_1 & ">=5020),--(" & strFormula & EDept_1 & "<=5022)," & strFormula & OT_E & ")")
.Range("L50") = Evaluate("=SUMPRODUCT(--(" & strFormula & EDept_1 & ">=5030),--(" & strFormula & EDept_1 & "<=5032),--(" & strFormula & OT_E & "<>0))")
.Range("M50") = Evaluate("=SUMPRODUCT(--(" & strFormula & EDept_1 & ">=5030),--(" & strFormula & EDept_1 & "<=5032)," & strFormula & OT_H & ")")
.Range("N50") = Evaluate("=SUMPRODUCT(--(" & strFormula & EDept_1 & ">=5030),--(" & strFormula & EDept_1 & "<=5032)," & strFormula & OT_E & ")")
End With
EarlyExit:
' If we opened wb then close it if not leave it open
If Not wbOpen Then
wb.Close
End If
Application.ScreenUpdating = True
Set wbDest = Nothing
Set wb = Nothing
' Check if there were ant errors
If Err.Number <> 0 Then
MsgBox "Error #: " & Err.Number & vbCrLf & "Description: " & Err.Description, vbInformation, "Error"
End If
End Sub
Private Function isWorkbookOpen(bookName As String) As Boolean
Dim vbResult As Boolean
Dim wbs As Workbook
vbResult = False
For Each wbs In Workbooks
If UCase(wbs.Name) = UCase(bookName) Then
vbResult = True
Exit For
End If
Next wbs
isWorkbookOpen = vbResult
End Function
And heres another way. I like this way. It will query the sheet like a database. A lot faster. See attachment Ver2
Sub OTReportdoi_v2()
Const sPath As String = "C:\DMPS\" '<==== Change if needed to path of wb. Include \ at the end
Const sFileName As String = "61955cb_2.xls" '<==== Change if needed to Name of the wb we want to work with
Const shSource As String = "61955cb_2" '<==== Change if needed to Source sheet name
Const shDest As String = "3" '<==== Change if needed to the sheet name that we want the data togo to.
Const EDept_1 As String = "EDept_1" '<==== If any Columns Headers names change, jus change them here.
Const OT_H As String = "OT H" '<
Const OT_E As String = "OT E" '<
Const adStateOpen As Long = 1
Const adOpenForwardOnly As Long = 1
Const adLockReadOnly As Long = 1
Dim oCnn As Object
Dim oRs As Object
Dim sSQL As String, sCnn As String, sDataSource As String
Dim aSQL(0 To 4) As String, indx As Long
With Worksheets(shDest)
.Range("L46:N50").ClearContents
End With
sDataSource = sPath & sFileName
sCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& sDataSource & ";Extended Properties=""Excel 8.0;HDR=YES"""
' Create Connection Object
Set oCnn = CreateObject("ADODB.Connection")
' Open Created Connection
On Error Resume Next
oCnn.Open sCnn
' Check Connection State.
If oCnn.State <> adStateOpen Then
MsgBox "File Not found: " & vbCrLf & oCnn.ConnectionString, vbCritical
Exit Sub
End If
On Error GoTo 0
' Assign aSQL with our 5 query Category
' Blanking
aSQL(0) = "SELECT Count([" & OT_E & "]) AS Count_OT, Sum([" & OT_H & "]) AS Sum_OT_H, Sum([" & OT_E & "]) AS SumOT_E " & _
"FROM [" & shSource & "$] " & _
"WHERE ((([" & EDept_1 & "])>=4000 And ([" & EDept_1 & "])<=4037) AND (([" & OT_E & "])<>0));"
' Maintenance
aSQL(1) = "SELECT Count([" & OT_E & "]) AS Count_OT, Sum([" & OT_H & "]) AS Sum_OT_H, Sum([" & OT_E & "]) AS SumOT_E " & _
"FROM [" & shSource & "$] " & _
"WHERE ((([" & EDept_1 & "])>=5010 And ([" & EDept_1 & "])<=5016) AND (([" & OT_E & "])<>0));"
' Supervisors
aSQL(2) = "SELECT Count([" & OT_E & "]) AS Count_OT, Sum([" & OT_H & "]) AS Sum_OT_H, Sum([" & OT_E & "]) AS SumOT_E " & _
"FROM [" & shSource & "$] " & _
"WHERE ((([" & EDept_1 & "])=5000) AND (([" & OT_E & "])<>0));"
' Quality
aSQL(3) = "SELECT Count([" & OT_E & "]) AS Count_OT, Sum([" & OT_H & "]) AS Sum_OT_H, Sum([" & OT_E & "]) AS SumOT_E " & _
"FROM [" & shSource & "$] " & _
"WHERE ((([" & EDept_1 & "])>=5020 And ([" & EDept_1 & "])<=5022) AND (([" & OT_E & "])<>0));"
' Office
aSQL(4) = "SELECT Count([" & OT_E & "]) AS Count_OT, Sum([" & OT_H & "]) AS Sum_OT_H, Sum([" & OT_E & "]) AS SumOT_E " & _
"FROM [" & shSource & "$] " & _
"WHERE ((([" & EDept_1 & "])>=5030 And ([" & EDept_1 & "])<=5032) AND (([" & OT_E & "])<>0));"
' Loop tru aSQL with our 5 query Category
For indx = LBound(aSQL) To UBound(aSQL)
sSQL = aSQL(indx)
Set oRs = CreateObject("ADODB.Recordset")
' Open Created Recordset
On Error Resume Next
oRs.Open sSQL, oCnn, adOpenForwardOnly, adLockReadOnly
If oRs.State <> adStateOpen Then
MsgBox Err.Description
oCnn.Close
Set oCnn = Nothing
Exit Sub
End If
On Error GoTo 0
If Not oRs.EOF Then
' Add values
With ThisWorkbook.Worksheets(shDest)
.Cells(46, "L").Offset(indx).Resize(, 3).CopyFromRecordset oRs
End With
End If
' Close Recordset
oRs.Close
Set oRs = Nothing
Next
' Close Connecton
oCnn.Close
Set oCnn = Nothing
You can run the macros by clicking on your little man with the hard hat.
Ver1.xlsm
Ver2.xlsm
Bookmarks