Copy data of last five days of previous month to another worksheet

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi Guys, I want to copy value of the last five days of the month July in a worksheet named 202107 (2021 July) to another worksheet named 202108 (August). Both sheets have identical calendar range from column C to AL .
The destination range of those 5 days in sheet 202108 is always in C1 to G but that period of days in the previous month sheet 202107 is in last part of the range. I need the code to search the position of those days then copy the data to sheet 202108. Please help to modify mine to make it works. Thanks.
 

Attachments

  • DestRange.png
    DestRange.png
    14.3 KB · Views: 3
  • SrcRange.png
    SrcRange.png
    111.3 KB · Views: 2

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
my code
VBA Code:
Sub Copy_Click8()

Dim startdate As Date, enddate As Date
Dim rng As Range, destRow As Long
Dim shtSrc As Worksheet, shtDest As Worksheet
Dim c As Range

Set shtSrc = Sheets("202107")      'Set Previous Month Sheet as source
Set shtDest = Sheets("202108")     'Set New Month Sheet as destination

Dim lastRowSrc As Long
    lastRowSrc = shtSrc.Cells(Rows.Count, "A").End(xlUp).Row

'Set Range of dates to start

startdate = CDate(ThisWorkbook.Sheets("202108").Range("C1"))  'Use the 5th last date of previous month as start date
enddate = CDate(ThisWorkbook.Sheets("202108").Range("G1"))    'Use the last day of previous month as end date

' Set range to search for dates (last 5 days of previous month)
Set rng = Application.Intersect(shtSrc.Range("H1:AL" & lastRowSrc), shtSrc.UsedRange)


'Look for matching dates in columns H1 to AL-last row

For Each c In rng.Cells
Dim lastRow As Long
 
    lastRow = shtDest.Range("A3").End(xlDown).Row
    
   If (c.Value = startdate And c.Offset(0, 5).Value = enddate) Then
        c.Offset(3, 0).Resize(lastRowSrc, 5).Copy _
        shtDest.Cells(3, 3).Resize(lastRow, 5)   
    End If
Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

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