Making a If True Double Loop Copy Special Paste Faster

Hmerman

Board Regular
Joined
Oct 2, 2016
Messages
102
Hello,
Hope you are well.

I have code that looks on Sheet 7 and Sheet 18 for a cells thats values are equal. Then pastes contiguous and non contiguous values in the same row as the equal cell in Sheet 7 to Sheet 18.

It takes a very long time. Can someone please help to make it faster?

<code>
Dim varResponse As String
Dim i As Long, x As Long

varResponse = MsgBox("Transfer Info to Inventory", vbYesNo, "Selection")
If varResponse <> vbYes Then Exit Sub

Application.ScreenUpdating = False
With Sheet18​
lastLookup = Sheet7.Range("C" & .Rows.Count).End(xlUp).row​
lastrow = .Range("C" & .Rows.Count).End(xlUp).row​
For i = 2 To lastrow​
For x = 2 To lastLookup​
If (Sheet7.Cells(x, 3) = .Cells(i, 3).Value) Then​
Sheet7.Cells(x, 2).Copy​
.Cells(i, 6).PasteSpecial xlPasteValues​
Sheet7.Cells(x, 4).Copy​
.Cells(i, 7).PasteSpecial xlPasteValues​
Sheet7.Cells(x, 8).Copy​
.Cells(i, 8).PasteSpecial xlPasteValues​
Sheet7.Cells(x, 12).Copy​
.Cells(i, 9).PasteSpecial xlPasteValues​
Sheet7.Cells(x, 14).Copy​
.Cells(i, 10).PasteSpecial xlPasteValues​
Sheet7.Cells(x, 15).Copy​
.Cells(i, 11).PasteSpecial xlPasteValues​
Sheet7.Cells(x, 16).Copy​
.Cells(i, 12).PasteSpecial xlPasteValues​
Sheet7.Cells(x, 17).Copy​
.Cells(i, 13).PasteSpecial xlPasteValues​
Sheet7.Cells(x, 18).Copy​
.Cells(i, 14).PasteSpecial xlPasteValues​
Sheet7.Cells(x, 19).Copy​
.Cells(i, 15).PasteSpecial xlPasteValues​
Sheet7.Cells(x, 20).Copy​
.Cells(i, 16).PasteSpecial xlPasteValues​
Sheet7.Cells(x, 21).Copy​
.Cells(i, 17).PasteSpecial xlPasteValues​
Sheet7.Cells(x, 22).Copy​
.Cells(i, 18).PasteSpecial xlPasteValues​
Sheet7.Cells(x, 23).Copy​
.Cells(i, 19).PasteSpecial xlPasteValues​
Sheet7.Cells(x, 24).Copy​
.Cells(i, 20).PasteSpecial xlPasteValues​
End If​
Next​
Next
MsgBox "Complete!"


End With
Application.ScreenUpdating = True
</code>

Any advice or suggestion will be helpful.

Regards,
Herman
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
How about
Code:
Sub chk()

   Dim varResponse As String
   Dim i As Long, x As Long
   Dim Var1 As Variant
   
   varResponse = MsgBox("Transfer Info to Inventory", vbYesNo, "Selection")
   If varResponse <> vbYes Then Exit Sub
   
   Application.ScreenUpdating = False
   With Sheet18
      lastlookup = Sheet7.Range("C" & .Rows.Count).End(xlUp).Row
      LastRow = .Range("C" & .Rows.Count).End(xlUp).Row
      For x = 2 To lastlookup
         Var1 = Application.Match(Sheet3.Range("C" & x).Value, .Columns(3), 0)
         If Not IsError(Var1) Then
            .Cells(Var1, 6).Value = Sheet7.Cells(x, 2).Value
            .Cells(Var1, 7).Value = Sheet7.Cells(x, 4).Value
            .Cells(Var1, 8).Value = Sheet7.Cells(x, 8).Value
         End If
      Next
      MsgBox "Complete!"
      
      
   End With
   Application.ScreenUpdating = True
End Sub
I've done the 1st 3 values to be copied, you simply need to do the rest in a similar manner.
 
Upvote 0
Thanks.

If I use the .value in my code it removes the values from the cells and places them in the target cells. So I cannot use that as the info on Sheet 7 must stay in tact.

I will definitely try Application.Match as that looks like true brevity.
 
Upvote 0
My code should not be changing anything on sheet7, it just copies the value from sheet7 to sheet18
 
Upvote 0
Sorry I was wrong. Your code works very very fast.

If I want to add the following logical questions, should I add them after iserror, like so:

<code>
<code>
If not IsError(Var1) and (Sheet7.Cells(x, 14).Value = vbNullString) And _
(Sheet7.Cells(x, 21).Value = vbNullString) then
</code>
</code>
 
Upvote 0
I have been trying to add the following logic and extra loop to check whether the destination cell on Sheet18 is empty before placing the values in the cell.

<code>

lastrow = Sheet18.Range("C" & .Rows.Count).End(xlUp).row

For i = 2 To lastrow
If (.Cells(i, 6).Value = vbNullString) Then

...your code...

End if
Next
</code>

But it does not work. Is there a way to check that the destination cell is empty, before placing Sheet7 values in the cells?

Sorry if I am pushing it a bit.
 
Upvote 0
Try like this
Code:
      For x = 2 To lastlookup
         Var1 = Application.Match(Sheet3.Range("C" & x).Value, .Columns(3), 0)
         If Not IsError(Var1) Then
            If .Cells(Var1, 6).Value = "" Then .Cells(Var1, 6).Value = Sheet7.Cells(x, 2).Value
            If .Cells(Var1, 7).Value = "" Then .Cells(Var1, 7).Value = Sheet7.Cells(x, 4).Value
            If .Cells(Var1, 8).Value = "" Then .Cells(Var1, 8).Value = Sheet7.Cells(x, 8).Value
         End If
      Next
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
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