Merge cells on formula values

NikkiMc204

New Member
Joined
May 24, 2021
Messages
23
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
Hello wise ones

I know everyone hates merged cells but i've asked/tasked with merging cells in a work spreadsheets. The information is pulled from another sheet with a formula =IFERROR(@INDEX(Table1[Project Name],FLOOR((ROW()-5)/10,1)+1),"") so each project line is repeated exactly 10 times and there's currently 70 odd projects so 700 rows.

I'm not allowed to download any helpful add ons like kutools that can quickly click and merge cells with formulas.

The outcome I'm after is to be able to merge the cells in columns b c d e f (every ten rows) column g is more variable (might be 10 or 20 rows) but i'd also like to merge where values are the same but i don't want to lose the values in order to ultimately pivot all the results

Is this even possible to do? open to all and any suggestions 😬

Merge cells.png
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Yes, It is possible. How should the Merged values be displayed/formatted?
 
Upvote 0
Question ... will your Sheet alway be sorted by Project Title; i.e. can you assume that project records will be grouped together in the Sheet?

Here is a VBA Macro that will:
  • turn off the warnings
  • saves the original text from the cells
  • merges the cells
  • restores the saved text (one line for cell value merged. merged data is centered vertically and horizontally.
  • changes the cell height so that the merge data displays (with linefeeds)
  • restores warning messages.
I'll assume you can write the code to do the merge in the rows where you need it. Provide feedback if you need additional help.

Hint: an easy way to get started on code like this is to Turn On "Record Macro". Do the steps you want to do (and repeat). Turn Off (Stop) macro recording.
This will create a VBA macro you can start to work with and edit.

Sample code:
VBA Code:
Sub Macro1()
'
' Macro1 Macro
'
  Dim rng As Range
  Dim ary, i
  Dim s As String
'
   
    Application.DisplayAlerts = False
    Set rng = Selection
    rng.Select
    ary = rng.Value
   
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = True
        For i = 1 To 5
          s = s & vbLf & ary(1, i)
        Next i
        .Value = Trim(s)
        .Height = 80
    End With
  Applcation.DisplayAlerts = True
End Sub
 
Upvote 0
How about something like this ... ?

Before Merge
MergeCells.xlsm
ABCDEFG
1
2
3RowProject TitleStartEndStatusProject LeadDirectorate
4Project 14499245137DeliveredName AD1
5Project 14499245137DeliveredName AD2
6Project 14499245137DeliveredName AD3
7Project24483545124PlanningName BD1
8Project24483545124PlanningName BD2
9Project24483545124PlanningName BD3
10Project24483545124PlanningName BD4
11Project24483545124PlanningName BD5
12Project34450145232OrderedName CD1
13Project 44458155001DeliveredName DD1
14Project 44458155001DeliveredName DD2
15Project 44458155001DeliveredName DD3
16Project 44458155001DeliveredName DD4
17Project 44458155001DeliveredName DD5
18Project 44458155001DeliveredName DD6
19Project 44458155001DeliveredName DD7
20Project 44458155001DeliveredName DD8
21Project 44458155001DeliveredName DD9
22Project 44458155001DeliveredName DD10
Sheet1


After Merge
MergeCells.xlsm
ABCDEFG
1
2
3RowProject TitleStartEndStatusProject LeadDirectorate
4Project 1, 44992, 45137, Delivered, Name AD1
5D2
6D3
7Project2, 44835, 45124, Planning, Name BD1
8D2
9D3
10D4
11D5
12Project34450145232OrderedName CD1
13Project 4, 44581, 55001, Delivered, Name D, , , ,D1
14D2
15D3
16D4
17D5
18D6
19D7
20D8
21D9
22D10
Sheet3


VBA Code:
Option Explicit

Sub Macro1()
'
' Macro1 Macro
'
  Dim rng As Range, rngMerge As Range
  Dim ary
  Dim s As String
  Dim r As Long, lr As Long, i As Long, rBegMerge As Long, rStart As Long
  Dim projRcds As Long
  Dim project As String
  
'
  rStart = WorksheetFunction.Match("Project Title", Range("B:B"), 0)
  lr = Range("B" & Rows.Count).End(xlUp).Row
    
  Application.DisplayAlerts = False
  
  rStart = rStart + 1
  project = ""
  projRcds = 0
  For r = rStart To lr + 1
    Set rng = Range("B" & r & ":F" & r)
    If project <> rng(1, 1) Then
      If projRcds > 1 Then 'merge cells
        Set rngMerge = Range("B" & rBegMerge & ":F" & r - 1)
      
        With rngMerge
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
            For i = 1 To 5
              s = s & ", " & ary(1, i)
            Next i
            .Value = Mid(Trim(s), 3)
            s = ""
        End With
        project = rng(1, 1)
        projRcds = 0
      Else
      End If
      rBegMerge = r
      ary = rng.Value 'save the range data Cols B thru F
      project = rng(1, 1)
    End If
    projRcds = projRcds + 1
  Next r
  Application.DisplayAlerts = True
End Sub
 
Upvote 0
OK --- how does this look?
Note the Macro code does not add the interior and exterior border lines. I did this manually in the unmerged Sheet.
The merge code does not modify the border lines, other than what happens when cells are merged.
(Of course this code could be added to the merge code in the future.)

Just a reminder this code does not sort the Sheet data. It assumes that the records are presorted and grouped together by Project Name.

Sheet Before merging cells
MergeCells.xlsm
ABCDEFG
1
2
3RowProject TitleStartEndStatusProject LeadDirectorate
4Project17/30/20237/31/2023On TrackName AD1
5Project17/30/20237/31/2023On TrackName AD2
6Project17/30/20237/31/2023On TrackName AD3
7Project17/30/20237/31/2023On TrackName AD4
8Project17/30/20237/31/2023On TrackName AD5
9Project17/30/20237/31/2023On TrackName AD6
10Project17/30/20237/31/2023On TrackName AD7
11Project17/30/20237/31/2023On TrackName AD8
12Project17/30/20237/31/2023On TrackName AD9
13Project17/30/20237/31/2023On TrackName AD10
14Project 27/16/20237/25/2023In ProgressName AD1
15Project 27/16/20237/25/2023In ProgressName AD2
16Project 27/16/20237/25/2023In ProgressName AD3
17Project 27/16/20237/25/2023In ProgressName AD4
18Project 27/16/20237/25/2023In ProgressName AD5
19Project 27/16/20237/25/2023In ProgressName AD6
20Project 27/16/20237/25/2023In ProgressName AD7
21Project 27/16/20237/25/2023In ProgressName AD8
22Project 27/16/20237/25/2023In ProgressName AD9
23Project 27/16/20237/25/2023In ProgressName AD10
Sheet1


Sheet After merging cells
MergeCells.xlsm
ABCDEFG
1
2
3RowProject TitleStartEndStatusProject LeadDirectorate
4Project17/30/20237/31/2023On TrackName AD1
5D2
6D3
7D4
8D5
9D6
10D7
11D8
12D9
13D10
14Project 27/16/20237/25/2023In ProgressD1
15D2
16D3
17D4
18D5
19D6
20D7
21D8
22D9
23D10
Sheet3


VBA Macro

VBA Code:
Sub MergeProjectCells()
'
' Macro1 Macro
'
  Dim rng As Range, rngMerge As Range
  Dim c
  Dim ary
  Dim aryidx As Long
  Dim s As String
  Dim r As Long, lr As Long, i As Long, rBegMerge As Long, rStart As Long
  Dim projRcds As Long
  Dim project As String
  Dim shading As Boolean
  
'
  rStart = WorksheetFunction.Match("Project Title", Range("B:B"), 0)
  lr = Range("B" & Rows.Count).End(xlUp).Row
    
  Application.DisplayAlerts = False
  
  rStart = rStart + 1
  rBegMerge = rStart
  project = ""
  projRcds = 0
  shading = False
  For r = rStart To lr + 1
    Set rng = Range("B" & r & ":F" & r) ' & ",G" & r)
    'Look for a change in the project name
    If project <> rng(1, 1) Then
      If projRcds > 0 Then 'If more than 1 records for the Project Name merge them together
        If projRcds > 1 Then  'merge cells
          aryidx = 1
          For c = Asc("B") To Asc("F") 'do the merge Column by Column (Col B to E)
            Set rngMerge = Range(Chr(c) & rBegMerge & ":" & Chr(c) & r - 1)
            With rngMerge
              .HorizontalAlignment = xlCenter
              .VerticalAlignment = xlCenter
              .WrapText = False
              .Orientation = 0
              .AddIndent = False
              .IndentLevel = 0
              .ShrinkToFit = False
              .ReadingOrder = xlContext
              .MergeCells = True
              .Value = ary(1, aryidx)
            End With
            aryidx = aryidx + 1
          Next c
          project = rng(1, 1)
          projRcds = 0
        End If
        'Change the background shading for each project name change
        Set rngMerge = Range("B" & rBegMerge & ":E" & (r - 1) & ",G" & rBegMerge & ":G" & (r - 1))
        With rngMerge.Interior
          If shading Then
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.14996795556505  'change shading color here
          Else
            .Pattern = xlNone
            .TintAndShade = 0 'shading set to no color
          End If
          .PatternTintAndShade = 0
        End With
        shading = Not shading 'this toggles shading on and off
        rBegMerge = r
      End If
      
      ary = rng.Value 'save the range data Cols B thru F
      project = rng(1, 1)
    End If
 
    projRcds = projRcds + 1
  Next r
  
  'Merge all consecutive Project Lead values
  project = ""
  projRcds = 0
  rBegMerge = rStart
  For r = rStart To lr + 1
    Set rng = Range("F" & r)
    'Look for a change in the project name
    If project <> rng.Value And Not IsEmpty(rng.Value) Or r = lr Then
      If projRcds > 0 Then 'If more than 1 records for the Project Name merge them together
        If projRcds > 1 Then  'merge cells
          Set rngMerge = Range("F" & rBegMerge & ":" & "F" & r - 1)
          With rngMerge
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
            .Value = project
          End With
          project = rng.Value
          projRcds = 0
        End If
      End If
      
      ary = rng.Value 'save the range data Cols B thru F
      project = rng.Value
    End If
 
    projRcds = projRcds + 1
  Next r

  
  Application.DisplayAlerts = True
End Sub
 
Upvote 1
Solution
This is perfect 😍 thank you for taking the time to share this code with me
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,956
Members
449,096
Latest member
Anshu121

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