I have VBA macros with that function Function ParseSQLDateTime(s As String) As Variant '…
- User
I have VBA macros with that function Function ParseSQLDateTime(s As String) As Variant ' Expects s like "yyyy-mm-dd hh:mm:ss(.nnnnnnn)" or "yyyy-mm-ddThh:mm:ss(.nnnnnnn)" ' Returns Date if successful, else CVErr
Dim dt As Date Dim datePart As String, timePart As String Dim spacePos As Long Dim dotPos 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 ' Remove fractional seconds if present dotPos = InStr(timePart, ".") If dotPos > 0 Then timePart = Left(timePart, dotPos - 1) End If dt = DateValue(datePart) + TimeValue(timePart) ParseSQLDateTime = dt Exit Functionfail: ParseSQLDateTime = CVErr(xlErrValue) End Function It for usage datetime format from SQL table. And in macros code i have a lines -Sub ProcessDateIntervalsFromSelection() Dim selRange As Range Dim wsData As Worksheet, wsData1 As Worksheet, wsData2 As Worksheet, wsData3 As Worksheet, wsData4 As Worksheet, wsData5 As Worksheet, wsData6 As Worksheet, wsData7 As Worksheet, wsData8 As Worksheet, wsData9 As Worksheet Dim i As Long Dim endDate As Date Dim startDate As Date Dim outputCell As Range Dim result As Double
' Set your data sheet name here Set wsData = ThisWorkbook.Worksheets("INF10") ' change to your data sheet name Set wsData1 = ThisWorkbook.Worksheets("INF11") Set wsData2 = ThisWorkbook.Worksheets("INF12") Set wsData3 = ThisWorkbook.Worksheets("INF13") Set wsData4 = ThisWorkbook.Worksheets("INF14") Set wsData5 = ThisWorkbook.Worksheets("INF15") Set wsData6 = ThisWorkbook.Worksheets("INF16") Set wsData7 = ThisWorkbook.Worksheets("INF17") Set wsData8 = ThisWorkbook.Worksheets("INF18") Set wsData9 = ThisWorkbook.Worksheets("INF19") ' Get the selected cells Set selRange = Selection ' Check if selection is at least two cells to process pairs If selRange.Cells.Count < 2 Then MsgBox "Select at least two cells vertically in one column containing datetime values.", vbExclamation Exit Sub End If ' Make sure selection is in one column (you said one column) If selRange.Columns.Count > 1 Then MsgBox "Please select cells in only one column.", vbExclamation Exit Sub End If Application.ScreenUpdating = False ' Loop through selected cells' pairs: ' For rows from first cell to second last cell: ' - current cell as EndDate ' - next cell as StartDate For i = 1 To selRange.Rows.Count - 1 ' Read dates - validate if they are dates If Not IsError(ParseSQLDateTime(selRange.Cells(i, 1).Value2)) And Not IsError(ParseSQLDateTime(selRange.Cells(i + 1, 1).Value2)) Then endDate = ParseSQLDateTime(selRange.Cells(i, 1).Text) startDate = ParseSQLDateTime(selRange.Cells(i + 1, 1).Text)Does it matter that function returning variant type, but endDate and startDate are Date type
- Assistant
- User
But my function returns DateValue(datePart) + TimeValue(timePart)
- Assistant
Yes, your function returns a Date internally, but since the function return type is , it can hold either: