replace column with another based on match column between two sheets

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
612
Office Version
  1. 2019
Hello
I need macro to match CODE column (A) between two sheets , if they are matched then I want replace column B for MAIN sheet with column C for REPORT sheet and ignore BRANDS contain empty cells for column A in REPORT sheet .
sa
AB
1CODEBRAND
2BSJ100BS 1200R20 G580 JAP
3BSJ100BS 1200R20 G580 JAP
4BSJ100BS 1200R20 G580 JAP
5BSJ100BS 1200R20 G580 JAP
6BSJ100BS 1200R20 G580 JAP
7BSJ101BS 1200R20 G580 THI
8BSJ101BS 1200R20 G580 THI
9BSJ101BS 1200R20 G580 THI
10BSJ102BS 1200R24 G582 JAP
11BSJ102BS 1200R24 G582 JAP
12BSJ102BS 1200R24 G582 JAP
13BSJ102BS 1200R24 G582 JAP
14BSJ103BS 1200R20 R187 JAP
15BSJ103BS 1200R20 R187 JAP
16BSJ104BS 1200R20 R187 THI
17BSJ105BS 1200R24 G580 JAP
18BSJ105BS 1200R24 G580 JAP
19BSJ106BS 13R22.5 R187 JAP
20BSJ107BS 1400R20 R180 JAP
21BSJ107BS 1400R20 R180 JAP
22BSJ108BS 1400R20 R180BZ JAP
23BSJ109BS 1400R20 VSJ JAP
24BSJ109BS 1400R20 VSJ JAP
25BSJ109BS 1400R20 VSJ JAP
26BSJ110BS 155R12C R624 INDO
27BSJ111BS 155R12C R623 INDO
MAIN



sa
ABC
1CODEITEMBRAND
2BSJ1031BS 1200R20 R187 TCF
3BSJ1042BS 1200R20-18PR R187 THI
4BSJ1053BS 1200R24 G580
54BS 1200R20 G580 TCF
65BS 1200R24 G582 JAP
7BSJ1076BS 1400R20 TCF R180
87BS 1400R20 TCF 20PR R180BZ JAP
9BSJ1098BS 1400R20 VSJ TCF
10BSJ1109BS 155 R12C R624 IND
REPORT



result should sort from small to big based on column A and keep empty cell in the last
sa
ABC
1CODEITEMBRAND
2BSJ1031BS 1200R20 R187 JAP
3BSJ1042BS 1200R20 R187 THI
4BSJ1053BS 1200R24 G580 JAP
5BSJ1074BS 1400R20 R180 JAP
6BSJ1095BS 1400R20 VSJ JAP
7BSJ1106BS 155R12C R624 INDO
87BS 1200R20 G580 TCF
98BS 1200R24 G582 JAP
109BS 1400R20 TCF 20PR R180BZ JAP
REPORT
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Untested, Try this on a copy of your workbook.
VBA Code:
Sub abdo()
Dim shMAIN As Worksheet, shREPORT As Worksheet, lRwM As Long, lRwR As Long
Dim VMain As Variant, VReport As Variant, i As Long, j As Long, ct As Long

Set shMAIN = Sheets("MAIN"): Set shREPORT = Sheets("REPORT")
lRwM = shMAIN.Cells(Rows.Count, "A").End(xlUp).Row: lRwR = shREPORT.Cells(Rows.Count, "A").End(xlUp).Row
VMain = shMAIN.Range("A2:B" & lRwM).Value: VReport = shREPORT.Range("A2:C" & lRwR)
ReDim Vout(1 To UBound(VMain, 1), 1 To 1)
For i = 1 To UBound(VMain, 1)
    For j = 1 To UBound(VReport, 1)
        If VMain(i, 1) = VReport(j, 1) Then
            ct = ct + 1
            VMain(i, 2) = VReport(j, 3)
        End If
    Next j
