1

I set up an event listener in Outlook that, when a certain item drops into a mailbox, will take the attachment from the email, unzip the file and save it into a directory.

I use ADODB.Connection to take the data from that saved document and copy it to a different Excel workbook.

My data has dates being read from 03-11-2025 into 11/03/2025. I do not know how to fix this without causing the entire thing to stall for minutes on end.

Option Explicit

Sub CopyDataFromXLS(saveFolder As String, newFileName As String, receivedDate As String, receivedHour As String, extractedFileName As String)

    Dim conn As Object
    Dim rs As Object
    Dim strConnection As String
    Dim strSQL As String
    Dim ws As Worksheet
    Dim targetWorkbook As Workbook
    Dim targetSheet As Worksheet
    Dim originalSheetName As String
    Dim lastRow As Long
    Dim lastCol As Long
    Dim targetRow As Long
    Dim dataArray As Variant
    Dim transposedArray As Variant
    Dim i As Long, j As Long

    originalSheetName = Left(extractedFileName, Len(extractedFileName) - 5)

    Dim filePath As String
    filePath = saveFolder

    strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & filePath & ";Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;"""
    
    Set conn = CreateObject("ADODB.Connection")

    On Error GoTo ErrorHandler

    conn.Open strConnection

    strSQL = "SELECT * FROM [" & originalSheetName & "$]"

    Set rs = CreateObject("ADODB.Recordset")
    rs.Open strSQL, conn

    lastCol = rs.Fields.count

    dataArray = rs.GetRows
    lastRow = UBound(dataArray, 2) + 1

    rs.Close
    conn.Close

    ReDim transposedArray(0 To UBound(dataArray, 2), 0 To UBound(dataArray, 1))
 
    For i = 0 To UBound(dataArray, 2)
        For j = 0 To UBound(dataArray, 1)
            transposedArray(i, j) = dataArray(j, i)
        Next j
    Next i


    Set targetWorkbook = Workbooks.Open("Totally Real Location")
    Set targetSheet = targetWorkbook.Sheets("DATA & FORMULAS")

    targetSheet.Range("A5:AX1000000").ClearContents
    targetRow = targetSheet.Cells(targetSheet.Rows.count, "A").End(xlUp).Row + 1

    If targetRow < 5 Then targetRow = 5

    targetSheet.Cells(targetRow, 1).Resize(UBound(transposedArray, 1) + 1, UBound(transposedArray, 2) + 1).Value = transposedArray

    targetWorkbook.Save
    targetWorkbook.Close False
    Set rs = Nothing
    Set conn = Nothing
    Set targetSheet = Nothing
    Set targetWorkbook = Nothing

    Exit Sub

ErrorHandler:
    Debug.Print "Error occurred in conn.Open: " & Err.Description
End Sub
3
  • 6
    ADODB uses the US locale (MM/DD/YYYY) by default when interpreting date values from Excel. If your Excel file contains actual date values (not strings), adding Locale Identifier=2057; to your connection string instructs ADODB to interpret those dates using UK-style logic (DD/MM/YYYY). This helps avoid misinterpretation of dates like 03-11-2025, which would otherwise be read as March 11th instead of November 3rd. Commented Nov 6 at 15:02
  • 4
    Why use GetRows and not Range.CopyFromRecordset ? Commented Nov 6 at 17:57
  • @CDP1802 Yeah, thats a better way of doing it, and then it works with Locale as well Commented Nov 7 at 9:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.