VBA - Concatenate Match Offset Loop

MixedUpExcel

Board Regular
Joined
Apr 7, 2015
Messages
206
Hi,

Detail of data:

Column A has a list of product codes (of which there can be duplicates upto 5000 duplicates but in some cases, there may only be 1)
Column B has a list of associated product codes (there may also be duplicates as more than one of the products in Column A can be associated with the products in Column B) - I don't think the duplicate issue will come in to this though

I have a unique list of product codes from Column A and these are placed under a header in Column J

I have a number from 1 to 5000 as headers in Cell K1 going to the right ->>>

I want a VBA code which will take the product code from Cell J2 - find the corresponding code in Column A and return the product code next to it in Column B and put that code in Cell K2

As there will be duplicates, I want the code to then move to Cell L2 and look for the next instance in Column A where it finds the product code from Cell J2 (the second instance) and puts the result in Cell L2 and so on until it can't find any more products (Cell J2) in Column A

Then, it moves to Row 3, Column J and does the process again until it can't find any more of the Product Code (Cell J3) in Column A.

I will have potentially 40,000 Unique Codes in Column J

I have a formula which can do this but takes FAR TOO LONG to process.

This is my formula:

Code:
{=INDEX(Sheet1!$B$2:$B$20000, SMALL(IF($J2=Sheet1!$A$2:$A$20000, ROW(Sheet1!$B$2:$B$20000)-MIN(ROW(Sheet1!$B$2:$B$20000))+1, ""), COLUMN(A1)))}

This is an example table as described above:

ABCDEFGHIJKLMNO
1ProductAssociated Unique Product12345
2AB123XY1 AB123XY1XY4XY1XY4#NUM!
3AB124XY2 AB124XY2XY5XY2XY5#NUM!
4AB125XY3 AB125XY3XY6XY3XY6#NUM!
5AB126XY4 AB126XY4XY5XY1XY2XY4
6AB126XY5 AB127XY6XY3XY6#NUM!
7AB127XY6 AB128XY1XY4XY1#NUM!
8AB128XY1 AB129XY2XY5XY2#NUM!
9AB129XY2 AB130XY3XY6XY3#NUM!
10AB130XY3
11AB123XY4
12AB124XY5
13AB125XY6
14AB126XY1
15AB126XY2
16AB127XY3
17AB128XY4
18AB129XY5
19AB130XY6
20AB123XY1
21AB124XY2
22AB125XY3
23AB126XY4
24AB126XY5
25AB127XY6
26AB128XY1
27AB129XY2
28AB130XY3
29AB123XY4
30AB124XY5
31AB125XY6
32AB126XY1

<colgroup><col span="2"><col><col span="7"><col><col span="5"></colgroup><tbody>
</tbody>


My thoughts were to possibly concatenate the unique codes in Column J with the header number (have an inserted Column at the start - a new Column A possibly) and using Countif eg. =Countif($A$2:A2,A2) in the new Column A - give me the number in the Count which I can concatenate in a new column and do a look up that way - or match / offset etc.

Is there any way to do the above or is there a better solution?

Thanks in advance.

Simon
 
Last edited:

Some videos you may like

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

MixedUpExcel

Board Regular
Joined
Apr 7, 2015
Messages
206
Hi,

I've put the below code together which mostly does what I want.

If anyone sees this posts and can suggest something better, please let me know.

Code:
Sub test2()

Dim x As String
Dim y As String
Dim m As String


For a = 2 To 1400
For z = 11 To 500


x = Cells(a, 10)
y = Cells(1, z)


m = y & x


numRow = Application.Match(m, Range("c:c"), 0)


If IsError(numRow) = False Then
    Cells(a, z).Value = Range("d1").Offset(numRow - 1)
Else
    
   GoTo NEXTROW
   
End If


Next


NEXTROW:


Next


End Sub

Thanks.

Simon
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,304
Office Version
  1. 365
Platform
  1. Windows
Do you want duplicates codes in K, L etc as you have shown, or just the unique values like

<b>Excel 2013/2016</b><table cellpadding="2.5px" rules="all" style=";background-color: rgb(255,255,255);border: 1px solid;border-collapse: collapse; border-color: rgb(187,187,187)"><colgroup><col width="25px" style="background-color: rgb(218,231,245)" /><col /><col /><col /><col /><col /></colgroup><thead><tr style=" background-color: rgb(218,231,245);text-align: center;color: rgb(22,17,32)"><th></th><th>J</th><th>K</th><th>L</th><th>M</th><th>N</th></tr></thead><tbody><tr ><td style="color: rgb(22,17,32);text-align: center;">2</td><td style=";">AB123</td><td style=";">XY1</td><td style=";">XY4</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">3</td><td style=";">AB124</td><td style=";">XY2</td><td style=";">XY5</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">4</td><td style=";">AB125</td><td style=";">XY3</td><td style=";">XY6</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">5</td><td style=";">AB126</td><td style=";">XY4</td><td style=";">XY5</td><td style=";">XY1</td><td style=";">XY2</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">6</td><td style=";">AB127</td><td style=";">XY6</td><td style=";">XY3</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">7</td><td style=";">AB128</td><td style=";">XY1</td><td style=";">XY4</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">8</td><td style=";">AB129</td><td style=";">XY2</td><td style=";">XY5</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">9</td><td style=";">AB130</td><td style=";">XY3</td><td style=";">XY6</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr></tbody></table><p style="width:0.8em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid rgb(187,187,187);border-top:none;text-align: center;background-color: rgb(218,231,245);color: rgb(22,17,32)">T</p><br /><br />
 

