Code To Look For Number On Sheet 2, When Found Insert Row & Add Number Next To It

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
Hi, I have sheet 1 with 3 columns of data. The code needs to look for the number that is in column C on sheet 1 on sheet 2. When it is found I need a row inserted below on sheet 1 with the numbers that are next to that number on sheet 2. The example below will explain better.

Excel Workbook
ABC
109AR145161001EFAR8008
1110AR145161001ECAR5012
1211AR145161001EXAR9001
Sheet1



Excel Workbook
ABCD
21EFAR8008EMG002Gasket
32ECAR5012EMG002Gasket
43ECAR5012EMG171Gasket
54EXAR9001EMR031Rubber
65EXAR9001EMR113Rubber
76EXAR9001EMR130Rubber
Sheet2



Excel Workbook
ABCD
109AR145161001EFAR8008
119AR145161001EMG002Gasket
1210AR145161001ECAR5012
1310AR145161001EMG002Gasket
1410AR145161001EMG171Gasket
1511AR145161001EXAR9001
1611AR145161001EMR031Rubber
1711AR145161001EMR113Rubber
1811AR145161001EMR130Rubber
Sheet1 Result


As you can see in the 3rd table is the result on sheet 1. It has found wherever the number is on sheet 2 and add the data that is next to it in columns C & D on sheet 2. Please help and can get big brownie points from my Director!!!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
What is in red in the third table is whats been inserted below the black when its been found on sheet 2. Help please Mick if your'e about!!
 
Upvote 0
Please help someone, really need a solution ASAP please!!!
 
Upvote 0
Somebody please help. If I don't get a solution by 4pm I could end up with the sack!!!
 
Upvote 0
try this in a standard module:

Rich (BB code):
Option Explicit


Sub test()
   Dim wsSource As Worksheet
   Dim wsTarget As Worksheet
   Dim strFind As String
   Dim rowCopy As Long
   Dim rowPaste As Long
   Dim i As Long
   Dim lr As Long
   Dim strFoundAddress As String
   Dim rngFound As Range
   Dim strId As String
   Dim lngId As Long
   
   Set wsTarget = Sheets("Sheet1")
   Set wsSource = Sheets("Sheet2")
   
   'get the last row
   lr = wsTarget.Range("A" & Rows.Count).End(xlUp).Row
   rowPaste = lr

      For i = 10 To lr
         With wsTarget
            lngId = .Range("A" & i).Value             'eg 1, 2, 3, etc
            strId = .Range("B" & i).Value             'eg AR145....
            strFind = wsTarget.Range("C" & i).Value   'eg EFAR.....
         End With
         
         With wsSource.Range("A2:D10")
            Set rngFound = .Find(strFind, LookIn:=xlValues)
            
            If Not rngFound Is Nothing Then
               strFoundAddress = rngFound.Address
               
               Do
                  rowCopy = rngFound.Row

                  'was the string found?
                   If rowCopy <> 0 Then
                      rowPaste = rowPaste + 1
                      
                      'copy and paste
                      wsTarget.Range("A" & rowPaste).Value = lngId
                      wsTarget.Range("B" & rowPaste).Value = strId
                      wsSource.Range("C" & rowCopy & ":D" & rowCopy).Copy _
                         Destination:=wsTarget.Range("C" & rowPaste)
                   End If
              
                  If Not rngFound Is Nothing Then
                     Set rngFound = .FindNext(rngFound)
                  End If
                  
               Loop While Not rngFound Is Nothing And rngFound.Address <> strFoundAddress
            End If
         End With
      Next i
      
      'sort the output
      wsTarget.Range("A10").Sort _
        Key1:=wsTarget.Columns("A"), _
        Header:=xlGuess
      
   'tidy up
   Set wsSource = Nothing
   Set wsTarget = Nothing
   Set rngFound = Nothing
End Sub
 
Last edited:
Upvote 0
Thanks Bertie but that only work on a few numbers, if it makes a difference I have 10s of thousands of different numbers in column C.
 
Upvote 0
This is probably due to the limited range to search on the source worksheet, i.e., Sheet2.
Replace this:
Rich (BB code):
         With wsSource.Range("A2:D10")
            Set rngFound = .Find(strFind, LookIn:=xlValues)

with:

Rich (BB code):
         'get the last row on the souce sheet
         lr = wsSource.Range("A" & Rows.Count).End(xlUp).Row
         With wsSource.Range("A2:D" & lr)
 
Upvote 0
I can see that it is doing what I need but again on only a few rows, even after changing as you say.
 
Upvote 0
At my end the code is working on the sample data you posted.

The Target sheet is Sheet1.
We get the last populated row on sheet1, based on column A.
And loop through this range:
Rich (BB code):
   'get the last row
   lr = wsTarget.Range("A" & Rows.Count).End(xlUp).Row
   rowPaste = lr


      For i = 10 To lr


The Source sheet we search through is sheet2.
Again we get the range to search based on the last populated row in column A.
Rich (BB code):
         'get the last row on the souce sheet
         lr = wsSource.Range("A" & Rows.Count).End(xlUp).Row
         With wsSource.Range("A2:D" & lr)

Can you see anything here which would cause a problem?
 
Upvote 0
Not really, but I dont know what I am looking for!! Could I send you the file so you can see the exact layout?
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,020
Members
448,938
Latest member
Aaliya13

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