VBA Copy Row based off cell value

floggingmolly

Board Regular
Joined
Sep 14, 2019
Messages
69
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
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Scott T

Well-known Member
Joined
Dec 14, 2016
Messages
2,612
Office Version
365, 2016
Platform
Windows
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
 

floggingmolly

Board Regular
Joined
Sep 14, 2019
Messages
69
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,267
Messages
5,485,765
Members
407,514
Latest member
viego99

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top