autofill column for each item based on matching column between two sheets

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
612
Office Version
  1. 2019
Hello
I want macro to matching column B between two sheets and fill missed codes for column A in REPORT sheet.
as
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




as
AB
1CODEBRAND
2BS 1200R24 G580 JAP
3BS 13R22.5 R187 JAP
4BS 1400R20 R180 JAP
5BS 1400R20 R180BZ JAP
6BS 1400R20 VSJ JAP
7BS 1200R20 G580 JAP
8BS 1200R20 G580 THI
9BS 1200R20 R187 JAP
10BS 1200R20 R187 THI
REPORT


result
as
AB
2BSJ105BS 1200R24 G580 JAP
3BSJ106BS 13R22.5 R187 JAP
4BSJ107BS 1400R20 R180 JAP
5BSJ108BS 1400R20 R180BZ JAP
6BSJ109BS 1400R20 VSJ JAP
7BSJ100BS 1200R20 G580 JAP
8BSJ101BS 1200R20 G580 THI
9BSJ103BS 1200R20 R187 JAP
10BSJ104BS 1200R20 R187 THI
REPORT


thanks
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I haven't tested but should work:
VBA Code:
Sub test()
  Dim MAIN As Worksheet, REPORT As Worksheet, i As Long
  Dim MAINRange As Range, REPORTRange As Range

  Set MAIN = Worksheets("MAIN")
  Set MAINRange = Intersect(MAIN.UsedRange, MAIN.Range("A:B"))
  Set REPORT = Worksheets("REPORT")
  Set REPORTRange = Intersect(REPORT.UsedRange, REPORT.Range("A:B"))

  With REPORTRange
  For i = 2 To .Rows.Count
    .Cells(i, 1).Value = WorksheetFunction.Lookup(.Cells(i, 2).Value, MAINRange.Columns(2), MAINRange.Columns(1))
  Next
  End With
End Sub
 
Upvote 0
perfect !
can you modify code ,please?
in sheet REPORT the BRAND column will be C ,not to B , then will match column B for MAIN sheet with column C for REPORT sheet and fill codes in column A in REPORT sheet, sorry about this error! .
 
Upvote 0
Like this?
VBA Code:
Sub test()
  Dim MAIN As Worksheet, REPORT As Worksheet, i As Long
  Dim MAINRange As Range, REPORTRange As Range

  Set MAIN = Worksheets("MAIN")
  Set MAINRange = Intersect(MAIN.UsedRange, MAIN.Range("A:B"))
  Set REPORT = Worksheets("REPORT")
  Set REPORTRange = Intersect(REPORT.UsedRange, REPORT.Range("A:C"))

  With REPORTRange
  For i = 2 To .Rows.Count
    .Cells(i, 1).Value = WorksheetFunction.Lookup(.Cells(i, 3).Value, MAINRange.Columns(2), MAINRange.Columns(1))
  Next
  End With
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