Two sheets, Street, City copy ID

Beginner778

New Member
Joined
Sep 23, 2021
Messages
6
Hi,

I have two sheets:

Sheet 1 (1K rows)

Street (A column)
City (B column)

Sheet 2 (2K rows)
Street (A column)
City (B column)
ID (C column)

Problem: If the street and city match in the sheets (1,2), copy the ID to the C column in Sheet 1

I tried modifying the code with bold text

Thank you very much for help
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I try to change this code:

VBA Code:
Sub Compare2Sheets()
' hiker95, 07/13/2011
' http://www.mrexcel.com/forum/showthread.php?t=564090
Dim w1 As Worksheet, w2 As Worksheet
Dim c As Range, LR As Long, FR As Long
Application.ScreenUpdating = False
Set w1 = Worksheets("Sheet1")
Set w2 = Worksheets("Sheet2")
LR = w1.Cells(Rows.Count, 1).End(xlUp).Row
With w1.Range("C1:C" & LR)
  .FormulaR1C1 = "=RC[-2]&RC[-1]"
  .Value = .Value
End With
LR = w2.Cells(Rows.Count, 1).End(xlUp).Row
With w2.Range("C1:C" & LR)
  .FormulaR1C1 = "=RC[-2]&RC[-1]"
  .Value = .Value
End With
For Each c In w2.Range("C1", w2.Range("C" & Rows.Count).End(xlUp))
  FR = 0
  On Error Resume Next
  FR = Application.Match(c, w1.Columns(3), 0)
  On Error GoTo 0
  If FR = 0 Then
    c.Offset(, -2).Resize(, 2).Font.Bold = True
  End If
Next c
w1.Columns(3).ClearContents
w2.Columns(3).ClearContents
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Please ignore. I see now you want a VBA solution.

Hi Beginner778,

Is this what you wanted?

Beginner778.xlsx
ABC
1StreetCityId
2MainMiamiABC
31stMiamiMNB
4ElmBriceXYZ
5OakBelmontPGT
6
Sheet2


Beginner778.xlsx
ABC
1StreetCityId
2ElmMiami 
3ElmBriceXYZ
4MainMiamiABC
51stNew York 
6OakPortland 
7OakBelmontPGT
8 
Sheet1
Cell Formulas
RangeFormula
C2:C8C2=IF(A2="","",IFERROR(INDEX(Sheet2!$C$2:$C$1200,AGGREGATE(15,6,ROW(Sheet2!$A$2:$A$1200)-ROW(Sheet2!$A$1)/((Sheet2!$A$2:$A$1200=A2)*(Sheet2!$B$2:$B$1200=B2)),1)),""))
 
Upvote 0
Hope this helps.
VBA Code:
Sub Sample()
    Dim Dic, i As Long, buf As String
    Dim ws1 As Worksheet, ws2 As Worksheet
    
    Set ws1 = Sheets("sheet1")
    Set ws2 = Sheets("Sheet2")
    Set Dic = CreateObject("Scripting.Dictionary")
    
    On Error Resume Next
    With ws2
        For i = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
            buf = .Cells(i, 1).Value & .Cells(i, 2).Value
            Dic.Add buf, .Cells(i, 3).Value
        Next
    End With
    
    With ws1
        For i = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
            buf = .Cells(i, 1).Value & .Cells(i, 2).Value
            .Cells(i, 3).Value = Dic(buf)
        Next
        Set Dic = Nothing
    End With
    On Error GoTo 0
End Sub
 
Upvote 0
Please ignore. I see now you want a VBA solution.

Hi Beginner778,

Is this what you wanted?

Beginner778.xlsx
ABC
1StreetCityId
2MainMiamiABC
31stMiamiMNB
4ElmBriceXYZ
5OakBelmontPGT
6
Sheet2


Beginner778.xlsx
ABC
1StreetCityId
2ElmMiami 
3ElmBriceXYZ
4MainMiamiABC
51stNew York 
6OakPortland 
7OakBelmontPGT
8 
Sheet1
Cell Formulas
RangeFormula
C2:C8C2=IF(A2="","",IFERROR(INDEX(Sheet2!$C$2:$C$1200,AGGREGATE(15,6,ROW(Sheet2!$A$2:$A$1200)-ROW(Sheet2!$A$1)/((Sheet2!$A$2:$A$1200=A2)*(Sheet2!$B$2:$B$1200=B2)),1)),""))

