Concatenate with obstacles

olfleck

New Member
Joined
Mar 8, 2017
Messages
9
Hi,
I have the following excerpt from a system (Table 1). However I need to change this input and make it look like the Table 2.
The data is not very homogenous, which makes it very troublesome to use the CONCATENATE or any other Function. Sometimes the system can only find old data (see company B) so then I have to work with this. Sometimes it puts Geographic Segments and says they are 0(-) as you can see with company A below, which should not be in the output.
It usually is more than 3 companies and each of them can have various numbers of segments they operate in.

The output for each company should look like the following:
Segment 1 (Percentage), Segment 2 (Percentage),...

Can anyone help me to write a macro that solves this problem?

Let me know if something is not clear and thank you in advance!

Table 1 (INPUT)
Company NameGeographic Segments20162015
1AUnited States 54.5%-
2ACanada 6.8%-
3AChina 4.9%-
4AGermany 4.4%-
5AAll Other 29.4%-
6AUnited Kingdom--
7ANetherlands--
Company NameGeographic Segments20162015
1BEast China- 12.4%
2BSouthern China- 78.0%
3BCentral China- 3.9%
Company NameGeographic Segments20162015
1CCanada- 3.4%
2CUnited Kingdom- 2.1%
3CSwitzerland--
4CBrazil- 1.0%
5CMexico- 0.9%

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

OUTPUT:
Company NameGeographic Segments
AUnited States (54.5%), All Other (29.4%), Canada (6.8%), China (4.9%), Germany (4.4%)
BSouthern China (78%), East China (12.4%), Central China (3.9%)
CCanada (3.4%), United Kingdom (2.1%), Brazil (1%), Mexico (0.9%)

<colgroup><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Give this macro a try...
Code:
[table="width: 500"]
[tr]
	[td]Sub RearrangeData()
  Dim X As Long, R As Long, Rng As Range, Ar As Variant, Joined As String, Result As Variant
  Set Rng = Intersect(Columns("A").SpecialCells(xlConstants).EntireRow, Columns("A:D"))
  Const FirstOutputCell As String = "H1"
  ReDim Result(1 To Rng.Areas.Count, 1 To 2)
  For X = 1 To Rng.Areas.Count
    Ar = Rng.Areas(X)
    Result(X, 1) = Ar(2, 1)
    For R = 2 To UBound(Ar)
      Joined = Replace(Ar(R, 3) & Ar(R, 4), "-", "")
      If Len(Joined) Then Result(X, 2) = Result(X, 2) & ", " & Ar(R, 2) & " (" & Format(100 * Joined, "General Number") & "%)"
    Next
    Result(X, 2) = Mid(Result(X, 2), 3)
  Next
  Range(FirstOutputCell).Resize(, 2) = Rng(1).Resize(, 2).Value
  Range(FirstOutputCell).Offset(1).Resize(UBound(Result), 2) = Result
  Range(FirstOutputCell).Resize(, 2).EntireColumn.AutoFit
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Thanks for the quick answer! It gives me a type mismatch error. Tried defining everything as Variant but didn't help..
 
Upvote 0
Thanks for the quick answer! It gives me a type mismatch error. Tried defining everything as Variant but didn't help..
Just telling us there is an error does not help. Run my code again and when the error message appears, click the Debug button and tell us what line of code is highlighted in yellow.

Edit Note 1: Also, tell us what columns your data are in.

Edit Note 2: When you said in your original post that your input data was "Table 1"... did you mean a real, Excel table object (with the name "Table 1") or just a range of cells that you think of as being a table.
 
Last edited:
Upvote 0
If you are on an Excel version which has the TEXTJOIN version...

In G1 control+shift+enter, not just enter, and copy down:

=TEXTJOIN(",",TRUE,IF($A$2:$A$20=$F1,IF(ISNUMBER($C$2:$C$20)," "&$B$2:$B$20&" ("&$C$2:$C$20&")",IF(ISNUMBER($D$2:$D$20)," "&$B$2:$B$20&" ("&$D$2:$D$20&")","")),""))

where F1 = A, etc.
 
Upvote 0
If you are on an Excel version which has the TEXTJOIN version...

In G1 control+shift+enter, not just enter, and copy down:

=TEXTJOIN(",",TRUE,IF($A$2:$A$20=$F1,IF(ISNUMBER($C$2:$C$20)," "&$B$2:$B$20&" ("&$C$2:$C$20&")",IF(ISNUMBER($D$2:$D$20)," "&$B$2:$B$20&" ("&$D$2:$D$20&")","")),""))

where F1 = A, etc.



I only have Excel 2013 should have mentioned that... Don't think it works in there. Thank you anyway!!
 
Upvote 0
Just telling us there is an error does not help. Run my code again and when the error message appears, click the Debug button and tell us what line of code is highlighted in yellow.

Edit Note 1: Also, tell us what columns your data are in.

Edit Note 2: When you said in your original post that your input data was "Table 1"... did you mean a real, Excel table object (with the name "Table 1") or just a range of cells that you think of as being a table.

I just meant the table that I posted. There table is not called Table 1. The following is highlighted:

Result(X, 2) = Result(X, 2) & ", " & Ar(R, 2) & " (" & Format(100 * Joined, "General Number") & "%)"


The segments are in E and the numbers in F (for 2016) or E (for 2015).
 
Upvote 0
I just meant the table that I posted. There table is not called Table 1. The following is highlighted:

Result(X, 2) = Result(X, 2) & ", " & Ar(R, 2) & " (" & Format(100 * Joined, "General Number") & "%)"


The segments are in E and the numbers in F (for 2016) or E (for 2015).


But I tried it in my sample sheet which looks exactly like the one I posted. With the first column being column A.
 
Upvote 0
The segments are in E and the numbers in F (for 2016) or E (for 2015).
I assume the "segments" are what you labeled in your table as "Geographic Segments"... if those are in Column E, then how can the 2015 numbers also be in Column E? In addition, in order to write code to address your data, we need to know where are the parts are (can't pull data from them if we don't know where they are). Tell us the column letters for each of these columns...

Company Name:

Geographic Segments:

2016:

2015:

EDIT NOTE: Your response in Message #8 makes it sound like you simplified your layout for this question and that your actual data may not be contiguous like your sample shows. If that is the case, please tell us where your data really is (we code to what you tell us... most times the solution to the simplified layout does not easily scale to a more disjointed layout).
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,209
Members
448,874
Latest member
b1step2far

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