I copy paste table from Ms SQL Server with datetime values in this format "2025-04-24…
- User
I copy paste table from Ms SQL Server with datetime values in this format "2025-04-24 06:52:00.3930000" to excel sheet. I have a VBA macros with parser function - Function ParseSQLDateTime(s As String) As Variant ' Expects s like "yyyy-mm-dd hh:mm:ss" or "yyyy-mm-ddThh:mm:ss" ' Returns Date if successful, else CVErr
Dim dt As Date Dim datePart As String, timePart As String Dim spacePos As Long On Error GoTo fail ' Find space between date and time spacePos = InStr(s, " ") If spacePos = 0 Then spacePos = InStr(s, "T") ' Handle ISO format with T If spacePos > 0 Then datePart = Left(s, spacePos - 1) timePart = Mid(s, spacePos + 1) Else ' If no time part, just date datePart = s timePart = "00:00:00" End If dt = DateValue(datePart) + TimeValue(timePart) ParseSQLDateTime = dt Exit Functionfail: ParseSQLDateTime = CVErr(xlErrValue) End Function
But in sub macros code i getting an error in this line If Not IsError(ParseSQLDateTime(selRange.Cells(i, 1).Text)) And Not IsError(ParseSQLDateTime(selRange.Cells(i + 1, 1).Text)) Then
- Assistant
- User
My Excel file stop working when i use that macros. I guess theres need some optimization - Function GetValuesForDateInterval(ws As Worksheet, _ startDate As Date, _ endDate As Date) As Double Dim lastRow As Long Dim i As Long Dim currentDate As Date Dim maxDateLEEnd As Date Dim minDateGEStart As Date Dim valForMaxDate As Variant Dim valForMinDate As Variant Dim foundMax As Boolean, foundMin As Boolean
foundMax = False foundMin = False maxDateLEEnd = 0 minDateGEStart = DateSerial(9999, 12, 31) ' very large date valForMaxDate = 0 valForMinDate = 0 lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row For i = 2 To lastRow ' assuming headers in row 1 currentDate = ws.Cells(i, "A").Value If IsDate(currentDate) Then If currentDate <= endDate Then If (Not foundMax) Or (currentDate > maxDateLEEnd) Then maxDateLEEnd = currentDate valForMaxDate = ws.Cells(i, "B").Value foundMax = True End If End If If currentDate >= startDate Then If (Not foundMin) Or (currentDate < minDateGEStart) Then minDateGEStart = currentDate valForMinDate = ws.Cells(i, "B").Value foundMin = True End If End If End If Next i ' Validate the values are numeric, else treat as zero If Not IsNumeric(valForMaxDate) Then valForMaxDate = 0 If Not IsNumeric(valForMinDate) Then valForMinDate = 0 ' Return the sum of both values for example GetValuesForDateInterval = CDbl(valForMaxDate) - CDbl(valForMinDate) - Assistant
- User
I want to add break from loop if both foundMin and foundMax equals true
- Assistant
To exit the loop early as soon as you have both
foundMinandfoundMaxset toTrue, you can simply add a check within the loop and useExit Forto break out.