Merging Cells

Ginger-daq

New Member
Joined
Feb 15, 2008
Messages
13
Hi,

How would I go about merging cells with the same values AUTOMATICALLY?

For instance in cells A1:E1, each cell contains a value of 4 except E1 so I require A1:D1 merged.

Driving me insane!! limited excel and VBA knowledge sorry.


Ginger-daq
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Any adjacent cell that has the same value should be merged? All in one row, or could be multiple rows?
 
Upvote 0
Select your region where you want the merge operation to take place, and run this macro:

Code:
Public Sub mergeSameCell()
  Dim rng As Excel.Range, rngPrev As Excel.Range
  Dim rngAll As Excel.Range, rngMerge As Excel.Range
  Dim rngBegin As Excel.Range, rngEnd As Excel.Range
  
  If VBA.TypeName(Selection) <> "Range" Then Exit Sub
  
  Set rngAll = Application.Selection
  
  Application.Calculation = Excel.xlCalculationManual
  Application.ScreenUpdating = False
  
  For Each rng In rngAll.Cells
    If rngBegin Is Nothing Then
      Set rngBegin = rng
    ElseIf rng.Value <> rngPrev.Value Or rng.Row <> rngPrev.Row Then
      Set rngEnd = rngPrev
      Set rngMerge = Range(rngBegin, rngEnd)
      If rngMerge.Cells.Count > 1 Then
        Application.DisplayAlerts = False
        rngMerge.Merge
        Application.DisplayAlerts = True
      End If
      Set rngBegin = rng
    End If
    Set rngPrev = rng
  Next rng
  
  Set rngEnd = rngAll.Cells(rngAll.Cells.Count)
  Set rngMerge = Range(rngBegin, rngEnd)
  
  If rngMerge.Cells.Count > 1 Then
    Application.DisplayAlerts = False
    rngMerge.Merge
    Application.DisplayAlerts = True
  End If
  
  Application.Calculation = Excel.xlCalculationAutomatic
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Away from my laptop today but certainly will try thanks!!... it is multiple rows however? Will that make a difference?


Ginger-Daq
 
Upvote 0
Sorry let me give you some info to work on... Each cell is formulated with:

=IF(AND($B5<=G$1,$C5>=G$1),(1*$D5),"")... only an example

So do not want cells with values of "" or 0 to be merged...

I am trying to show workload visually for allocators to work from when allocating a large number of staff over a 24 hour period.

So the formulae above will show (visually) the allocation: Start to Finish time and how many staff allocated. The reason I need the cells merged is so that I can show details of the allocation rather than number of staff upon selection.


Hope this gives you a better idea??????


Ginger-Daq
 
Upvote 0
Then you need an additional condition, next to the one checking for the first cell, like this:

Code:
  For Each Rng In rngAll.Cells
    If rngBegin Is Nothing [COLOR=red][B]Or CInt(Rng.Value) = 0[/B][/COLOR] Then
      Set rngBegin = Rng
    ElseIf Rng.Value <> rngPrev.Value Or Rng.Row <> rngPrev.Row Then
      Set rngEnd = rngPrev
      Set rngMerge = Range(rngBegin, rngEnd)
      If rngMerge.Cells.Count > 1 Then
        Application.DisplayAlerts = False
        rngMerge.Merge
        Application.DisplayAlerts = True
      End If
      Set rngBegin = Rng
    End If
    Set rngPrev = Rng
  Next Rng
 
Upvote 0
Select that range and run the code.
 
Upvote 0
Legend!!

Ah I see!... OK tried that but it wouldn't accept the amended (red) bit?

Run Time Error 13... so as guessed using your first post the zeros get merged still?

Any idea's?
 
Upvote 0
Do you have non-numeric text in some of the cells?
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,831
Members
449,051
Latest member
excelquestion515

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