Macro to merge cells

dment

New Member
Joined
Dec 4, 2008
Messages
16
Hi

I a trying to create a macro that will merge a number of cells in a column based on a value in another cell

the spreadsheet is laid out

Column A Column B Column C Column D Column E

City ProductID Cost Total Amount Count
London 001 £1.00 £7.00 5
London 002 £2.00
London 008 £8.00
London 004 £4.00
London 002 £2.00
Birmingham 003 £3.00 £20.00 5
Birmingham 005 £5.00
Birmingham 004 £4.00
Birmingham 005 £5.00
Birmingham 003 £3.00
Manchester 001 £1.00 £16.00 6
Manchester 002 £2.00
Manchester 004 £4.00
Manchester 001 £1.00
Manchester 005 £5.00
Manchester 003 £3.00

I need to merge column D for each city.
I had a macro that used xlDown but this takes it down to the bottom of the workbook rather than the next cell which has a figure in (This is due to the spreadsheet having a formula in the cells) so I think I will need to use the count figure in column E

Any help will be much appreciated.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
If you need the sum of the Cost, try this


Book1
ABCDEF
1CityProductIDCostTotalAmountCount
2London11175
3London22
4London88
5London44
6London22
7Birmingham33205
8Birmingham55
9Birmingham44
10Birmingham55
11Birmingham33
12Manchester11166
13Manchester22
14Manchester44
15Manchester11
16Manchester55
17Manchester33
Sheet1
Cell Formulas
RangeFormula
D2=IF(COUNTIF($A$2:A2,A2)=1,SUMIF($A$2:$A$17,A2,$C$2:$C$17),"")
 
Upvote 0
Thanks but it's not the sum that I want. The example didn't come out as it should have.

Based on your pic column D should be Total Amount and column E is the count.

I need Macro that will merge column D based on the count n column E

so Cells D2 to D6 will be merged. D7 to D11 will be merged and so on
 
Upvote 0
Based on my pic, cell D2 has value, cells D3 to D6 are empty, and so on.
If the above is correct, then try the following:

Code:
Sub merge_cells()
  Dim c As Range
  For Each c In Range("D2:D" & Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeBlanks).Areas
    With c.Offset(-1).Resize(c.Rows.Count + 1)
      .MergeCells = True
      .HorizontalAlignment = xlCenter
      .VerticalAlignment = xlCenter
    End With
  Next
End Sub
 
Upvote 0
Thank you. That works in my test spreadsheet but not on the one I need it to work on. problem is the spreadsheet is created from one that has formulas in column D so while the cells are blank End + Down takes you to the end of the column rather than the next number. If I click in each cell in column D before running the macro it works fine. This is why I included column E which holds the number of cells to be merged.

Any ideas of how best to get round this problem?
 
Upvote 0
If col D had formulae that returned "", try
Code:
Sub merge_cells()
    Dim c As Range
    With Range("D2:D" & Range("A" & Rows.Count).End(xlUp).Row)
        .Value = .Value
        For Each c In .SpecialCells(xlCellTypeBlanks).Areas
            With c.Offset(-1).Resize(c.Rows.Count + 1)
                .MergeCells = True
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
            End With
        Next c
    End With
End Sub
 
Last edited:
Upvote 0
In column E do you also have formulas?

Do you want to keep the formula in column D, in this case on the merged cell?

Try this:

Code:
Sub merge_cells2()
  Dim ant As Variant, i As Long, ini As Long
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  ini = 2
  ant = Range("A2").Value
  For i = 2 To Range("A" & Rows.Count).End(xlUp).Row + 1
    If ant <> Cells(i, "A").Value Then
      With Range("D" & ini & ":D" & i - 1)
        .MergeCells = True
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
      End With
      ini = i
    End If
    ant = Cells(i, "A").Value
  Next
End Sub
 
Upvote 0
Thanks. That is almost what I am after. the cells merge OK apart from the last one. the original formula goes down to row 25 so the amount for Manchester merges D12 to D25 instead of D12 to D17
 
Upvote 0
Thanks. That is almost what I am after. the cells merge OK apart from the last one. the original formula goes down to row 25 so the amount for Manchester merges D12 to D25 instead of D12 to D17

This was in reply to the macro from Fluff
 
Upvote 0
I don't know how you have the formulas.
You could put how you have the formulas.
Or change the references of the formulas to absolute, that is, if you have this: D12:D17 you must change it to: $D$12:$D$17
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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