Extracting data based on Date.

fluxcapacitr609

New Member
Joined
Feb 21, 2018
Messages
24
Im trying to to extract data and push it to another sheet based on date. The code I have works but only does column A and B when C and D has information in it also. Any help would be greatly appreciated thank you.

Sub extractDataBasedOnDate()
Dim lastrow As Long
Dim erow As Long
Dim i As Long


lastrow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row
Sheet2.Select
Range("A1").Select


For i = 2 To lastrow


mydate = Cells(i, 1)
If mydate >= "2018-02-01" And mydate <= "2018-02-29" Then
erow = Sheet4.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Range(Cells(i, 1), Cells(i, 2)).Copy Destination:=Sheet4.Cells(erow, 1)
End If

Next i



End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi & welcome to MrExcel
If you want to copy cols A:D then try
Code:
Range(Cells(i, 1), Cells(i, 4)).Copy Destination:=Sheet4.Cells(erow, 1)
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
To copy to E1 use
Code:
Range(Cells(i, 1), Cells(i, 4)).Copy Destination:=Sheet4.Range("E1")
 
Upvote 0
It's actually copying all the rows, just overwriting them.
Try
Code:
Range(Cells(i, 1), Cells(i, 4)).Copy Destination:=Sheet4.Range("E"&erow)
 
Upvote 0
Step through the code using F8 & check that the value of erow is changing
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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