I am using a ADOdb connection to import specific columns of data within a user-defined date range from 4 seperate tabs in another Excel file. For example: Start Date is selected by user. End Date is selected by user. File is selected by user. All data within the date range is pulled from "Granulation" tab, "Blending" tab, "Compression" tab, and "coating" tab. The error: Currently this macro is pulling rows of data outside of the date range and ignoring some data that is within the date range. It does not appear to be an issue with the source file...I have tried to copy/paste values into a seperate file to eliminate date format issues, but it still pulls data outside defined range. Error data frequently is associated only with dates beginning with 1 (January, October, November, December) despite selecting otherwise.
Please review the macro and let me know if there are any issues OR a simpler way to achieve result.
Thank you,
Mark
Date Range begin is entered in R2C1. Date Range end is entered in R2C2. Source file is entered in R2C4.
Sub GetData()
Const adStateOpen As Long = 1
Const adOpenForwardOnly As Long = 1
Const adLockReadOnly As Long = 1
Const shOutput As String = "Output"
Dim oCnn As Object
Dim oRs As Object
Dim oFields As Object
Dim sFilePath As String
Dim sSQL As String, sCnn As String
Dim dStartDate As Date, dEndDate As Date
Dim shArray As Variant
Dim i As Long, Ptr As Long, LastRow As Long
' Check date formats
With Worksheets("Report Setup")
If Not IsDate(.Cells(2, "a")) Or Not IsDate(.Cells(2, "b")) Or Not Len(.Cells(2, "d")) > 0 Then
MsgBox "Invalid date and or no file selected."
Exit Sub
End If
dStartDate = .Cells(2, "a")
dEndDate = .Cells(2, "b")
sFilePath = .Cells(2, "d")
End With
sCnn = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & sFilePath & ";Extended Properties=""Excel 12.0 Xml;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
shArray = Array("Granulation", "Blending", "Compression", "Coating")
For i = LBound(shArray) To UBound(shArray)
' Query sheet
sSQL = "SELECT [Row #], [Room #], [Lot #], [Item #], [Product Description], [Batch Size], [Start Date], [Machine Hours] " & _
"FROM [" & shArray(i) & "$] " & _
"WHERE ((([Start Date])>=#" & dStartDate & "#) And " & _
"(([Start Date])<=#" & dEndDate & "#)) ORDER BY [Row #];"
Set oRs = CreateObject("ADODB.Recordset")
' Open Created Recordset
On Error Resume Next
oRs.Open sSQL, oCnn
If oRs.State <> adStateOpen Then
MsgBox "Could not table: " & sSQL, vbCritical
oCnn.Close
Set oCnn = Nothing
Exit Sub
End If
On Error GoTo 0
Ptr = 0
With Worksheets(shOutput)
'.Cells.Clear
If Not i > 0 Then
For Each oFields In oRs.Fields
Ptr = Ptr + 1
.Cells(1, Ptr) = oFields.Name
Next
End If
LastRow = .Cells(Rows.CountLarge, "a").End(xlUp).Row + 1
If Not oRs.BOF Then
.Range("a" & LastRow).CopyFromRecordset oRs
End If
End With
' Close Recordset
oRs.Close
Set oRs = Nothing
Next i
With Worksheets(shOutput)
'Convert text Date field and text Machine Hours field to number values:
.Range("g2:g" & .Cells(Rows.CountLarge, "a").End(xlUp).Row) = _
.Range("g2:g" & .Cells(Rows.CountLarge, "a").End(xlUp).Row).Value
.Range("h2:h" & .Cells(Rows.CountLarge, "a").End(xlUp).Row) = _
.Range("h2:h" & .Cells(Rows.CountLarge, "a").End(xlUp).Row).Value
End With
' Close Connecton
oCnn.Close
Set oCnn = Nothing
End Sub
Bookmarks