Need help with a macro to select all rows with yesterdays date

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
194
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

I have a worksheet(Sheet1) with many rows of data that are presorted so that the most current date which is yesterday would start in Row 2(Row 1 is headers).
What I need is a macro that will SELECT ALL rows with yesterdays date([Today()-1]). The number of rows varies. It could be 3 yesterday and 4 today, etc.

From there I will need to copy the SELECTED rows and paste in Cell A1 in Sheet2.
The code below works but only selects the first row with yesterdays date.

Thanks for any help.
D



End Sub
VBA Code:
Sub FindYesterdaysDateRow()

Dim Rng     As Range
Dim YesterD As Date

YesterD = DateAdd("d", -1, Date)
Set Rng = Range("A:A").Find(What:=YesterD, LookIn:=xlValues, LookAt:=xlWhole)

Range("A" & Rng.Row & ":V" & Rng.Row).Select
 
OK, this version is more robust. See where I've indicated where you can change the sheet names to whatever you want. Also includes an error trap if no records found for yesterday.

VBA Code:
Option Explicit
Sub CopyYesterday2()
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("SHIFT_IMPORT")    '<~~ Change the sheet names here
Set ws2 = Worksheets("SHIFT_EXPORT ")   '<~~ And here

    With ws1.Range("A1").CurrentRegion
        .AutoFilter 1, 2, 11
        If .Cells(Rows.Count, 1).End(xlUp).Row > 1 Then
            .Offset(1).Resize(.Rows.Count - 1).Copy ws2.Range("A1")
        Else
            MsgBox "No records found for yesterday"
        End If
        .AutoFilter
    End With
End Sub
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
OK, this version is more robust. See where I've indicated where you can change the sheet names to whatever you want. Also includes an error trap if no records found for yesterday.

VBA Code:
Option Explicit
Sub CopyYesterday2()
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("SHIFT_IMPORT")    '<~~ Change the sheet names here
Set ws2 = Worksheets("SHIFT_EXPORT ")   '<~~ And here

    With ws1.Range("A1").CurrentRegion
        .AutoFilter 1, 2, 11
        If .Cells(Rows.Count, 1).End(xlUp).Row > 1 Then
            .Offset(1).Resize(.Rows.Count - 1).Copy ws2.Range("A1")
        Else
            MsgBox "No records found for yesterday"
        End If
        .AutoFilter
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,050
Messages
6,122,868
Members
449,097
Latest member
dbomb1414

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