A quicker way of looping

Pauljj

Well-known Member
Joined
Mar 28, 2004
Messages
2,047
I am using a FOR, NEXT looking, checking through a list of 5,000 names to see if the corresponding cell is populated with any data. if it does it's copying it to another workbook, going back to the original.....etc etc , it seems that doing this check and copying and pasting is taking a substantial amount of time, is there anyway of speeding this up.

This is the main section

Code:
For i = 6 To lastrow



    wbo.Activate

    If Cells(i, 265) <> "" Then
    
    Cells(i, 265).Copy
    
    wbn.Activate
    
    newlastrow = Cells(Rows.Count, 1).End(xlUp).Row
    
    Cells(newlastrow + 1, 2).Activate
    
    ActiveCell.PasteSpecial xlPasteValues
    

    wbo.Activate
    
    Cells(i, 112).Copy
    
    wbn.Activate
    
    Cells(newlastrow + 1, 1).Activate
    
    ActiveCell.PasteSpecial xlPasteValues
    
    End If
    
    
Next i
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
How about
Code:
   For i = 6 To LastRow
      With wbo
         If .Cells(i, 265) <> "" Then
            WBn.Cells(Rows.Count, 1).End(xlUp).Offset(1, 1).Value = .Cells(i, 265).Value
            WBn.Cells(Rows.Count, 1).End(xlUp).Offset(1).Value = .Cells(i, 112).Value
         End If
      End With
   Next i
 
Upvote 0
As a matter of interest, what is in col 265? Is it hard values, or formulae?
 
Upvote 0
In that case here's a non-looping approach
Code:
   With wbo.Columns(265)
      .SpecialCells(xlConstants).Copy wbn.Cells(Rows.Count, 1).End(xlUp).Offset(1, 1)
      .SpecialCells(xlConstants).Offset(, -153).Copy wbn.Cells(Rows.Count, 1).End(xlUp).Offset(1)
   End With
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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