Lookup value in sheet 1 transpose value from sheet 2 & the value in next column to sheet 1

BoyBoy

New Member
Joined
Sep 25, 2020
Messages
26
Office Version
  1. 365
Platform
  1. Windows
Hello,
I am trying to update one of my worksheet using VBA Code, by look up value from sheet 1 in column A, search the same value in sheet 2 column A (there will be duplicates), and return the value from column A and the associate value in sheet 2 column B to sheet 1 from column F to column AJ+ (depend how many storage in sheet 2 column B, could be 50+ storages)
Sheet 2 is somewhat look like this:
1604537669287.png

following is the Sheet 1 and the result data (yellow highlighted)
1604537714840.png

A simple working VBA code would be highly appreciated.
Thanks.
BoyBoy
 
If that can happen, then try this version.

VBA Code:
Sub Rearrange_v2()
  Dim a As Variant, b As Variant, c As Variant, j As Variant
  Dim i As Long, k As Long
 
  Const MaxStorages As Long = 100 '<- Edit this if required
 
  With Sheets("Sheet2")
    a = .Range("A2", .Range("B" & Rows.Count).End(xlUp).Offset(1)).Value
    c = Application.Index(a, 0, 1)
  End With
  With Sheets("Sheet1")
    b = .Range("A2", .Range("A" & Rows.Count).End(xlUp)).Value
    ReDim Preserve b(1 To UBound(b), 1 To MaxStorages + 1)
    For i = 1 To UBound(b)
      j = Application.Match(b(i, 1), c, 0)
      If IsNumeric(j) Then
        k = 2
        Do
          b(i, k) = a(j, 2)
          k = k + 1
          j = j + 1
        Loop Until a(j, 1) <> b(i, 1)
      End If
    Next i
    .Range("F2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  End With
End Sub

My sample data

BoyBoy.xlsm
AB
1
2No 9A
3No 9B
4No 9C
5No 9D
6No 9E
7No 9F
8No 9G
9No 9H
10No 9I
11No 8J
12No 8K
13No 8L
14No 8M
15No 4N
16No 4O
17No 4P
18No 4Q
19No 4R
20No 4S
21No 4T
22No 4U
23No 4V
24No 4W
25No 4X
Sheet2


.. and results. Note that No 44 does not appear in Sheet2 above.

BoyBoy.xlsm
ABCDEFGHIJKLMNOP
1
2No 9No 9ABCDEFGHI
3No 44No 44
4No 8No 8JKLM
Sheet1
Yes, you are right, the example I used to try on the first code did have the model that's not in sheet 2.
Thank you so much, the v2 works like a charm!
Have a nice day :)
BoyBoy
 
Upvote 0

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

Forum statistics

Threads
1,215,556
Messages
6,125,495
Members
449,235
Latest member
Terra0013

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