3
4

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.

VBAで文字列を結合する関数を作った

Posted at

vbaにはJoinという関数があるが、配列にしか使えない。
そこで汎用的なjoinを作成した。

join
' rng 結合したい範囲
' delim 結合文字
Function join(rng As Range, Optional delim As String = ",") As String
    Dim result As String
    Dim cell As Variant

    result = ""
    For Each cell In rng
        result = result & delim
    Next

    join = chop(result, Len(delim))
End Function

ついでに、末尾を削除する関数

chop
' str 入力文字列
' num 末尾から削除したい文字数
Function chop(str As String, Optional num As Integer = 1) As String
    chop = Left(str, Len(str) - num)
End Function

ワークシート上で以下のように使用してもいいね
=join(A1:A5)

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?