Next i
If ct > 0 Then
    shMAIN.Range("A2:B" & lRwM) = VMain
    shREPORT.Range("A1:C" & lRwR).Sort key1:=shREPORT.Range("A2"), order1:=xlAscending, Header:=xlYes
Else
    MsgBox "No matches found"
    Exit Sub
End If
End Sub
 
Upvote 0
thanks Joe.
the result just will be in REPORT sheet when replace BRANDS column for REPORT sheet should be the same thing as in MAIN sheet.
don't change BRANDS column for MAIN sheet .
 
Upvote 0
thanks Joe.
the result just will be in REPORT sheet when replace BRANDS column for REPORT sheet should be the same thing as in MAIN sheet.
don't change BRANDS column for MAIN sheet .
Your OP says: "I need macro to match CODE column (A) between two sheets , if they are matched then I want replace column B for MAIN sheet with column C for REPORT sheet ..."
As I now understand from your reply, all changes prompted by a CODE match should be made to the REPORT sheet, not the MAIN sheet. Try the revision below:
VBA Code:
Sub abdo()
Dim shMAIN As Worksheet, shREPORT As Worksheet, lRwM As Long, lRwR As Long
Dim VMain As Variant, VReport As Variant, i As Long, j As Long, Vout As Variant, ct As Long

Set shMAIN = Sheets("MAIN"): Set shREPORT = Sheets("REPORT")
lRwM = shMAIN.Cells(Rows.Count, "A").End(xlUp).Row: lRwR = shREPORT.Cells(Rows.Count, "A").End(xlUp).Row
VMain = shMAIN.Range("A2:B" & lRwM).Value: VReport = shREPORT.Range("A2:C" & lRwR)
For i = 1 To UBound(VMain, 1)
    For j = 1 To UBound(VReport, 1)
        If VMain(i, 1) = VReport(j, 1) Then
            ct = ct + 1
            VReport(j, 3) = VMain(i, 2)
        End If
    Next j
Next i
If ct > 0 Then
    With shREPORT.Range("A2:C" & lRwR)
        .Value = VReport
        .Sort key1:=shREPORT.Range("A2"), order1:=xlAscending, Header:=xlYes
    End With
Else
    MsgBox "No matches found"
    Exit Sub
End If
End Sub
 
Upvote 0
sorry about wrong details in OP .
thanks for new version .:)
last thing could re-autonumbering when sort data for REPORT sheet in column B .
 
Upvote 0
sorry about wrong details in OP .
thanks for new version .:)
last thing could re-autonumbering when sort data for REPORT sheet in column B .
This version includes renumbering of column B items in sheet REPORT.
VBA Code:
Sub abdo()
Dim shMAIN As Worksheet, shREPORT As Worksheet, lRwM As Long, lRwR As Long
Dim VMain As Variant, VReport As Variant, i As Long, j As Long, Vout As Variant, ct As Long

Set shMAIN = Sheets("MAIN"): Set shREPORT = Sheets("REPORT")
lRwM = shMAIN.Cells(Rows.Count, "A").End(xlUp).Row: lRwR = shREPORT.Cells(Rows.Count, "A").End(xlUp).Row
VMain = shMAIN.Range("A2:B" & lRwM).Value: VReport = shREPORT.Range("A2:C" & lRwR)
For i = 1 To UBound(VMain, 1)
    For j = 1 To UBound(VReport, 1)
        If VMain(i, 1) = VReport(j, 1) Then
            ct = ct + 1
            VReport(j, 3) = VMain(i, 2)
        End If
    Next j
Next i
If ct > 0 Then
    With shREPORT
        .Range("A2:C" & lRwR).Value = VReport
        .Range("A2:C" & lRwR).Sort key1:=shREPORT.Range("A2"), order1:=xlAscending, Header:=xlYes
        .Range("B2").Value = 1
        .Range("B2").AutoFill .Range("B2:B" & lRwR), xlFillSeries
    End With
Else
    MsgBox "No matches found"
    Exit Sub
End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,712
Messages
6,174,031
Members
452,542
Latest member
Bricklin

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