How do you code in VBA to conditionally select and copy certain data from one range to another?

turner737

New Member
Joined
May 12, 2015
Messages
3
Hello,

I have a table of data where i want to select data that meet a certain criteria and copy and paste that to a table in another range.

I.e. I have a table with 6 columns. The first column is date and that is what i am using as a condition. If the date for a given row is in the past week then i want to take the value from columns 2, 3, and 4 from that row and copy paste their values into a table on another sheet.

I've dimensionalised the ranges for each column, but im not sure how to go about going through each row to look at the date and then copy paste the data from the other columns if that date was in the past week.

Can someone assist?!

Much appreciated.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Maybe something like this
This will do the search on the active sheet and copy the range to the last row + 1 on Sheet2 in column "A"

Code:
Sub MM1()
Dim lr As Long, lr2 As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
lr2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 1 Step -1
    If Range("A" & r).Value <= Date And Range("A" & r).Value >= Date - 7 Then
        Range("B" & r & ":D" & r).Copy Sheets("Sheet2").Range("A" & lr2 + 1)
        lr2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
    End If
Next r
End Sub
 
Upvote 0
Many thanks for the quick response. This was very helpful.

I'm working through this now and will provide any questions if they come up.

Thanks again.
 
Upvote 0
You solved my first problem perfectly.

The follow up question being now if i wanted to cherry pick the columns and paste them into different columns on the other sheet how would that be done.

I.e. the data i need isnt actually in consecutive columns.

So, I need to take data from Column A & B posted to Column A & B on the next sheet. But, then i need data from column E to be pasted to column C on sheet 2 and column G on sheet 1 to column E on sheet 2.

Many thanks for your assistance.
 
Upvote 0
Try

Code:
Sub MM1()
Dim lr As Long, lr2 As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
lr2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 1 Step -1
    If Range("A" & r).Value <= Date And Range("A" & r).Value >= Date - 7 Then
        Range("A" & r & ":B" & r).Copy Sheets("Sheet2").Range("A" & lr2 + 1)
        Range("E" & r).Copy Sheets("Sheet2").Range("C" & lr2 + 1)
        Range("G" & r).Copy Sheets("Sheet2").Range("E" & lr2 + 1)
        lr2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
    End If
Next r
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,107
Members
452,302
Latest member
TaMere

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