4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

セルを縦方向に結合するExcelマクロ

Last updated at Posted at 2015-05-30

やりたいこと

選択範囲のセルを縦方向に結合する。

使い方

  1. セルを選択
    図3.png
  2. マクロ実行
  3. セルが縦方向に結合される
    図4.png

コード

Module1
Option Explicit

Public Sub verticalMerge()
    
    Dim rng As Range
    Dim i As Long
    
    '選択範囲がセルでない時は動作させない
    If TypeName(Selection) <> "Range" Then Exit Sub
    
    Set rng = Selection
    
    '複数領域を選択した時は動作させない
    If rng.Areas.Count > 1 Then Exit Sub
    
    '縦方向にマージする
    With rng
        For i = 1 To .Columns.Count
            Range(.Item(1, i), .Item(.Rows.Count, i)).Merge
        Next
    End With
    
    Set rng = Nothing
    
End Sub

検討

今回は、シンプル化するために一つのエリアを選択した時のみ動作する仕様にした。
複数のエリアを選択した場合は、各エリアごとに行数と列数を調べて、同じように処理すれば対応可能。

4
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?