Need to combine data from multiple rows - script not working as expected

Cantrecallmyusername

Board Regular
Joined
May 24, 2021
Messages
50
Office Version
  1. 365
Platform
  1. Windows
Hey there,

I have a dump of data which contains many rows - these rows contain common data in column A and B but there are variables in the other columns which I want to be combined in to the same cell. My dump file has ~9k rows and I am expecting the output to have ~700.
I have been utilising the below script however I am noticing that the expected output is not working as expected.

VBA Code:
   Ary = Sheets("Dump").Range("A1").CurrentRegion.Value2
   ReDim Nary(1 To UBound(Ary), 1 To UBound(Ary, 2))
   For r = 2 To UBound(Ary)
      If Ary(r, 1) <> Ary(r - 1, 1) Then
         nr = nr + 1
         For c = 1 To UBound(Ary, 2)
            Nary(nr, c) = Ary(r, c)
         Next c
      Else
         For c = 1 To UBound(Ary, 2)
            If InStr(1, Nary(nr, c), Ary(r, c), 1) = 0 Then Nary(nr, c) = Nary(nr, c) & vbLf & Ary(r, c)
         Next c
      End If
      
   Next r
   Sheets("Output").Range("A6").Resize(nr, UBound(Nary, 2)).Value = Nary

For one example I have noticed that it is combining the data in columns A,B,C as one input where I am expecting it to split the data based on the difference in column c

This is what I am expecting as a result;
Communication MonitoringCommunications surveillance of staffFIL Holdings (Luxembourg) S.A.
Communication MonitoringCommunications surveillance of staffFIL Investments (Japan) Limited
FIL Asia Holdings Pte Limited - Hong Kong Branch

What I am getting is ;
Communication MonitoringCommunications surveillance of staffFIL Holdings (Luxembourg) S.A.
FIL Investments (Japan) Limited
FIL Asia Holdings Pte Limited - Hong Kong Branch
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
try changing this line:
VBA Code:
If Ary(r, 1) <> Ary(r - 1, 1) Then
to
VBA Code:
If Ary(r, 3) <> Ary(r - 1, 3) Then
you also should initialise the nr variable at the begining with this line right at the top:
VBA Code:
nr=1
 
Upvote 0
try changing this line:
VBA Code:
If Ary(r, 1) <> Ary(r - 1, 1) Then
to
VBA Code:
If Ary(r, 3) <> Ary(r - 1, 3) Then
you also should initialise the nr variable at the begining with this line right at the top:
VBA Code:
nr=1
Hi, thanks for the reply - I have updated my code and it did not change the output from what I am getting above.
Is there any other updates required as a result of making that change you suggested?
 
Upvote 0
For one example I have noticed that it is combining the data in columns A,B,C as one input
That's because the code was written to do just that. ;)

What is your data like & what should it look like at the end?
 
Upvote 0

Forum statistics

Threads
1,223,935
Messages
6,175,496
Members
452,649
Latest member
mr_bhavesh

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