合并相似的相邻单元格 - 对于列

Merging similar adjacent cells - for columns

我有函数 mergeSimilarAdjacentCells 用于在第一行合并我的 Excel headers。 第一行的 headers 开始于未知列,结束于未知列。

该功能对我有用,但每次合并我都会收到此消息!

输入数据示例:

我得到的输出:

我应该怎么做才能停止收到这些弹出消息?

我的代码:

Function mergeSimilarAdjacentCells()

Dim Rng_headers As Range
Dim numOfDataCols As Long
Dim myCell As Variant

numOfDataCols = ActiveSheet.Cells(1, Columns.count).End(xlToLeft).Column

Set Rng_headers = Range("A" & 1 & ":" & Col_Letter(numOfDataCols) & 1)

MergeAgain:
   For Each myCell In Rng_headers
      If myCell.Value = myCell.Offset(0, 1).Value And IsEmpty(myCell) = False Then
          Range(myCell, myCell.Offset(0, 1)).Merge
          GoTo MergeAgain
      End If
   Next

End Function

合并时请尝试这种方式:

'your existing code
   For Each myCell In Rng_headers
      If myCell.value = myCell.Offset(0, 1).value And IsEmpty(myCell) = False Then
            Application.DisplayAlerts = False
                Range(myCell, myCell.Offset(0, 1)).merge
            Application.DisplayAlerts = True
           GoTo MergeAgain
      End If
   Next

消息正常,Excel想防止误丢数据,不知道只保留了左上角的值