VBA - First and last row between dates

eastrand

Board Regular
Joined
Nov 27, 2013
Messages
101
Office Version
  1. 365
Platform
  1. Windows
I have a data set similar to the one below. In VBA, how do I find the first row and the last row in between two dates. In this example, between 01/10/2022 - 01/14/2022

Book2
ABCD
1DateProductQtySales
21/3/2022Prod316$ 70.72
31/3/2022Prod124$ 106.08
41/3/2022Prod219$ 83.98
51/3/2022Prod214$ 61.88
61/4/2022Prod218$ 79.56
71/4/2022Prod323$ 101.66
81/4/2022Prod116$ 70.72
91/10/2022Prod218$ 79.56
101/10/2022Prod323$ 101.66
111/10/2022Prod223$ 101.66
121/10/2022Prod310$ 44.20
131/11/2022Prod321$ 92.82
141/11/2022Prod17$ 30.94
151/11/2022Prod123$ 101.66
161/11/2022Prod37$ 30.94
171/12/2022Prod120$ 88.40
181/12/2022Prod114$ 62.22
191/13/2022Prod214$ 59.99
201/13/2022Prod313$ 57.75
211/14/2022Prod113$ 55.52
221/14/2022Prod212$ 53.28
231/15/2022Prod312$ 51.05
241/15/2022Prod211$ 48.81
251/16/2022Prod311$ 46.58
261/16/2022Prod310$ 44.35
271/17/2022Prod110$ 42.11
Sheet1
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try this:

VBA Code:
Sub finddates()
  Dim f As Range
  Dim first As Long, last As Long
  Dim d1 As Date, d2 As Date
  
  d1 = CDate("10/01/2022")
  d2 = CDate("14/01/2022")
  Set f = Range("A:A").Find(d1, , xlFormulas, xlWhole, , xlNext)
  If Not f Is Nothing Then
    first = f.Row
    Set f = Range("A:A").Find(d2, , xlFormulas, xlWhole, , xlPrevious)
    If Not f Is Nothing Then
      last = f.Row
      MsgBox "First row : " & first & ". Last row : " & last
    End If
  End If
End Sub
 
Upvote 0
Thanks @DanteAmor. This works if it finds both dates but sometimes there is not always data on the start or finish date. Is there a way the find the row of the first date on or after the start date and on or before the finished date? Sorry for not explaining in advance.
 
Upvote 0
I think I got it. Below if anyone else needs it. Feel free to approve upon.

VBA Code:
Sub test3()
Dim rng As Range
Dim SD As Variant
Dim ED As Variant
Dim rowstartnum As Long
Dim rowendnum As Long

 
Set rng = Range("P:P")
SD = 44571 'Start Date
For Each cll In rng
If cll.Value >= SD And IsDate(cll.Value) Then
    rowstartnum = cll.Row
    Exit For
End If
Next

ED = 44575 'End Date
For Each cll In rng
If cll.Value >= SD And cll.Value <= ED And IsDate(cll.Value) Then
   If cll.Row > rowendnum Then rowendnum = cll.Row
End If
Next

MsgBox rowstartnum & "  " & rowendnum
End Sub
 
Upvote 0
Whitout loop

VBA Code:
Sub finddates()
  Dim f As Range
  Dim first As Long, last As Long
  
  Set f = Range("A:A").Find(CDate("10/01/2022"), , xlFormulas, xlWhole, , xlNext)
  If Not f Is Nothing Then first = f.Row
  Set f = Range("A:A").Find(CDate("14/01/2022"), , xlFormulas, xlWhole, , xlPrevious)
  If Not f Is Nothing Then last = f.Row
  MsgBox "First row : " & first & ". Last row : " & last
End Sub
 
Upvote 0
Solution
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,684
Members
449,116
Latest member
HypnoFant

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top