Function GetValuesForDateInterval(ws As Worksheet, _ startDate As Variant, _ endDate As…
- User
Function GetValuesForDateInterval(ws As Worksheet, _ startDate As Variant, _ endDate As Variant) As Double Dim lastRow As Long Dim i As Long Dim dataArr As Variant Dim currentDate As Variant Dim val As Variant
Dim valForMaxDate As Variant Dim valForMinDate As Variant Dim foundMax As Boolean, foundMin As Boolean ' Validate input dates If startDate > endDate Then GetValuesForDateInterval = 0 Exit Function End If lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row If lastRow < 2 Then GetValuesForDateInterval = 0 Exit Function End If ' Read Dates and Values into Variant Array (better performance) ' Assumes Date in column A, Value in column B, from row 2 down to lastRow dataArr = ws.Range("A2:B" & lastRow).Value valForMaxDate = 0 valForMinDate = 0 foundMax = False foundMin = False For i = 1 To UBound(dataArr, 1) currentDate = dataArr(i, 1) val = dataArr(i, 2) If IsDate(currentDate) Then ' Handle max date = endDate If currentDate <= endDate Then If (Not foundMax) Then valForMaxDate = val foundMax = True End If End If ' Handle min date = startDate If currentDate < startDate Then If (Not foundMin) Then valForMinDate = val foundMin = True End If End If End If ' Exit early if both found If foundMin And foundMax Then Exit For Next i If Not IsNumeric(valForMaxDate) Then valForMaxDate = 0 If Not IsNumeric(valForMinDate) Then valForMinDate = 0 ' Return difference GetValuesForDateInterval = CDbl(valForMaxDate) - CDbl(valForMinDate)End Function VBA code. I need to add to startDate and endDate 3 hours
- Assistant
If you want to add 3 hours to
startDateandendDateinside your VBA function, you can do so by adding 3 hours expressed as a fraction of a day to the date.In VBA, dates are stored as doubles where 1 = 1 day, so 3 hours = 3 / 24 = 0.125.
You can simply add 0.125 to the
startDateandendDatevariants to shift them by 3 hours.
How to modify your code
Right after validating
startDateandendDate, add these lines:' Add 3 hours (3/24) to each date startDate = CDate(startDate) + 3 / 24 endDate = CDate(endDate) + 3 / 24
Full snippet with this addition:
' Validate input dates If startDate > endDate Then GetValuesForDateInterval = 0 Exit Function End If ' Add 3 hours (3/24) to each date startDate = CDate(startDate) + 3 / 24 endDate = CDate(endDate) + 3 / 24 - User
I need to add before calculations in function
- Assistant
- User
Function GetValuesForDateInterval(ws As Worksheet, _ startDate As Variant, _ endDate As Variant) As Double Dim lastRow As Long Dim i As Long Dim dataArr As Variant Dim currentDate As Variant Dim val As Variant
Dim valForMaxDate As Variant Dim valForMinDate As Variant Dim foundMax As Boolean, foundMin As Boolean ' Validate input dates If startDate > endDate Then GetValuesForDateInterval = 0 Exit Function End If startDate = CDate(startDate) + 3 / 24 endDate = CDate(endDate) + 3 / 24 lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row If lastRow < 2 Then GetValuesForDateInterval = 0 Exit Function End If ' Read Dates and Values into Variant Array (better performance) ' Assumes Date in column A, Value in column B, from row 2 down to lastRow dataArr = ws.Range("A2:B" & lastRow).Value valForMaxDate = 0 valForMinDate = 0 foundMax = False foundMin = False For i = 1 To UBound(dataArr, 1) currentDate = dataArr(i, 1) val = dataArr(i, 2) If IsDate(currentDate) Then ' Handle max date = endDate If currentDate <= endDate Then If (Not foundMax) Then valForMaxDate = val foundMax = True End If End If ' Handle min date = startDate If currentDate < startDate Then If (Not foundMin) Then valForMinDate = val foundMin = True End If End If End If ' Exit early if both found If foundMin And foundMax Then Exit For Next i If Not IsNumeric(valForMaxDate) Then valForMaxDate = 0 If Not IsNumeric(valForMinDate) Then valForMinDate = 0 ' Return difference GetValuesForDateInterval = CDbl(valForMaxDate) - CDbl(valForMinDate) - Assistant
To make the cell in column A red in the worksheet when you find or inside your loop, you need to identify the of the current cell you are processing in the worksheet to set its .