Combine multiple rows into a single row

kyddrivers

Board Regular
Joined
Mar 22, 2013
Messages
64
Office Version
  1. 365
Platform
  1. Windows
Here is a sample table of the data we need to combine into single rows of data based on the office.
Measure 1Measure 2Measure 3Measure 4Measure 5Measure 6Measure
7
Measure 8Measure 9
Office 1895089508950
Office 1373237323732
Office 1804580458045
Office 267286728672867286728
Office 26681668166816681
Office 3271227122712
Office 348144814
Office 3749474947494
Office 42137213721372137
Office 44515451545154515
Office 5258525851603160325851603

The number of measures will vary from project to project
The number of times an office repeats can vary, sometimes they will not repeat
Some offices may not have a value for each measure, but will not have more than 1 value for each measure

The ultimate goal is to combine all of an office's measure into a single line. Example: Office 5

Thank you in advance for your help!!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
something like this?
pqpt.jpg
 
Upvote 0
first, use Power Query
Code:
// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Unpivot = Table.UnpivotOtherColumns(Source, {"Office"}, "Attribute", "Value")
in
    Unpivot
second, create Pivot from this Query

but if you want vba maybe someone else will give you that
 
Upvote 0
Assuming ..
- One office never has more than one entry in a measure column then try
- There is nothing below this data and nothing in the column immediately to the right
.. try

VBA Code:
Sub CombineOffices()
  Dim a As Variant
  Dim i As Long, j As Long, k As Long
  
  a = Range("A1").CurrentRegion.Value
  k = 1
  For i = 2 To UBound(a)
    If a(i, 1) <> a(i - 1, 1) Then k = k + 1
    For j = 1 To UBound(a, 2)
      If Len(a(i, j)) > 0 Then a(k, j) = a(i, j)
    Next j
  Next i
  Range("A" & Rows.Count).End(xlUp).Offset(2).Resize(k, UBound(a, 2)).Value = a
End Sub

My sample data & results:
Book1
ABCDEFGHIJ
1Measure 1Measure 2Measure 3Measure 4Measure 5Measure 6Measure 7Measure 8Measure 9
2Office 1895089508950
3Office 1373237323732
4Office 1804580458045
5Office 267286728672867286728
6Office 26681668166816681
7Office 3271227122712
8Office 348144814
9Office 3749474947494
10Office 42137213721372137
11Office 44515451545154515
12Office 5258525851603160325851603
13
14Measure 1Measure 2Measure 3Measure 4Measure 5Measure 6Measure 7Measure 8Measure 9
15Office 1895037328045895037328045895037328045
16Office 2672866816728668167286681672866816728
17Office 374942712481474942712749448142712
18Office 4672821374515213745152137451521374515
19Office 52585668125851603160325851603
Combine Offices
 
Upvote 0
C12 vs C19 ?
Thanks. There some others too. Trying to be too clever. :oops:

Hopefully this is better. ?

VBA Code:
Sub CombineOffices_v2()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, lr As Long
  
  a = Range("A1").CurrentRegion.Value
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  For i = 2 To UBound(a)
    If a(i, 1) <> a(i - 1, 1) Then k = k + 1
    For j = 1 To UBound(a, 2)
      If Len(a(i, j)) > 0 Then b(k, j) = a(i, j)
    Next j
  Next i
  lr = Range("A" & Rows.Count).End(xlUp).Row
  Range("A" & lr + 2).Resize(, UBound(a, 2)).Value = a
  Range("A" & lr + 3).Resize(k, UBound(a, 2)).Value = b
End Sub

Book1
ABCDEFGHIJ
1Measure 1Measure 2Measure 3Measure 4Measure 5Measure 6Measure 7Measure 8Measure 9
2Office 1895089508950
3Office 1373237323732
4Office 1804580458045
5Office 267286728672867286728
6Office 26681668166816681
7Office 3271227122712
8Office 348144814
9Office 3749474947494
10Office 42137213721372137
11Office 44515451545154515
12Office 5258525851603160325851603
13
14Measure 1Measure 2Measure 3Measure 4Measure 5Measure 6Measure 7Measure 8Measure 9
15Office 1895037328045895037328045895037328045
16Office 2672866816728668167286681672866816728
17Office 374942712481474942712749448142712
18Office 421374515213745152137451521374515
19Office 5258525851603160325851603
Combine Offices
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,114
Members
452,302
Latest member
TaMere

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