Stitching values from one sheet to another

MurdochQuill

Board Regular
Joined
Nov 21, 2020
Messages
84
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm using the following subroutine to stitch values from sheet "Stitch" col E to sheet "Sheet 1" col O

If it is a match, we then copy data from col F & G into col L & K onto Sheet 1.

This works if I have ONE match for each value in col E on "Stitch". But after using it for a while I have found I have multiple matches on "Sheet 1" col O. How would I rework the following so if it finds more than one match it can copy the data to ALL matches rather than just the first encountered match?

VBA Code:
Sub StitchRunsComms()
Application.ScreenUpdating = False
Dim lastRw1, lastRw2, nxtRw, m
Dim wb As Workbook
Set ws = ThisWorkbook.Worksheets("Sheet 1")
Set tb = ThisWorkbook
   

' *** Logic for stitching back together ***
        With tb.Worksheets("Sheet 1")
       
                lastRw1 = ws.Range("O" & Rows.Count).End(xlUp).Row
                lastRw2 = Sheets("Stitch").Range("E" & Rows.Count).End(xlUp).Row
           
             For nxtRw = 1 To lastRw2
                 With ws.Range("O2:O" & lastRw1)
                      Set m = .Find(tb.Sheets("Stitch").Range("E" & nxtRw), LookIn:=xlValues, lookat:=xlWhole)
                            If Not m Is Nothing Then
                                 tb.Sheets("Stitch").Range("F" & nxtRw).Copy
                                 ws.Range("L" & m.Row).PasteSpecial xlValues
                                 tb.Sheets("Stitch").Range("G" & nxtRw).Copy
                                 ws.Range("K" & m.Row).PasteSpecial xlValues
                            End If
                           
                 End With
                
             Next
            
         End With
     tb.Worksheets("Stitch").Range("E1:G1001").Clear
            
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
How about
VBA Code:
Sub MurdochQuill()
   Dim Ws1 As Worksheet, Ws2 As Worksheet
   Dim Cl As Range
   Dim Dic As Object
   
   Set Ws1 = ThisWorkbook.Sheets("Sheet 1")
   Set Ws2 = ThisWorkbook.Sheets("Stitch")
   Set Dic = CreateObject("scripting.dictionary")
   
   For Each Cl In Ws2.Range("E2", Ws2.Range("E" & Rows.Count).End(xlUp))
      Dic(Cl.Value) = Array(Cl.Offset(, 2).Value, Cl.Offset(, 1).Value)
   Next Cl
   For Each Cl In Ws1.Range("O2", Ws1.Range("O" & Rows.Count).End(xlUp))
      If Dic.exists(Cl.Value) Then Cl.Offset(, -4).Resize(, 2).Value = Dic(Cl.Value)
   Next Cl
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub MurdochQuill()
   Dim Ws1 As Worksheet, Ws2 As Worksheet
   Dim Cl As Range
   Dim Dic As Object
  
   Set Ws1 = ThisWorkbook.Sheets("Sheet 1")
   Set Ws2 = ThisWorkbook.Sheets("Stitch")
   Set Dic = CreateObject("scripting.dictionary")
  
   For Each Cl In Ws2.Range("E2", Ws2.Range("E" & Rows.Count).End(xlUp))
      Dic(Cl.Value) = Array(Cl.Offset(, 2).Value, Cl.Offset(, 1).Value)
   Next Cl
   For Each Cl In Ws1.Range("O2", Ws1.Range("O" & Rows.Count).End(xlUp))
      If Dic.exists(Cl.Value) Then Cl.Offset(, -4).Resize(, 2).Value = Dic(Cl.Value)
   Next Cl
End Sub
Much more concise. I like this method a lot more.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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