1
我正試圖將相鄰行中的單元格合併到已合併的單元格中。如果合併列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
解決: '範圍(「C」&Lrow&「:D」&Lrow).Merge True'這個作品也是我剛剛發現的。 – XLmatters
是的,我在開始的時候就已經這樣做了,但是我選擇了「更容易打字」:D。 –
感謝您的幫助。 – XLmatters