1
0

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 3 years have passed since last update.

セル選択の重複数を保持するUnion

Last updated at Posted at 2021-09-02

はじめに

ExcelVBAのUnionは、各セルが何重に選択されているかを無視して、セル範囲を結合する。
そこで、選択の重複数を保持したまま合算するUnionっぽいFunctionを自作した。
使い道があるかはわからぬ。

コード

myUnion
'セル選択の重複数を保持するUnion
Private Function myUnion(rng1 As Range, rng2 As Range, ParamArray rng() As Variant) As Range
    Dim i As Long
    Dim str As String
    str = rng1.Address & "," & rng2.Address
    For i = 0 To UBound(rng)
        str = str & "," & rng(i).Address
    Next i
    str = sortAddress(str)
    Set myUnion = Range(str)
End Function

Private Function sortAddress(str0 As String) As String
    Dim ary() As String
    Dim str As String
    Dim strnext As String
    ary = Split(str0, ",")
    str = ary(0)
    For i = 1 To UBound(ary)
        If Not (Intersect(Range(str), Range(ary(i))) Is Nothing) Then
            If strnext = "" Then
                strnext = Intersect(Range(str), Range(ary(i))).Address
            Else
                strnext = strnext & "," & Intersect(Range(str), Range(ary(i))).Address
            End If
        End If
        str = Application.Union(Range(str), Range(ary(i))).Address
    Next i
    If strnext <> "" Then
        sortAddress = str & "," & sortAddress(strnext)
    Else
        sortAddress = str
    End If
End Function

参考

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?