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>
 
Company name is in C.
Geographic segment is in E and the 2016 data in F and the 2015 data in G.

EDIT: I simplified it. The headers (Business Segment, Company Name, 2016, 2015) of the real table start in row 7 and the columns are the way I wrote it above.
 
Last edited:
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Company name is in C.
Geographic segment is in E and the 2016 data in F and the 2015 data in G.

EDIT: I simplified it. The headers (Business Segment, Company Name, 2016, 2015) of the real table start in row 7 and the columns are the way I wrote it above.
Okay, for the layout you told us about above, the macro below should produce the output you want. Note that I set the output to go to Sheet2 starting at cell A1... if that is wrong, change the appropriate part of the code that I highlighted in red to the correct destination.
Code:
[table="width: 500"]
[tr]
	[td]Sub RearrangeData()
  Dim X As Long, R As Long, Rng As Range, FirstOutputCell As Range, Ar As Variant, Joined As String, Result As Variant
  Set Rng = Intersect(Range("C7", Cells(Rows.Count, "C").End(xlUp)).SpecialCells(xlConstants).EntireRow, Columns("C:G"))
  Set FirstOutputCell = Sheets("[COLOR="#FF0000"]Sheet2[/COLOR]").Range("[COLOR="#FF0000"]A1[/COLOR]")
  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, 4) & Ar(R, 5), "-", "")
      If Len(Joined) Then Result(X, 2) = Result(X, 2) & ", " & Ar(R, 3) & " (" & Format(100 * Joined, "General Number") & "%)"
    Next
    Result(X, 2) = Mid(Result(X, 2), 3)
  Next
  FirstOutputCell.Resize(, 2) = Rng(1).Resize(, 2).Value
  FirstOutputCell.Offset(1).Resize(UBound(Result), 2) = Result
  FirstOutputCell.Resize(, 2).EntireColumn.AutoFit
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Okay, for the layout you told us about above, the macro below should produce the output you want. Note that I set the output to go to Sheet2 starting at cell A1... if that is wrong, change the appropriate part of the code that I highlighted in red to the correct destination.
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub RearrangeData()
  Dim X As Long, R As Long, Rng As Range, FirstOutputCell As Range, Ar As Variant, Joined As String, Result As Variant
  Set Rng = Intersect(Range("C7", Cells(Rows.Count, "C").End(xlUp)).SpecialCells(xlConstants).EntireRow, Columns("C:G"))
  Set FirstOutputCell = Sheets("[COLOR=#FF0000]Sheet2[/COLOR]").Range("[COLOR=#FF0000]A1[/COLOR]")
  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, 4) & Ar(R, 5), "-", "")
      If Len(Joined) Then Result(X, 2) = Result(X, 2) & ", " & Ar(R, 3) & " (" & Format(100 * Joined, "General Number") & "%)"
    Next
    Result(X, 2) = Mid(Result(X, 2), 3)
  Next
  FirstOutputCell.Resize(, 2) = Rng(1).Resize(, 2).Value
  FirstOutputCell.Offset(1).Resize(UBound(Result), 2) = Result
  FirstOutputCell.Resize(, 2).EntireColumn.AutoFit
End Sub[/TD]
[/TR]
</tbody>[/TABLE]


Works really well! It just occasionally turns the value into a 5 or 6 number figure, for example 31307.3% instead of 31.3%.
 
Upvote 0
Okay, for the layout you told us about above, the macro below should produce the output you want. Note that I set the output to go to Sheet2 starting at cell A1... if that is wrong, change the appropriate part of the code that I highlighted in red to the correct destination.
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub RearrangeData()
  Dim X As Long, R As Long, Rng As Range, FirstOutputCell As Range, Ar As Variant, Joined As String, Result As Variant
  Set Rng = Intersect(Range("C7", Cells(Rows.Count, "C").End(xlUp)).SpecialCells(xlConstants).EntireRow, Columns("C:G"))
  Set FirstOutputCell = Sheets("[COLOR=#FF0000]Sheet2[/COLOR]").Range("[COLOR=#FF0000]A1[/COLOR]")
  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, 4) & Ar(R, 5), "-", "")
      If Len(Joined) Then Result(X, 2) = Result(X, 2) & ", " & Ar(R, 3) & " (" & Format(100 * Joined, "General Number") & "%)"
    Next
    Result(X, 2) = Mid(Result(X, 2), 3)
  Next
  FirstOutputCell.Resize(, 2) = Rng(1).Resize(, 2).Value
  FirstOutputCell.Offset(1).Resize(UBound(Result), 2) = Result
  FirstOutputCell.Resize(, 2).EntireColumn.AutoFit
End Sub[/TD]
[/TR]
</tbody>[/TABLE]


Hey Rick sorry to bother you again with this but I've come across a problem. I've come across tables that I need to work on that include negative values... Is there a way to amend the code so it can also include values that are negative?
 
Upvote 0
Hey Rick sorry to bother you again with this but I've come across a problem. I've come across tables that I need to work on that include negative values... Is there a way to amend the code so it can also include values that are negative?
This modified version of my previous code should work for you...
Code:
[table="width: 500"]
[tr]
	[td]Sub RearrangeData()
  Dim X As Long, R As Long, Rng As Range, FirstOutputCell As Range, Ar As Variant, Joined As String, Result As Variant
  Set Rng = Intersect(Range("C7", Cells(Rows.Count, "C").End(xlUp)).SpecialCells(xlConstants).EntireRow, Columns("C:G"))
  Set FirstOutputCell = Sheets("Sheet2").Range("A1")
  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)
      If Ar(R, 4) = "-" Then Ar(R, 4) = ""
      If Ar(R, 5) = "-" Then Ar(R, 5) = ""
      Joined = Ar(R, 4) & Ar(R, 5)
      If Len(Joined) Then Result(X, 2) = Result(X, 2) & ", " & Ar(R, 3) & " (" & Format(100 * Joined, "General Number") & "%)"
    Next
    Result(X, 2) = Mid(Result(X, 2), 3)
  Next
  FirstOutputCell.Resize(, 2) = Rng(1).Resize(, 2).Value
  FirstOutputCell.Offset(1).Resize(UBound(Result), 2) = Result
  FirstOutputCell.Resize(, 2).EntireColumn.AutoFit
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,216,073
Messages
6,128,645
Members
449,461
Latest member
kokoanutt

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