【发布时间】:2017-07-24 06:37:33
【问题描述】:
我正在尝试将相邻行中的单元格合并到已合并的单元格中。如果合并了 D 列中的相邻单元格,我想合并 C 列中的相邻单元格。并非所有单元格都合并在 D 列中。当我在 MsgBox 中使用变量时,我有下面的代码为我提供正确的行号,但是,当我将变量添加到要合并的范围时,每一行都会被合并。我想它一定很简单,但我只能确定是什么导致范围内的每一行被合并。我通常不会合并任何东西,但我需要将这些合并的单元格留在电子表格中。非常感谢您帮助破解这个问题。
Sub FindMerge()
Dim cell As Range
Dim Lrow As Long
Application.ScreenUpdating = False 'Turn off screen updating. Code runs faster without screen flicker
Application.DisplayAlerts = False 'stops Windows Alerts from poping up
'loops through range to find merged cells
'For Each cell In ActiveSheet.UsedRange 'commented out to try static range below.
For Each cell In Range("D1:D81")
If cell.MergeCells Then
If cell.Address = cell.MergeArea.Cells(1, 1).Address Then
' Msgbox "Row: " & cell.row 'displays correct row number where merged cell is located
Lrow = cell.row
Range("C2:O" & Lrow).Merge True 'Unintentionally merges every row
End If
End If
Next cell
Application.ScreenUpdating = True 'Turns screen updating back on
Application.DisplayAlerts = True 'Turns Windows Alerts back on
End Sub
【问题讨论】: