Match Date Search


M

murfyang

Hi there, new to excel forum & also slow learner in VBA. Am doing a
match date search & then copying that range of price with the date
range to another worksheet. used this code as suggested but unable to
work, it end with a "Start date not found" msg.
My date is in column B formatted as dd/mm/yyyy. Have tried adding the
cdate to the startdate & enddate & also changing the 0 to 1 in the
match function, but still the same.
Any one can help, thanks greatly



Code:
--------------------
Option Explicit

Sub FindDates()
Worksheets("Prices").Select

On Error Goto errorHandler
Dim startDate As String
Dim stopDate As String
Dim startRow As Integer
Dim stopRow As Integer
Dim rng1 As Range, rng2 As Range
Dim x As Variant, r As Range

startDate = InputBox("Enter the Start Date: (dd/mm/yyyy)")
If startDate = "" Then End
stopDate = InputBox("Enter the Stop Date: (dd/mm/yyyy)")
If stopDate = "" Then End
startDate = Format(startDate, "dd/mm/yyyy")
stopDate = Format(stopDate, "dd/mm/yyyy")
With Worksheets("Prices")
Set r = .Range(.Cells(1, 2), .Cells(65536, 2).End(xlUp))
End With
x = Application.Match(startDate, r, 0)
If IsNumeric(x) Then
startRow = x
Else
MsgBox "Start date not found"
End
End If

x = Application.Match(stopDate, r, 0)
If IsNumeric(x) Then
stopRow = x
Else
MsgBox "End date not found"
End
End If
 
Ad

Advertisements

T

Tom Ogilvy

The deleted lines were meant to be deleted - don't add them back.


Option Explicit

Sub FindDates()
Worksheets("Prices").Select

On Error Goto errorHandler
Dim startDate As String
Dim stopDate As String
Dim startRow As Integer
Dim stopRow As Integer
Dim rng1 As Range, rng2 As Range
Dim x As Variant, r As Range

startDate = InputBox("Enter the Start Date: (dd/mm/yyyy)")
If startDate = "" Then Exit Sub
stopDate = InputBox("Enter the Stop Date: (dd/mm/yyyy)")
If stopDate = "" Then Exit Sub
With Worksheets("Prices")
Set r = .Range(.Cells(1, 2), .Cells(65536, 2).End(xlUp))
End With
x = Application.Match(clng(cdate(startDate)), r, 0)
If IsNumeric(x) Then
startRow = x
Else
MsgBox "Start date not found"
End
End If

x = Application.Match(clng(cdate(stopDate)), r, 0)
If IsNumeric(x) Then
stopRow = x
Else
MsgBox "End date not found"
End
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top