「個人用マクロブック」PERSONAL.XLSB という機能があり、 Excel起動時に自動で開かれる、どのExcelファイルでも使える、自作マクロを保存できるので、これを使う。
まず作成方法。
Excel起動 「表示」タブ 「マクロの記録」 保存先で「個人用マクロブック」を選択 OK すぐ記録停止 これで PERSONAL.XLSB が作られる。
次に開く方法。
Alt + F11 VBAエディタ左側のプロジェクト一覧
ここにVBAProject (PERSONAL.XLSB)が出るので、その中の Modules → Module1 へマクロを貼る。
Sub 結合右縮小()
Dim dic As Object
Dim c As Range
Dim ma As Range
Dim k
Dim addr()
Set dic = CreateObject("Scripting.Dictionary")
For Each c In Selection.Cells
If c.MergeCells Then
If Not dic.Exists(c.MergeArea.Address) Then
dic.Add c.MergeArea.Address, c.MergeArea.Address
End If
End If
Next
ReDim addr(0 To dic.Count - 1)
Dim i As Long
i = 0
For Each k In dic.Keys
addr(i) = k
i = i + 1
Next
For i = 0 To UBound(addr)
Set ma = Range(addr(i))
If ma.Columns.Count > 1 Then
Dim r As Long
Dim col As Long
Dim row0 As Long
Dim col0 As Long
r = ma.rows.Count
col = ma.Columns.Count
row0 = ma.Row
col0 = ma.Column
ma.UnMerge
Range( _
Cells(row0, col0), _
Cells(row0 + r - 1, col0 + col - 2) _
).Merge
End If
Next
End Sub
Sub 結合右拡張()
Dim dic As Object
Dim c As Range
Dim ma As Range
Dim k
Dim addr()
Set dic = CreateObject("Scripting.Dictionary")
For Each c In Selection.Cells
If c.MergeCells Then
If Not dic.Exists(c.MergeArea.Address) Then
dic.Add c.MergeArea.Address, c.MergeArea.Address
End If
End If
Next
ReDim addr(0 To dic.Count - 1)
Dim i As Long
i = 0
For Each k In dic.Keys
addr(i) = k
i = i + 1
Next
For i = 0 To UBound(addr)
Set ma = Range(addr(i))
Dim r As Long
Dim col As Long
Dim row0 As Long
Dim col0 As Long
r = ma.rows.Count
col = ma.Columns.Count
row0 = ma.Row
col0 = ma.Column
ma.UnMerge
Range( _
Cells(row0, col0), _
Cells(row0 + r - 1, col0 + col) _
).Merge
Next
End Sub
Sub 結合右移動()
Dim dic As Object
Dim c As Range
Dim ma As Range
Dim k
Dim addr()
Set dic = CreateObject("Scripting.Dictionary")
For Each c In Selection.Cells
If c.MergeCells Then
If Not dic.Exists(c.MergeArea.Address) Then
dic.Add c.MergeArea.Address, c.MergeArea.Address
End If
End If
Next
ReDim addr(0 To dic.Count - 1)
Dim i As Long
i = 0
For Each k In dic.Keys
addr(i) = k
i = i + 1
Next
For i = 0 To UBound(addr)
Set ma = Range(addr(i))
Dim txt As Variant
Dim r As Long
Dim col As Long
Dim row0 As Long
Dim col0 As Long
txt = ma.Cells(1, 1).Value
r = ma.rows.Count
col = ma.Columns.Count
row0 = ma.Row
col0 = ma.Column
ma.UnMerge
Range( _
Cells(row0, col0 + 1), _
Cells(row0 + r - 1, col0 + col) _
).Merge
Cells(row0, col0 + 1).Value = txt
Next
End Sub
ボタン追加は簡単。
ファイル オプション クイックアクセスツールバー 「コマンドの選択」で「マクロ」 追加
するとExcel左上にボタンが出る。