Excel VBA Merge Cells for a Table run time error"1004"

Anonymous
2020-02-09T15:51:00+00:00

Table AccessDownload (A1:H172)  Merge cells where dates are the same and if description is closing Balance

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

65 additional answers

Sort by: Newest
  1. Anonymous
    2020-02-12T14:12:30+00:00

    Here is the macro. File can be downloaded from https://1drv.ms/x/s!AldvjX7HG_m7gUYczQqCHoInx2p...

    Option Explicit
    
    Sub MergeCells()
        Dim LastRow As Long, i As Long, j As Long, k As Long
        Dim Ws As Worksheet
        
        Set Ws = ActiveSheet
        LastRow = Ws.Cells(Rows.Count, 1).End(xlUp).Row
        'If there is no data, leave Sub
        If LastRow <= 2 Then
            MsgBox "Sheet has no data"
            Exit Sub
        End If
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        j = 1
        For i = 3 To LastRow
            With Ws
                If .Cells(i, "A") = .Cells(i - 1, "A") Then
                    If .Cells(i, "B") = .Cells(i - 1, "B") And .Cells(i, "B") = "Closing Balance" Then
                        If j = 1 Then
                            k = i - 1
                        End If
                        j = j + 1
                    End If
                    Else
                    If j >= 2 Then
                        .Cells(k, "A").Resize(j).Merge
                        .Cells(k, "B").Resize(j).Merge
                        j = 1
                    End If
                End If
            End With
        Next i
    ExitSub:
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
    End Sub
    

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2020-02-12T13:50:11+00:00

    Closing Balance have to be included for row 119 only

    Rows 17 and 18 remains as is

    Income Paid 0.0 for both and Grand Total row 17 $137,495.00

                                                   Grand Total row 18 $ 244,338.65

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2020-02-12T13:33:40+00:00

    I have a doubt, you have highlighted many cells in pink even though description is not Closing Balance.

    Do I need to ignore Closing Balance logic?

    Also see row numbers 17 and 18 where dates are same but descriptions are different.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2020-02-12T13:30:54+00:00

    Was this answer helpful?

    0 comments No comments