match strings and copy it to adjacent cell

lady_alina

Board Regular
Joined
Feb 18, 2015
Messages
52
Hi,

Here I'm again with another problem... please help me experts..

I have data in column B sheet 1 which is something like this - 123alina09032015 &
I have data in column A sheet 2 which is like this - alina.

The data is huge and keeps adding every month. So what I want is a VBA code to match the words from column A sheet 2 and column b sheet1 and paste it to the adjacent cell in column c of sheet 1

I have a code but that only finds the exact match and pastes it to column c in sheet 1. Here is the code:


Code:
Sub CopyBasedonSheet1()

Dim i As Long
Dim j As Long
Sheet1LastRow = Worksheets("sheet1").Range("B" & Rows.Count).End(xlUp).Row
Sheet2LastRow = Worksheets("sheet2").Range("A" & Rows.Count).End(xlUp).Row


    For j = 1 To Sheet1LastRow
        For i = 1 To Sheet2LastRow
            If Worksheets("sheet1").Cells(j, 1).Value = Worksheets("sheet2").Cells(i, 4).Value Then
                Worksheets("sheet1").Cells(j, 3).Value = Worksheets("sheet2").Cells(i, 1).Value
            Else
        End If
    Next i
Next j
End Sub

Looking forward to your answer.
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
It is always better to work from a mockup illustration or a screen shot, but maybe this method would work.
Code:
Sub CopyBasedonSheet1()
Dim i As Long
Dim j As Long
Sheet1LastRow = Worksheets("sheet1").Range("B" & Rows.Count).End(xlUp).Row
Sheet2LastRow = Worksheets("sheet2").Range("A" & Rows.Count).End(xlUp).Row
    For j = 1 To Sheet1LastRow
        For i = 1 To Sheet2LastRow
            If InStr(Worksheets("sheet1").Cells(j, 1).Value), Worksheets("sheet2").Cells(i, 4).Value) > 0 Then
                Worksheets("sheet1").Cells(j, 3).Value = Worksheets("sheet2").Cells(i, 1).Value
            Else
        End If
    Next i
Next j
End Sub
 
Upvote 0
Thanks JLGWhiz for your answer however the line below appears red when i paste it and shows error when I run the macro.

Code:
[COLOR=#333333]If InStr(Worksheets("sheet1").Cells(j, 1).Value), Worksheets("sheet2").Cells(i, 4).Value) > 0 Then[/COLOR]
 
Upvote 0
When I run this code it takes too long to return the value and more over the code gives only one value which is exactly matched and copys it to entire rows of columns c .. here is what I want with examples


Sheet1.................................................................THIS IS THE OUTPUT COLUMN C AND THIS ARE THE VALUES THAT IT SHOULD RETURN
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]column A[/TD]
[TD]column B[/TD]
[TD]column C[/TD]
[/TR]
[TR]
[TD]STR109[/TD]
[TD]RTE00ALINA000[/TD]
[TD]ALINA[/TD]
[/TR]
[TR]
[TD]STR986[/TD]
[TD]TRH00PETER000[/TD]
[TD]PETER[/TD]
[/TR]
[TR]
[TD]STR341[/TD]
[TD]GHE00JOHN000[/TD]
[TD]JOHN[/TD]
[/TR]
[TR]
[TD]STR001[/TD]
[TD]PWR00JAMES000[/TD]
[TD]JAMES[/TD]
[/TR]
</tbody>[/TABLE]










Find column A of sheet 2 in column B of sheet1 and paste the matching values in column c of sheet1
Sheet2
[TABLE="class: grid, width: 100, align: left"]
<tbody>[TR]
[TD]column A[/TD]
[/TR]
[TR]
[TD]PETER[/TD]
[/TR]
[TR]
[TD]ALINA[/TD]
[/TR]
[TR]
[TD]JAMES[/TD]
[/TR]
[TR]
[TD]JOHN[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:
Upvote 0
Code:
If InStr(Worksheets("sheet1").Cells(j, 1).Value, Worksheets("sheet2").Cells(i, 4).Value) > 0 Then
Had too many right Parenthesis in it. Haste makes waste they say. Try it without the extra parenthsis.
 
Upvote 0
This might be faster.
Code:
Sub CopyBasedonSheet1()
Dim i As Long
Dim j As Long, fn As Range, sh As Worksheet, fAdr As String
Set sh = Sheets("Sheet1")
Sheet2LastRow = Worksheets("sheet2").Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To Sheet2LastRow
        Set fn = sh.Range("B:B").Find(Sheets("Sheet2").Cells(i, 1).Value, , xlValues, xlPart)
        If Not fn Is Nothing Then
            fAdr = fn.Address
                Do
                    fn.Offset(0, 1) = Sheets("Sheet2").Cells(i, 1).Value
                    Set fn = sh.Range("B:B").FindNext(fn)
                Loop While fAdr <> fn.Address
        End If
    Next
End Sub
 
Upvote 0
Perfect... Thank you JLGWhiz.... it worked... if there is way to rate you or something let me know please I would love to do that..
 
Upvote 0
Hi JLGWHIZ, will you be able to help me with one more thing like if more than one matching word is found like in RTEALINAPETER00123 (sheet1 /column B) there are 2 words Alina and peter from sheet 2 column A so can we make changes to the code where it can copy both in column c of sheet1 seperated by comma like Alina,Peter.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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