Vba to select rows based on date

Wither125

New Member
Joined
May 16, 2016
Messages
5
Hello,

I want to select the rows based on the date and past it in other sheet.

for ex: lets take today's date 24/06/2016. So now select the rows that is < 8 days and >8 days from 24/06/2016 on wards and paste it in the other sheet.

Below is pic.

ipt.png



Thank You!

Regards
Wither
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
This could probably be done faster with a filter, but this should work.

Code:
Sub eightaway()
Dim c As Range
    With ActiveSheet
        For Each c In .Range("B2", .Cells(Rows.Count, 2).End(xlUp))
            If c >= Date + 8 Or c < Date - 8 Then
                c.EntireRow.Copy Sheets(2).Cells(Rows.Count, 1).End(xlUp)(2)
            End If
        Next
    End With
End Sub
 
Upvote 0
Hello,

I want to select the rows based on the date and past it in other sheet.

for ex: lets take today's date 24/06/2016. So now select the rows that is < 8 days and >8 days from 24/06/2016 on wards and paste it in the other sheet.

Below is pic.

ipt.png



Thank You!

Regards
Wither

Just saw the previous post...ignore this if question answered.

Wither,
Assumes your data is on Sheet 1 and you want to copy data to Sheet 2 if the 'Effective_Date' falls outside the 16 days centered on the value in C1. Make sure to format your dates as numbers, not as text. If the date aligns to the left side of the cell it is text...if it aligns to the right then it is a number. Your date is a 'Custom' format: 'dd/mm/yyyy'
I provided a line to delete the copied rows from Sheet 1. Just uncomment, ie. remove the single quote at the beginning of that line (see line in red font below) if you want to delete those rows from Sheet 1.
Copy the subroutine into a module (Alt+F11 to open the VBE window, Paste, close the VBE window, and save as a macro-enabled workbook). You should probably do that in a copy of your workbook so you don't lose any data. Then press Alt+F8 and select ' CopyToSheet2', then press 'Run'.
Perpa

Code:
Sub CopyToSheet2()
Dim LastRow, Last2Row, rw As Long

LastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
With Sheets("Sheet1")
    For rw = LastRow To 2 Step -1
       If .Cells(rw, "B") < .Cells(1, "C") - 8 Or .Cells(rw, "B") > .Cells(1, "C") + 8 Then
            Last2Row = Sheets("Sheet2").UsedRange.Rows.Count + 1
            Sheets("Sheet2").Cells(Last2Row, "A") = .Cells(rw, "A")
            Sheets("Sheet2").Cells(Last2Row, "B") = .Cells(rw, "B")
            
            [COLOR="#FF0000"]'.Cells(rw, "B").EntireRow.Delete    'Uncomment this line if you want to delete the row from Sheet 1[/COLOR]
       End If
    Next rw
End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,504
Messages
6,125,185
Members
449,213
Latest member
Kirbito

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