Could you add a file with this solution?
 
Upvote 0
Could you add a file with this solution?
You can't upload files to this forum. You just need to copy that formula down column C to row 2000

Excel Formula:
=IF(A2="","",IFERROR(INDEX(Sheet2!$C$2:$C$1200,AGGREGATE(15,6,ROW(Sheet2!$A$2:$A$1200)-ROW(Sheet2!$A$1)/((Sheet2!$A$2:$A$1200=A2)*(Sheet2!$B$2:$B$1200=B2)),1)),""))
 
Upvote 0
You can't upload files to this forum. You just need to copy that formula down column C to row 2000

Excel Formula:
=IF(A2="","",IFERROR(INDEX(Sheet2!$C$2:$C$1200,AGGREGATE(15,6,ROW(Sheet2!$A$2:$A$1200)-ROW(Sheet2!$A$1)/((Sheet2!$A$2:$A$1200=A2)*(Sheet2!$B$2:$B$1200=B2)),1)),""))
Thank you Very Much :) Works Great :)

Prerequisite: If the street and city match in the sheets (1,2), copy the ID to the C column in Sheet 1 - this works great :)

How to add condition if column D has zip code but city and street are different still copy ID

Sheet1:
1633343485718.png

Sheet2:

1633343507393.png


Thank You for Big Help :)

If it is difficult it may be a separate function :)
 
Upvote 0
I don't see how this would work as column C wouldn't have an Id if A and B didn't match.

If you want to copy Zip using Id but regardless of other values then this does it.

Beginner778-2.xlsx
ABCD
1StreetCityIdZip
2MainMiamiABC1
31stMiamiMNB2
4ElmBriceXYZ3
5OakBelmontPGT4
6AABBAWS123
Sheet2


Beginner778-2.xlsx
ABCD
1StreetCityIdZip
2ElmMiami  
3ElmBriceXYZ3
4MainMiamiABC1
51stNew York  
6OakPortland  
7OakBelmontPGT4
8CCDD  
Sheet1
Cell Formulas
RangeFormula
C2:C8C2=IF(A2="","",IFERROR(INDEX(Sheet2!$C$2:$C$1200,AGGREGATE(15,6,ROW(Sheet2!$A$2:$A$1200)-ROW(Sheet2!$A$1)/((Sheet2!$A$2:$A$1200=A2)*(Sheet2!$B$2:$B$1200=B2)),1)),""))
D2:D8D2=IF(ISNA(MATCH(C2,Sheet2!$C$2:$C$1200,0)),"",INDEX(Sheet2!$D$2:$D$11200,MATCH(C2,Sheet2!$C$2:$C$1200,0)))
 
Upvote 0
I don't see how this would work as column C wouldn't have an Id if A and B didn't match.

If you want to copy Zip using Id but regardless of other values then this does it.

Beginner778-2.xlsx
ABCD
1StreetCityIdZip
2MainMiamiABC1
31stMiamiMNB2
4ElmBriceXYZ3
5OakBelmontPGT4
6AABBAWS123
Sheet2


Beginner778-2.xlsx
ABCD
1StreetCityIdZip
2ElmMiami  
3ElmBriceXYZ3
4MainMiamiABC1
51stNew York  
6OakPortland  
7OakBelmontPGT4
8CCDD  
Sheet1
Cell Formulas
RangeFormula
C2:C8C2=IF(A2="","",IFERROR(INDEX(Sheet2!$C$2:$C$1200,AGGREGATE(15,6,ROW(Sheet2!$A$2:$A$1200)-ROW(Sheet2!$A$1)/((Sheet2!$A$2:$A$1200=A2)*(Sheet2!$B$2:$B$1200=B2)),1)),""))
D2:D8D2=IF(ISNA(MATCH(C2,Sheet2!$C$2:$C$1200,0)),"",INDEX(Sheet2!$D$2:$D$11200,MATCH(C2,Sheet2!$C$2:$C$1200,0)))

I mean, if the city and street are different, check if the zip codes are the same, if so, then copy the ID - (this will narrow down the search and data ordering)
 
Upvote 0

Forum statistics

Threads
1,215,410
Messages
6,124,756
Members
449,187
Latest member
hermansoa

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