I've been racking my brain over this for a whole day now and cannot figger out why my VBA FIND command is not coming up with results. Here's what I have:
It's an XL workbook used to compare Electricity prices for suppliers who have Peak and non-Peak charges.
The worksheet "Electricity- Peak-Off Peak" is the main sheet and the worksheet "AusNetDownload" is the raw electricity usage data over a number of months.
In worksheet "Electricity- Peak-Off Peak", cell $I$13, I have a UDM (User Defined Macro) which takes the start & end dates from S1:T1 ("24/11/2021" & "21/02/2022"), and the start and end times $G$13:$H$13 ("15:00 - 15:30" & "20:30 - 21:00") and finds the range of values between the dates and times specified, and will then sum the values (this part has not been started yet).
My problem is this line of code:
When the UDM fires, I see the following in the Immediate Window:
Can anyone offer any help into why this is happening?
Link to workbook:
For those who do not want to open the workbook:
Cell $I$13: =GetPeakOffPeakUsage(G13,H13)
The VBA code:
Thanks for helping out.
It's an XL workbook used to compare Electricity prices for suppliers who have Peak and non-Peak charges.
The worksheet "Electricity- Peak-Off Peak" is the main sheet and the worksheet "AusNetDownload" is the raw electricity usage data over a number of months.
In worksheet "Electricity- Peak-Off Peak", cell $I$13, I have a UDM (User Defined Macro) which takes the start & end dates from S1:T1 ("24/11/2021" & "21/02/2022"), and the start and end times $G$13:$H$13 ("15:00 - 15:30" & "20:30 - 21:00") and finds the range of values between the dates and times specified, and will then sum the values (this part has not been started yet).
My problem is this line of code:
VBA Code:
Dim vRangeDateStart As Range
Dim vRangeDateEnd As Range
Debug.Print vDateColumnRange.Parent.Name
Debug.Print vDateColumnRange.Address
Debug.Print vDateColumnRange.Cells(586, 1).Value
Debug.Print vDateColumnRange.Cells(1, 1).Value
Debug.Print vDateColumnRange.Cells(2, 1).Value
' Find the Row numbers of the Start Date & End Date.
With vDateColumnRange
Set vRangeDateStart = vDateColumnRange.Find(vDateFrom.Value)
Set vRangeDateEnd = vDateColumnRange.Find(vDateTo.Value, After:=vDateColumnRange.Cells(1), LookIn:=xlValues)
End With
When the UDM fires, I see the following in the Immediate Window:
So the code seems OK, it seems to have the correct references, however, the vRangeDateStart & vRangeDateEnd variables always come out blank???AusNetDownload
$A$1:$A$731
24/11/2021
DATE
19/04/2020
Can anyone offer any help into why this is happening?
Link to workbook:
ExcelHelp - Google Drive
drive.google.com
For those who do not want to open the workbook:
Cell $I$13: =GetPeakOffPeakUsage(G13,H13)
The VBA code:
VBA Code:
Function GetPeakOffPeakUsage(vTimeStart As String, vTimeEnd As String)
' Define worksheet variables.
Dim vCurrentWorksheet As Worksheet
Set vCurrentWorksheet = ActiveSheet
Dim wUsageHistory As Worksheet
Set wUsageHistory = Worksheets("AusNetDownload")
Dim vDateFrom As Range, vDateTo As Range
Set vDateFrom = Range("DateFrom")
Set vDateTo = Range("DateTo")
'
' Find the column numbers for the time range.
'
Dim vRangeTimeStart As Range ' The range where the Time Start value is found.
Dim vRangeTimeEnd As Range ' The range where the Time End value is found.
Dim vColumnTimeStart As Integer ' The column of the range where the Time Start value is found.
Dim vColumnTimeEnd As Integer ' The column of the range where the Time End value is found.
' Search the Time header for the start time & end time.
With wUsageHistory.Range("B1:AW1")
Set vRangeTimeStart = .Find(vTimeStart, After:=.Cells(1))
Set vRangeTimeEnd = .Find(vTimeEnd, After:=.Cells(1))
End With
vColumnTimeStart = vRangeTimeStart.Column ' Retrieve the column number of the Start Time.
vColumnTimeEnd = vRangeTimeEnd.Column ' Retrieve the column number of the End Time.
'
' Find the row numbers for the date range.
'
Dim vRowDateStart As Range
Dim vRowDateEnd As Range
' Find the last column of the date range. This can vary.
Dim vLastDateRow As Integer
vLastDateRow = wUsageHistory.Range("A1").End(xlDown).Row
Set vDateColumnRange = wUsageHistory.Range("A1:A" & vLastDateRow)
Dim vRangeDateStart As Range
Dim vRangeDateEnd As Range
Debug.Print vDateColumnRange.Parent.Name
Debug.Print vDateColumnRange.Address
Debug.Print vDateColumnRange.Cells(586, 1).Value
Debug.Print vDateColumnRange.Cells(1, 1).Value
Debug.Print vDateColumnRange.Cells(2, 1).Value
' Find the Row numbers of the Start Date & End Date.
With vDateColumnRange
Set vRangeDateStart = vDateColumnRange.Find(vDateFrom.Value)
Set vRangeDateEnd = vDateColumnRange.Find(vDateTo.Value, After:=vDateColumnRange.Cells(1), LookIn:=xlValues)
End With
' Debug.Print vRangeDateStart.Address
' Debug.Print vRangeDateEnd.Address
vRowTimeStart = vRangeTimeStart.Row ' Retrive the row number of the Start Date.
vRowTimeEnd = vRangeTimeEnd.Row ' Retrieve the row number of the End Date.
' The next set of lines will sum the Kilowatt values for the date range and time range.
End Function
Thanks for helping out.