VBA Copy Row based off cell value

floggingmolly

Board Regular
Joined
Sep 14, 2019
Messages
167
Office Version
  1. 365
Platform
  1. Windows
I am needing some help here. I have a sheet that has dates in column O. I have a code that copies dates based off a date, but I have to enter the date into the code. I would like to be able to enter a date in say cell A1 and then run the code to copy the rows based off the date I enter in A1. Below is the code I have so far. Can anyone help me out?

Code:
Sub CopyRows()
a = Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
 For i = 2 To a
 
 If Worksheets("Sheet1").Cells(i, 15).Value = "4/10/2020" Then
 
 Worksheets("Sheet1").Rows(i).Copy
 Worksheets("Sheet2").Activate
 b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
 Worksheets("Sheet2").Cells(b + 1, 1).Select
 ActiveSheet.Paste


 
 End If
 Next
 


End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try

Note: you do not need to activate sheet2 if you do not want to. Also you do not need the select the cell.
Code:
Sub CopyRows()
a = Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
 
 For i = 2 To a
 
    If Worksheets("Sheet1").Cells(i, 15) = Sheets("Sheet1").Cells(1, 1) Then
    Worksheets("Sheet1").Rows(i).Copy
    Worksheets("Sheet2").Activate
    b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
    Worksheets("Sheet2").Cells(b + 1, 1).Select
    ActiveSheet.Paste
 End If
 Next

End Sub

Without activating sheet and selecting cell
Code:
Sub CopyRows()
a = Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
 
 For i = 2 To a
 
    If Worksheets("Sheet1").Cells(i, 15) = Sheets("Sheet1").Cells(1, 1) Then
    b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
    Worksheets("Sheet1").Rows(i).Copy Worksheets("Sheet2").Cells(b + 1, 1)
    
 End If
 Next

End Sub
 
Upvote 0
Try

Note: you do not need to activate sheet2 if you do not want to. Also you do not need the select the cell.
Code:
Sub CopyRows()
a = Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
 
 For i = 2 To a
 
    If Worksheets("Sheet1").Cells(i, 15) = Sheets("Sheet1").Cells(1, 1) Then
    Worksheets("Sheet1").Rows(i).Copy
    Worksheets("Sheet2").Activate
    b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
    Worksheets("Sheet2").Cells(b + 1, 1).Select
    ActiveSheet.Paste
 End If
 Next

End Sub

Without activating sheet and selecting cell
Code:
Sub CopyRows()
a = Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
 
 For i = 2 To a
 
    If Worksheets("Sheet1").Cells(i, 15) = Sheets("Sheet1").Cells(1, 1) Then
    b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
    Worksheets("Sheet1").Rows(i).Copy Worksheets("Sheet2").Cells(b + 1, 1)
    
 End If
 Next

End Sub


Thank you so much. This worked perfectly. I appreciate your help.
 
Upvote 0

Forum statistics

Threads
1,213,496
Messages
6,113,995
Members
448,539
Latest member
alex78

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