Copy Row Error

johnnyb5

Board Regular
Joined
Dec 23, 2014
Messages
89
I'm trying to copy the entire rows from a spreadsheet called "RAW" to another blank Spreadsheet named "FMX DUIC" The rows that I'm trying to copy are based on the value in the Cell in column "A" (Currently I have three criteria but I may add more in the future). The code appears to have an error(s) in the line that presents the instruction to "cell.entirerow copy"(as I can't get past that point). Would anyone have suggestions on how to fix the code??

ActiveSheet.Name = "RAW"
ActiveWindow.Zoom = 80
Sheets("RAW").Activate
Sheets(1).Select
Worksheets.Add
ActiveSheet.Name = "FMX DUIC"
ActiveWindow.Zoom = 80
Sheets("RAW").Select
Range("A1").Select

Dim bottomC As Long

bottomC = Range("A" & Rows.Count).End(xlUp).Row
Dim cell As Range
For Each cell In Range("A2:A" & bottomC)
If cell.Value = "W0H989" Or cell.Value = "W1D241" Or cell.Value = "W1D261" Then
cell.EntireRow.Copy Sheets("FMX DUIC").Cells(r, 1)
r = r + 1
End If
Next cell

Thanks,
JB
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Would anyone have suggestions on how to fix the code??
When you run the code, the initial value of r is zero. There is no Cells(0,1) on the destination sheet.
Set r=1 before the loop or move the r = r + 1 just above the copy line.
 
Upvote 0
Awesome Peter,

I couldn't get the "Set" to work but I took your idea and modified the "r" in the first "Cells" command to "1+r" and it copied everything that I needed it to!!!

cell.EntireRow.Copy Sheets("FMX DUIC").Cells("1" + r, 1)

Thanks, Thanks, and Thanks Again,
JB
 
Upvote 0
I couldn't get the "Set" to work ...
The 'Set' was not meant to be a vba code word, just English. :)
So this is what I meant by that bit
Rich (BB code):
Dim cell As Range
r = 1    <- This line inserted
For Each cell In Range("A2:A" & bottomC)
If cell.Value = "W0H989" Or cell.Value = "W1D241" Or cell.Value = "W1D261" Then
cell.EntireRow.Copy Sheets("FMX DUIC").Cells(r, 1)
r = r + 1
End If
Next cell

0r


Rich (BB code):
Dim cell As Range
For Each cell In Range("A2:A" & bottomC)
If cell.Value = "W0H989" Or cell.Value = "W1D241" Or cell.Value = "W1D261" Then
r = r + 1                                                  <- These lines swapped
cell.EntireRow.Copy Sheets("FMX DUIC").Cells(r + 1, 1)     <- These lines swapped
End If
Next cell



.. but I took your idea and modified the "r" in the first "Cells" command to "1+r" and it copied everything that I needed it to!!!

cell.EntireRow.Copy Sheets("FMX DUIC").Cells("1" + r, 1)

Thanks, Thanks, and Thanks Again,
JB
Not that important but you shouldn't need those red quote marks.
 
Upvote 0
Pete,
I worked with your suggestions over the weekend and they were right on the mark. I removed my "addition" solution and decided to use your "line swap" solution. After trying the different options I added the criteria for further requirements and didn't have any problems searching across two columns for 14 criteria. The macro is excellent!

Thanks Again!!!
JB
 
Upvote 0
Pete,
I worked with your suggestions over the weekend and they were right on the mark. I removed my "addition" solution and decided to use your "line swap" solution. After trying the different options I added the criteria for further requirements and didn't have any problems searching across two columns for 14 criteria. The macro is excellent!

Thanks Again!!!
JB
Good news. Glad you have made progress. Thanks for letting us know.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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