carlspywell
asked on
Excel VBA find the row number for a start date and end date in range
If i had a range of dates in column B (sorted in date order)
what is the easiest way to find the row number for an input boxes start date and end date ?
Thanks
what is the easiest way to find the row number for an input boxes start date and end date ?
Thanks
ASKER
To clarify i will have an input box asking for a start date and end date. i then need to find the 1st row with the start date and the last row with the end date.
here you go
Sub test()
Dim dt1 As Date, dt2 As Date
Dim str1 As String, str2 As String
Dim rng As Range
dt1 = InputBox("enter start date")
dt2 = InputBox("enter end date")
For Each rng In Range("B1:B100")
If CDate(rng.Value) = dt1 Then str1 = "Start date of " & dt1 & " is in row " & rng.Row
If CDate(rng.Value) = dt2 Then str2 = "End date of " & dt2 & " is in row " & rng.Row
Next rng
MsgBox str1 & vbCrLf & str2
End Sub
Sub test()
Dim dt1 As Date, dt2 As Date
Dim str1 As String, str2 As String
Dim rng As Range
dt1 = InputBox("enter start date")
dt2 = InputBox("enter end date")
For Each rng In Range("B1:B100")
If CDate(rng.Value) = dt1 Then str1 = "Start date of " & dt1 & " is in row " & rng.Row
If CDate(rng.Value) = dt2 Then str2 = "End date of " & dt2 & " is in row " & rng.Row
Next rng
MsgBox str1 & vbCrLf & str2
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Wobbled, instead of looping you can use
str1 = "Start date of " & dt1 & " is in row " & worksheetfunction.match(dt 1,range("B :B"),0)
Saqib
str1 = "Start date of " & dt1 & " is in row " & worksheetfunction.match(dt
Saqib
Hi Ssagibh you are correct - the only reason I went for the loop was to pick up the first and last instances. If I'm right, the match function will only find the first instance?
so a better way of writing my code would be
Sub test()
Dim dt1 As Date, dt2 As Date
Dim str1 As String, str2 As String
Dim rng As Range
dt1 = InputBox("enter start date")
dt2 = InputBox("enter end date")
str1 = "Start date of " & dt1 & " is in row " & worksheetfunction.match(dt 1,range("B :B"),0)
For Each rng In Range("B1:B100")
If CDate(rng.Value) = dt2 Then str2 = "End date of " & dt2 & " is in row " & rng.Row
Next rng
MsgBox str1 & vbCrLf & str2
End Sub
so a better way of writing my code would be
Sub test()
Dim dt1 As Date, dt2 As Date
Dim str1 As String, str2 As String
Dim rng As Range
dt1 = InputBox("enter start date")
dt2 = InputBox("enter end date")
str1 = "Start date of " & dt1 & " is in row " & worksheetfunction.match(dt
For Each rng In Range("B1:B100")
If CDate(rng.Value) = dt2 Then str2 = "End date of " & dt2 & " is in row " & rng.Row
Next rng
MsgBox str1 & vbCrLf & str2
End Sub
ASKER
I get the Error "Unable to get the match property of the worksheetFunction class" on the line
str1 = "Start date of " & dt1 & " is in row " & WorksheetFunction.Match(dt 1, Range("B:B"), 0)
str1 = "Start date of " & dt1 & " is in row " & WorksheetFunction.Match(dt
ASKER
Attache is the sample from my workbook.
I looked for start date = 02/01/2008 and end date = 02/01/2008
but get error.
Carl
test.xlsm
I looked for start date = 02/01/2008 and end date = 02/01/2008
but get error.
Carl
test.xlsm
ASKER
@ssaqibh This throws up an error as the loop suggested works fine.
Cheers
Cheers
WorksheetFunction.Min(Rang
and
WorksheetFunction.Max(Rang
Saqib