Error trying to loop through cells and copy to another sheet

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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