![見出し画像](https://assets.st-note.com/production/uploads/images/160750593/rectangle_large_type_2_b3bffa90ab6859cb80e040e68cdd9010.png?width=1200)
セル結合を解除してブランクをうめるよ
この説明は、ChatGPTで作成しています。
このVBAプロシージャは、結合されているセルを解除し、もともと結合されていたセルの内容を隣接するセルにも反映させるものです。たとえば、結合セルがあった部分が空白になっているときに、そこに値をコピーするのに便利です。
プロシージャの説明
画面の更新を停止
最初に `Application.ScreenUpdating = False` によって画面の更新を一時的に止め、作業中の画面のちらつきを防ぎます。最後に `True` に戻して、通常の画面表示に戻します。エラーの一時無視
`On Error Resume Next` はエラーが起きたときにプログラムが止まらずに次の操作に進むための指示です。このプロシージャでは、セルの結合を解除するときに発生する可能性のあるエラーを無視するようにしています。結合セルの確認と解除
選択範囲の各セルについて For Each ループ を使い、`If myRng.MergeCells` の条件によって、セルが結合されているか確認します。結合されていれば、そのセルの `MergeArea` プロパティで結合エリア全体を選択します。結合の解除と値のコピー
`UnMerge` メソッドで結合を解除し、`Resize(1, 1).Value` を使用して、最初のセルの値を解除後の範囲すべてにコピーします。これで、結合していたセルの値が、もとの結合範囲に均等に埋め込まれるようになります。画面更新の再開
最後に `Application.ScreenUpdating = True` にすることで、画面の更新が再開され、通常の操作ができるようになります。
参考にしたウェブサイト
Sub セル結合を解除してブランクをうめるよ()
Application.ScreenUpdating = False
On Error Resume Next
Dim myRng As Range
For Each myRng In Selection
If myRng.MergeCells Then
With myRng.MergeArea
.UnMerge
.Value = .Resize(1, 1).Value
End With
End If
Next myRng
Application.ScreenUpdating = True
End Sub
キーワード
#excel #できること #vba #セル結合解除 #セル結合 #ブランク埋める #選択範囲 #UnMerge #MergeArea #値コピー #結合セル #セル操作 #画面更新 #マクロ #ForEachループ #Application #Range #エラー無視 #セル範囲 #ScreenUpdating
English Explanation
Unmerge Cells and Fill Blanks
This explanation was created by ChatGPT.
This VBA procedure unmerges cells and fills blank cells within the unmerged range with the original value. It's particularly useful for unmerging cells while preserving values in every cell within the previously merged area.
Procedure Explanation
Stop Screen Updating
`Application.ScreenUpdating = False` pauses screen updating to prevent flickering. Finally, it resets to `True`.Temporarily Ignore Errors
`On Error Resume Next` allows the procedure to continue if errors occur, especially while unmerging cells.Check and Unmerge Cells
The For Each loop iterates through the selected range, checking for merged cells with `If myRng.MergeCells`. If true, the `MergeArea` property selects the merged cell area.Unmerge and Copy Values
The `UnMerge` method releases the merge, and `Resize(1, 1).Value` copies the first cell’s value to all cells in the previously merged range.Resume Screen Updating
`Application.ScreenUpdating = True` resumes normal screen updates.
Reference Website