If a name in sheet 1 matches a name in sheet 2, replace the data from sheet 2's name with the data from sheet 1's name. ?

Will82892

New Member
Joined
Jan 24, 2013
Messages
13
Hello,

I need some help with a problem at work. I need to create a macro (or combination of formulas) to match columns a,b, and d in sheet 1 & 2 and replace data in column c from sheet 2 with sheet 1's column c.
Below is what I'm trying to do:

Sheet 1
abcd
1
John Smith
9
123
155
2
Tim Smith
10
124
158
3
Mike Smith
55
125
200
4
Tony Smith
60
128
250

<tbody>
</tbody>

Sheet 2
abcd
1
John Smith
9
456
155
2
Tim Smith
10
789
158
3
Mike Smith
55
125
200
4
Tony Smith
60
546
250

<tbody>
</tbody>

End Result:Sheet 2
abcd
1
John Smith
9
123
155
2
Tim Smith
10
124
158
3
Mike Smith
55
125
200
4
Tony Smith
60
128
250

<tbody>
</tbody>
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Code:
Sub test()

Dim LastRow As Long

LastRow = Worksheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To LastRow
    
    If Worksheets("sheet1").Range("A" & i) = Worksheets("sheet2").Range("A" & i) Then
        
        If Worksheets("sheet1").Range("B" & i) = Worksheets("sheet2").Range("B" & i) Then
    
            If Worksheets("sheet1").Range("D" & i) = Worksheets("sheet2").Range("D" & i) Then
        
                Worksheets("sheet2").Range("C" & i) = Worksheets("sheet1").Range("C" & i)
        
            End If
            
        End If
    
    End If
Next
    
End Sub
 
Upvote 0
This works great! i just tested it out with my example.
Now I'm new to Visual Basic, so if i needed to edit this macro to different columns, would I just change the capital letter to what I want? For example:

If i needed to slide the macro 1 column to the right, would i just change the below from-

If Worksheets("sheet1").Range("A" & i) = Worksheets("sheet2").Range("A" & i) Then
to
If Worksheets("sheet1").Range("B" & i) = Worksheets("sheet2").Range("B" & i) Then
?
 
Upvote 0
A is column A, sheet1 and sheet2 is the sheetname. i is increment of 1 from 1 to the last rows.

If Worksheets("sheet1").Range("A" & i) = Worksheets("sheet2").Range("A" & i) Then

mean if sheet1 col A and 1 (A1) = sheet2 col A and 1 (A1) then ...

If Worksheets("sheet1").Range("B" & i) = Worksheets("sheet2").Range("B" & i) Then

mean if sheet1 col B and 1 (B1) = sheet2 col B and 1 (B1) then ...
 
Upvote 0
Hmm... I thought I understood what you wrote but I guess not since it's not working in my work file. I tried to edit the VBA to match my work files columns but it's not working. Can you help?


Sheet 1
A B C D E F G H I J K


Doris


74
103401
x
80xxxxxx
Sitck
7
37560252
x300xxxxxx
Win47
1090527x158xxxxxx
Nelson96
483487x30.05
x
xxxxx
Bigman255
466942
x300xxxxxx
Brooks251789113
x
200xxxxxx
Casillas1837559372
x300xxxxxx
Clark76
571953x70xxxxxx
Fager
27437438206x50xxxxxx
Hutcheson129590814x608.4xxxxxx
Lee
2551103874x125xxxxxx
McGuire3537550488x300xxxxxx
Clenendin
1291109225x59xxxxxx
Johnson14479459
x110.7xxxxxx

<col style="width: 121pt;" width="161"> <col style="width: 21pt;" width="28"> <col style="width: 47pt;" width="63"> <col style="width: 96pt;" width="128"> <col style="width: 35pt;" width="46"> <col style="width: 48pt;" width="64"> <col style="width: 86pt;" width="115"> <col style="width: 59pt;" width="78"> <col style="width: 26pt;" width="35"> <col style="width: 36pt;" width="48"> <col style="width: 47pt;" width="63"> <col style="width: 19pt;" width="25"> <tbody>
</tbody>

