0

I can't use ctrl z(Undo) after using the following code in excel 2019.

I want to merge 8 rows into a cell by using macros but I hope it can undo.

here is the code:

 
Sub main()
    Dim i As Long

    With ActiveSheet
        For i = 1 To .Cells(Rows.Count, 1).End(xlUp).Row Step 8
            With .Cells(i, 1).Resize(8)
                .Merge
                .HorizontalAlignment = xlCenterAcrossSelection
                .VerticalAlignment = xlCenter
            End With
            With .Cells(i, 2).Resize(8)
                .Merge
                .HorizontalAlignment = xlCenterAcrossSelection
                .VerticalAlignment = xlCenter
            End With
        With .Cells(i, 3).Resize(8)
                .Merge
                .HorizontalAlignment = xlCenterAcrossSelection
                .VerticalAlignment = xlCenter
            End With
        With .Cells(i, 4).Resize(8)
                .Merge
                .HorizontalAlignment = xlCenterAcrossSelection
                .VerticalAlignment = xlCenter
            End With
        Next i
    End With
End Sub
braX
  • 11,506
  • 5
  • 20
  • 33
  • 1
    Excel VBA result cannot be undone. This is the way Excel behaves... – FaneDuru Jan 23 '22 at 14:54
  • https://stackoverflow.com/questions/33813806/is-it-possible-to-undo-a-macro-action – Ian Kenney Jan 23 '22 at 15:01
  • You can use [Application.OnUndo](https://learn.microsoft.com/en-us/office/vba/api/excel.application.onundo) to specify a Sub to run when Undo is called. You have to code the Undo actions yourself. Note the regular Excel Undo stack is cleared whenever VBA takes an action that changes a something in Excel – chris neilsen Jan 23 '22 at 19:04

0 Answers0