How to automatically merge cells?

29,004

Extending Jon Fournier's answer, I've changed the range calculation to look for non-blank cells and added code to turn off the warning dialog that Merge throws up. I also changed the function to Public so I could run it from the Macros dialog.

Public Sub AutoMerge()

Dim LastRowToMergeTo As Long
Dim i As Long
Dim LastRow As Long

Application.DisplayAlerts = False

LastRow = Range("S" & CStr(Rows.Count)).End(xlUp).Row

For i = 2 To LastRow

    LastRowToMergeTo = i
    Do While (Len(Range("D" & CStr(LastRowToMergeTo + 1)).Value) = 0) And (LastRowToMergeTo <> LastRow)
        LastRowToMergeTo = LastRowToMergeTo + 1
    Loop

    With Range("D" & CStr(i) & ":D" & CStr(LastRowToMergeTo))
        .Merge
        .WrapText = True
        .VerticalAlignment = xlVAlignTop
    End With

    i = LastRowToMergeTo

Next i

Application.DisplayAlerts = True

End Sub

Jon's second part, which should run the macro at every recalculate, doesn't seem to work but doesn't matter to me for the small amount of updating I'm doing.

Share:
29,004
double-beep
Author by

double-beep

👋 Hi curious user! 🙌 Tahnks 👏 you 🙏 in advancemnets for visiting my profile page! If you want to discuss 💬 with me about one of my actions or have a userscript idea (😊), drop into Userscript newbies and friends (🤞). Annoyed by the lack of highlighting in revisions and review because of this hotfix? You can use this script of mine to restore it! I am a bot room owner at SOBotics, where I helped in the development of Belisarius (StackApps), a bot that detects for potential vandalism on Stack Overflow. Actively rolling back edits that: add answers/solutions to the question. In general, those should NOT be edited into questions. Self-answering/accepting is encouraged and is what should be done instead. change a question into a different one (potentially invalidating any existing answers). Users on Stack Overflow are more than happy to help, but new/additional issues need to be posted as separate questions.

Updated on April 22, 2020

Comments

  • double-beep
    double-beep about 4 years

    I have an Excel table with several items 1, 2, 3..., each of which has subitems 1.1, 1.2, etc. I'm using the list of subitems as my key column and populating the main items using vlookups, but only showing each main item once.

    /|    A    |    B     |    C     |
    -+---------+----------+----------+
    1| Item1   |  1.Note  |  Item1.1 |
    2|         |          |  Item1.2 |
    3|         |          |  Item1.3 |
    4| Item2   |  2.Note  |  Item2.1 |
    5|         |          |  Item2.2 |
    6|         |          |  Item2.3 |
    7|         |          |  Item2.4 |
    8| Item3   |  3.Note  |  Item3.1 |
    9|         |          |  Item3.2 |
    0|         |          |  Item3.3 |
    

    Column C is raw data; A and B are formulas.

    Column B has notes, so the text may be long. I want to wrap the notes to take up all the rows available. I can do this manually by selecting B1:B3 and merging them, but then it won't update if I add items to column C.

    I don't care if the cells are merged or just wrapped and overlapping.

    Can this be done in formulas or VBA?

  • Admin
    Admin over 15 years
    Thanks for your response, this looks promising. Unfortunately Range("B" & CStr(i)).End(xlDown).Row doesn't work because the cells that show empty are not, they contain formulas. The code merges the entire column to the bottom of the last subitem.
  • Admin
    Admin over 15 years
    I can iterate through the cells looking for the next non-blank cell. The problem is that merge pops up a dialog box every time asking if I'm sure I want to overwrite all the cells being merged. Any way to get rid of the dialog box?