Error trying to loop through cells and copy to another sheet

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to loop through the non-blank cells in column A of a worksheet and then copy each of those values to another worksheet by using this;

Code:
'Import items
Set ws = DestFile.Sheets("HandoverSummary")
With DestFile.Sheets("HandoverItems")
Lastrow = Range("A65536").End(xlUp).Row
For Each cell In Range("A2:A" & Lastrow)
If cell <> "" Then
iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ws.Cells(iRow, 1).Value = .Range("A" & cell).Value
End If
Next
End With

It fails on this line and I don't know why;

Code:
iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

I know that I can use a straight copy and paste but I will need to avoid that because once I've got this working I will need to add an IF statement if the value in the cell is a certain string.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
If you're working with both old & new versions of xl try
Code:
iRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0).Row

That said there are a few other potential problems. Try
Code:
Set WS = DestFile.Sheets("HandoverSummary")
With DestFile.Sheets("HandoverItems")
   LastRow = .Range("A65536").End(xlUp).Row
   For Each cell In .Range("A2:A" & LastRow)
      If cell <> "" Then
         irow = WS.Cells(WS.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
         WS.Cells(irow, 1).Value = cell.Value
      End If
   Next
End With
 
Last edited:
Upvote 0
It also appears that you are using "With" a bit oddly.
Typically, you would see a structure like:
Code:
With DestFile.Sheets("HandoverItems")
    Lastrow = .Range("A65536").End(xlUp).Row
    ...
End With
Note the period in front of Range, indicating that it goes with the WITH statement.
Otherwise, it really serves no purpose to use WITH.
 
Last edited:
Upvote 0
Thanks guys - I'll have a play with your suggestions.
 
Upvote 0

Forum statistics

Threads
1,215,456
Messages
6,124,939
Members
449,197
Latest member
k_bs

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