For Each repeating on same data

Luke777

Board Regular
Joined
Aug 10, 2020
Messages
243
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have a workbook from which I'm trying to copy the entire row IF the date matches the date I'm working with. Below is a section of my code - certainly not the best way to do this, but I've been trying to iterate and teach myself what and how it works and improve it as I go along. However, this particular bit of code only copies the same row (there's multiple rows with the matching date) over and over again instead of moving through the column (column A is where the dates are).

With wb2
For Each Cell In ws2.Range("A1:A10000")
If Cell.Value = activeDate Then Cell.EntireRow.Copy Destination:=ws3.Range("A:A")
Next
End With

Any help here would be appreciated :)

Thanks all!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
i didn't test it, but something like this.
VBA Code:
If Cell.Value = activeDate Then Cell.EntireRow.Copy Destination:=ws3.Range("A" & rows.count).end(xlup).offset(1)
 
Upvote 0
Solution
This needs to be defined as a cell not a column
Maybe
VBA Code:
Dim lr As Long
With wb2
lr = ws3.Cells(Rows.Count, "A").End(xlUp).Row + 1
For Each Cell In ws2.Range("A1:A10000")
If Cell.Value = activeDate Then 
Cell.EntireRow.Copy Destination:=ws3.Range("A" & lr)
lr = lr + 1
end if
Next
End With
 
Upvote 0
i didn't test it, but something like this.
VBA Code:
If Cell.Value = activeDate Then Cell.EntireRow.Copy Destination:=ws3.Range("A" & rows.count).end(xlup).offset(1)
That worked perfectly thank you - seems confusing that changing the destination part affects the function of the code in that way
 
Upvote 0
i didn't test it, but something like this.
VBA Code:
If Cell.Value = activeDate Then Cell.EntireRow.Copy Destination:=ws3.Range("A" & rows.count).end(xlup).offset(1)
Not sure if we're meant to ask follow up questions or if I'd be better off starting a new thread, but I was wondering if there was an easy method that replaces Cell.EntireRow.copy with specific columns from that row? e.g, instead of all of row, just A, D,E and P from that row.

Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,003
Members
448,935
Latest member
ijat

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