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

配列操作:配列追加

Posted at

配列追加

指定した配列に追加変数を設定する。
追加位置指定あり。

'------------------------------------------------------------------------------
' 配列追加
'------------------------------------------------------------------------------
Public Function F_GetArrayAdd( _
        ByRef aRtnAry As Variant, _
        ByVal aAdd As Variant, _
        Optional ByVal aIdx As Long = D_IDX_END) As Boolean
    Dim wkRtnAry As Variant: wkRtnAry = aRtnAry
    
    Dim wkIdx As Long: wkIdx = aIdx
    Dim wkIStt As Long, wkIEnd As Long, wkICnt As Long
    
    '配列がある場合
    If IsArray(wkRtnAry) = True Then
        '現在の配列から調整
        wkIStt = LBound(wkRtnAry)
        wkIEnd = UBound(wkRtnAry)
        If wkIdx < D_IDX_START Then
            wkIdx = wkIEnd + 1
        End If
        
        '終了位置チェック
        If wkIdx > wkIEnd Then
            ReDim Preserve wkRtnAry(wkIStt To wkIdx)
        '開始位置チェック
        ElseIf wkIdx < wkIStt Then
            ReDim wkRtnAry(wkIdx To wkIEnd)
            
            '配列再設定
            For wkICnt = wkIStt To wkIEnd
                S_SetItem wkRtnAry(wkICnt), aRtnAry(wkICnt)
            Next wkICnt
        End If
    '配列がない場合は新規作成
    Else
        '開始位置調整
        If wkIdx < D_IDX_START Then
            wkIStt = D_IDX_START
            wkIdx = D_IDX_START
        Else
            wkIStt = wkIdx
        End If
        
        '値ありなら開始位置再調整
        If IsEmpty(wkRtnAry) <> True Then
            wkIStt = wkIStt - 1
            If wkIStt < D_IDX_START Then
                wkIStt = D_IDX_START
            End If
        End If
        
        '配列を生成
        ReDim wkRtnAry(wkIStt To wkIdx)
        
        '元の値を先頭に設定
        S_SetItem wkRtnAry(wkIStt), aRtnAry
    End If
    
    S_SetItem wkRtnAry(wkIdx), aAdd
    
    aRtnAry = wkRtnAry
    F_GetArrayAdd = True
End Function
Public Function F_ReturnArrayAdd( _
        ByVal aArray As Variant, _
        ByVal aAdd As Variant, _
        Optional ByVal aIdx As Long = D_IDX_END) As Variant
    Dim wkRtnAry As Variant: wkRtnAry = aArray
    
    F_GetArrayAdd wkRtnAry, aAdd, aIdx:=aIdx
    
    F_ReturnArrayAdd = wkRtnAry
End Function
0
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
0
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?