Sheet 2
A B C D E F G H I J K

xx
74
x
x
Doris
x590033803
x
80xx
x7
xxStickx37560252x300xx
x47xxWinx1090527x158.04
xx
x96xxNelsonx483487x30.05xx
x255xxBigmanx466942x300xx
x76xxClarkx571953x70xx
x274xxFagerx37438206x50xx
x274xxDorothyx37438206x50xx
x129xxHutchesonx590814x608.38xx
x255xxLeex115436x125xx
x35xxMcGuirex37550488x300xx

<col style="width: 19pt;" width="25"> <col style="width: 21pt;" width="28"> <col style="width: 51pt;" width="68"> <col style="width: 57pt;" width="76"> <col style="width: 111pt;" width="148"> <col style="width: 53pt;" span="2" width="70"> <col style="width: 37pt;" span="2" width="49"> <col style="width: 57pt;" width="76"> <col style="width: 47pt;" width="63"> <tbody>
</tbody>


Code Input (Not Working)

Sub Macro3()

Dim LastRow As Long

LastRow = Worksheets("Sheet 1").Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To LastRow

If Worksheets("Sheet 1").Range("A" & i) = Worksheets("Sheet 2").Range("E" & i) Then

If Worksheets("Sheet 1").Range("B" & i) = Worksheets("Sheet 2").Range("B" & i) Then

If Worksheets("Sheet 1").Range("E" & i) = Worksheets("Sheet 2").Range("I" & i) Then

Worksheets("Sheet 2").Range("G" & i) = Worksheets("Sheet 1").Range("C" & i)

End If
End If
End If
Next
End Sub



There are two in the above that it should be fixing: Doris and Lee. The Macro I'm currently using will only fix Doris and Idk why. Does it have to be in the same row for it to work? b/c the majority of the names are not in the same row in both sheets. Doris just happened to be in the same row for sheets 1 and 2.
 
Upvote 0
from the above sheets, they are in same rows, therefore it can work, the 1st sheets you give me also all in same rows.
 
Upvote 0
The second example I posted has Doris in the same row but not Lee (10 down on first, 11 down on second). How do I modify the macro to search anywhere in the column for a matching name rather than in the same row?
 
Upvote 0
Code:
rowA = application.match()

application.match is same as =match

e.g. A1 is john. you searching for john in sheet2 col A
application.match(sheet1.range("A1"),sheet2.range("A:A"),0)

it will give you row number, so rowA = row number
 
Upvote 0
I wasn't sure how to incorporate the application.match ... so I did a little more research and now I have this code:

Sub evaluate_data()
Dim i, j, LastRowSh1, LastRowSh2
LastRowSh2 = Sheets("CC Company").Range("E" & Rows.Count).End(xlUp).Row
LastRowSh1 = Sheets("Qfunds").Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LastRowSh2
For j = 1 To LastRowSh1
If Sheets("CC Company").Cells(i, "E").Value = Sheets("Qfunds").Cells(j, "A") Then
If Sheets("CC Company").Cells(i, "B").Value = Sheets("Qfunds").Cells(j, "B") Then
If Sheets("CC Company").Cells(i, "I").Value = Sheets("Qfunds").Cells(j, "E") Then
Sheets("CC Company").Cells(i, "G").Value = Sheets("Qfunds").Cells(j, "C").Value
End If
End If
End If
Next j
Next i
End Sub

It appears to do what I want on small amounts of data (1500 rows) but when I run it on my actual work task (over 3000 rows on each tab) it freezes my excel. Can you review the above code for me and see if it is intended to work with that much data? or can you revisit your code so I can try to copy paste that? thx
 
Upvote 0

Forum statistics

Threads
1,216,586
Messages
6,131,582
Members
449,656
Latest member
pavankumar1421

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