LoginSignup
4
5

More than 5 years have passed since last update.

EXCEL 結合セルのClearContentsのエラーの回避方法

Last updated at Posted at 2018-02-14

大事なことはいつもネットの片隅に書いてある

ポイントはMergeArea

注意.
A1:B1セル範囲が結合されている場合、
Range("A1").ClearContents
これは、エラーとなりますので、この場合は、
Range("A1").MergeArea.ClearContents
とする必要があります。

実務としては、
Range("A1").Value = ""
この方が簡単でしょう。
ただし、指定のセルが結合範囲の先頭セルである必要があります。

VBAで解決するには

VBAでも後者はめんどくさいので、きっちりクリアする方法で行きます。
というのも「指定のセルが結合範囲の先頭セルである必要」というのはMergeAreaには関係がないからです。
 また実験用テーブルを実際にクリアするとわかりますが、MergeAreaは実際に結合していなくてもClearContentsが実行されます。(これはExcel2013以降だからかもしれません)

コードの概要

MakeTableで作った表を、マクロでクリアします。
clearcontentsVBA(混在していてもすべて結合セルとしてクリアする方法)とclearcontentsVBA2(結合と非結合セルをエラーで分けて処理する方法)は同じ結果になりますが、Excel2010以前の場合はclearcontentsVBA2の方が確実かもしれません。(on errorで処理しているため)

コード

テーブル作成

vb.net
Sub MakeTable()
'実験用の結合セルが混在した簡易な表を作ります
Range("A1:D4").Select: Selection.Clear
Range("A1").Select: ActiveCell.FormulaR1C1 = "1"
Range("B1:C1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.ReadingOrder = xlContext
.MergeCells = True
End With
ActiveCell.FormulaR1C1 = "2"
Range("A2:B2").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.MergeCells = True
End With
ActiveCell.FormulaR1C1 = "1"
Range("C2").Select: ActiveCell.FormulaR1C1 = "3"
Range("D1").Select: ActiveCell.FormulaR1C1 = "4"
Range("D2").Select: ActiveCell.FormulaR1C1 = "5"
Range("A3:A4").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.MergeCells = True
End With
ActiveCell.FormulaR1C1 = "3":
Range("A3:A4").Select: ActiveCell.FormulaR1C1 = "6"
Range("B3").Select: ActiveCell.FormulaR1C1 = "1"
Range("C3").Select: ActiveCell.FormulaR1C1 = "2"
Range("D3").Select: ActiveCell.FormulaR1C1 = "3"
Range("B4").Select: ActiveCell.FormulaR1C1 = "2"
Range("D4").Select: ActiveCell.FormulaR1C1 = "1"
Range("C4").Select: ActiveCell.FormulaR1C1 = "3"
End Sub

混在していてもすべて結合セルとしてクリアする方法

ハイパーアクセラレーションブースト(ウソ)つき
なお、どちらも結合状態は解除していない。

clearcontentsVBA
Sub ClearcontentsVBA()
'こちらが混在していてもすべて結合セルとしてクリアする方法
'Starting Hyper-Accellalation :) Boost! ;]
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = ActiveSheet
Dim irow As Long, icol As Long, i As Long, LastRow As Long, LastCol As Long
LastRow = ws.UsedRange.Rows.Count 'ws.Range("A" & Rows.Count).End(xlUp).Rowは縦に結合していると正しくない
LastCol = ws.UsedRange.Columns.Count
For irow = 1 To LastRow
For icol = 1 To LastCol
ws.Range(ws.Cells(irow, icol).Address).MergeArea.ClearContents
Next
Next
'ハイパーアクセラレーション・ブースト(ウソ)終わり
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

結合と非結合セルをエラーで分けて処理する方法

ClearContentsVBA2
Sub ClearContentsVBA2()
'結合と非結合セルをエラーで分けて処理する方法
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = ActiveSheet
Dim irow As Long, icol As Long, i As Long, LastRow As Long, LastCol As Long
LastRow = ws.UsedRange.Rows.Count 'ws.Range("A" & Rows.Count).End(xlUp).Rowは縦に結合していると正しくない
LastCol = ws.UsedRange.Columns.Count
On Error Resume Next 'On Errorでバグを判定させる(この手法はあまり推奨されない手法だがMergecellでも同じなのとexcel 2010以前は実験できないのでこちらを使用する)
For irow = 1 To LastRow
For icol = 1 To LastCol
ws.Cells(irow, icol).ClearContents
If Err.Number <> 0 Then
ws.Range(ws.Cells(irow, icol).Address).MergeArea.ClearContents
Err.Clear
End If
Next
Next
End Sub
4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5