MixedUpExcel

Board Regular
Joined
Apr 7, 2015
Messages
206
Hi Fluff,

I hadn't even noticed that - I'd done that with the formula and dragged across (I think I must have missed a $ sign somewhere or something as it usually works ok (or so I think))

I've got the coding doing what I needed now, so won't need the formula now.

Thanks for letting me know though, it's worth checking to make sure it is only a simple error on my side of things.

Simon
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,304
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

If you just want the unique values try
Code:
Sub MixedUpExcel()
   Dim Ary As Variant, Ky As Variant
   Dim Dic As Object
   Dim i As Long
   
   Ary = Range("A1").CurrentRegion.Value2
   
   Set Dic = CreateObject("scripting.dictionary")
   For i = 2 To UBound(Ary)
      If Not Dic.Exists(Ary(i, 1)) Then Dic.Add Ary(i, 1), CreateObject("scripting.dictionary")
      Dic(Ary(i, 1))(Ary(i, 2)) = Empty
   Next i
   i = 0
   With Range("J" & Rows.Count).End(xlUp).Offset(1)
      For Each Ky In Dic.Keys
         .Offset(i).Value = Ky
         .Offset(i, 1).Resize(, Dic(Ky).Count).Value = Dic(Ky).Keys
         i = i + 1
      Next Ky
   End With
End Sub
 

MixedUpExcel

Board Regular
Joined
Apr 7, 2015
Messages
206
Thanks Fluff,

I've tried running it but I'm not sure what I'm expecting it to do?

I've got my product codes in Column A / associated codes in Column B and the unique list of product codes in Column J.

When I run your code, nothing seems to happen and then after a while, I get the following message:

Run-time error '1004':
Application-defined or object defined error

Thanks.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,304
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Firstly you don't need the list in col J, the macro will put them in for you.
Secondly when you get the error click debug, what line is highlighted?
 

MixedUpExcel

Board Regular
Joined
Apr 7, 2015
Messages
206
Hi Fluff,

Error appears to be against line:

Code:
.Offset(i, 1).Resize(, Dic(Ky).Count).Value = Dic(Ky).Keys


Thanks.
 
Last edited:

MixedUpExcel

Board Regular
Joined
Apr 7, 2015
Messages
206
Firstly you don't need the list in col J, the macro will put them in for you.
Secondly when you get the error click debug, what line is highlighted?

Hi Fluff,

Thank you for taking the time to create that piece of code for me.

A lot of that is beyond my current understanding but I'm more than happy to learn more.

I hadn't realised at first that your code was bypassing some of the processes I thought I need and speeding it up to such an amazingly quick pace.

Really appreciate it.

Thank you for your help again.

Simon


Edit:

If this makes any difference - this is from my actual sheet:

Column A and B have 162,820 rows

When I got the error message, it had put in the Product Code in Column J on Row 7570 BUT it hadn't put in the first Associated code into Cell K7570 (it just stopped there)

The Product Code in Cell J7570 first appears in Column A on Row 108431

I've put the above because I don't know if there are any limitations to the code you've provided me with regards to number of rows it can deal with etc.

Thanks.
 
Last edited:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,304
Office Version
  1. 365
Platform
  1. Windows
Clear column J & run this
Code:
Sub MixedUpExcelTest()
   Dim Ary As Variant, Ky As Variant
   Dim Dic As Object
   Dim i As Long
   
   Ary = Range("A1").CurrentRegion.Value2
   
   Set Dic = CreateObject("scripting.dictionary")
   For i = 2 To UBound(Ary)
      Dic(Ary(i, 1)) = Dic(Ary(i, 1)) + 1
   Next i
   i = 0
   With Range("J" & Rows.Count).End(xlUp).Offset(1)
      For Each Ky In Dic.Keys
         .Offset(i).Resize(, 2).Value = Array(Ky, Dic(Ky))
         i = i + 1
      Next Ky
   End With
End Sub
Then check what the highest value in col L, what is it?
 

Watch MrExcel Video

Forum statistics

Threads
1,127,119
Messages
5,622,834
Members
415,934
Latest member
adstocking

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
Top