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

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
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,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

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