
マクロ 85_b回
85回からの続きです。
コードの中でどんな変数を使ったらいいかということを考えていきます。
1. 例えば この3つのセルに入っている全体のレンジを配列として受け取りたい。
あるいは、セルが2つだったり4つだったりといろいろあることが想定されます。

2. そこで、ここではかたまりと考えて配列のひとつgroupedという変数を使います。

3. その中の1個1個はひとつの中のピースとして、変数pieceを使います。

4. そしてつかうのは、For文の中でFor Each piece In groupedのように使います。

5. 他に動的配列newArrayも使います。
Function ketugou(delimiter As String, ignore_empty As Boolean, _
ParamArray joinText() As Variant)
Dim grouped As Variant, piece As Variant, i As Long
Dim newArray() As Variant
End Function

6. 中身のコードですが、変数iの初期値を=0とします。変数iを使用したFor文を入れましょう。
For Each piece In grouped
If Not ignore_empty Or Not IsEmpty(piece) Then
ReDim Preserve newArray(i)
newArray(i) = piece
i = i + 1
End If
Next piece

7.このFor文はさらにFor文でネストします。外側のFor文では、 引数で受け取ったjoinTextの中のgroupedを見ていくということになります。

8. そして、joinTextの中のgroupedがRangeかどうかの判定をして、Rangeならば内側のFor文で処理をします。

9. 次に内側のFor文では、受け取ったgroupedの中のpieceを空であるかどうかの判定 をして空でないなら、newArrayに渡してやります。
空でない=文字列がある
If Not ignore_empty Or Not IsEmpty(piece) Then
Function ketugou(delimiter As String, ignore_empty As Boolean, _
ParamArray joinText() As Variant)
Dim grouped As Variant, piece As Variant, i As Long
Dim newArray() As Variant
i = 0
For Each grouped In joinText
If TypeName(grouped) = "Range" Then
For Each piece In grouped
If Not ignore_empty Or Not IsEmpty(piece) Then
ReDim Preserve newArray(i)
newArray(i) = piece
i = i + 1
End If
Next piece
’ まだ
Next grouped
’ まだ
End Function
