0% found this document useful (0 votes)
39 views

Code VBA VStack

Uploaded by

digitalmoney0922
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Code VBA VStack

Uploaded by

digitalmoney0922
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Function VSTACK(ParamArray Ranges() As Variant) As Variant

Dim Result() As Variant

Dim TotalRows As Long, TotalCols As Long

Dim r As Range, i As Long, j As Long, rowOffset As Long

' Hitung ukuran hasil

TotalRows = 0: TotalCols = 0

For Each r In Ranges

TotalRows = TotalRows + r.Rows.Count

TotalCols = Application.WorksheetFunction.Max(TotalCols,
r.Columns.Count)

Next r

ReDim Result(1 To TotalRows, 1 To TotalCols)

' Gabungkan data

rowOffset = 0

For Each r In Ranges

For i = 1 To r.Rows.Count

For j = 1 To r.Columns.Count

Result(rowOffset + i, j) = r.Cells(i, j).Value

Next j

Next i

rowOffset = rowOffset + r.Rows.Count

Next r

VSTACK = Result

End Function
_______________________________________________________________

Function VSTACK(ParamArray Ranges() As Variant) As Variant

Dim Result() As Variant

Dim TotalRows As Long, TotalCols As Long

Dim r As Variant, cellRange As Range

Dim i As Long, j As Long, rowOffset As Long

Dim currentRows As Long, currentCols As Long

' Inisialisasi

TotalRows = 0

TotalCols = 0

' Hitung ukuran hasil gabungan

For Each r In Ranges

' Validasi input untuk memastikan setiap elemen adalah range

If TypeName(r) = "Range" Then

Set cellRange = r

currentRows = cellRange.Rows.Count

currentCols = cellRange.Columns.Count

TotalRows = TotalRows + currentRows

TotalCols = Application.WorksheetFunction.Max(TotalCols,
currentCols)

Else

' Abaikan input yang bukan range

MsgBox "Salah satu parameter bukan range! Abaikan.",


vbExclamation

End If
Next r

' Tentukan ukuran array hasil

If TotalRows = 0 Or TotalCols = 0 Then

VSTACK = CVErr(xlErrRef)

Exit Function

End If

ReDim Result(1 To TotalRows, 1 To TotalCols)

' Gabungkan data dari setiap range

rowOffset = 0

For Each r In Ranges

If TypeName(r) = "Range" Then

Set cellRange = r

currentRows = cellRange.Rows.Count

currentCols = cellRange.Columns.Count

For i = 1 To currentRows

For j = 1 To currentCols

Result(rowOffset + i, j) = cellRange.Cells(i, j).Value

Next j

Next i

rowOffset = rowOffset + currentRows

End If

Next r

' Kembalikan hasil

VSTACK = Result
End Function

Function VSTACK(ParamArray Ranges() As Variant) As Variant

Dim Result() As Variant

Dim TotalRows As Long, TotalCols As Long

Dim r As Variant, cellRange As Range, tbl As ListObject

Dim i As Long, j As Long, rowOffset As Long

Dim currentRows As Long, currentCols As Long

' Inisialisasi

TotalRows = 0

TotalCols = 0

' Hitung ukuran hasil gabungan

For Each r In Ranges

If TypeName(r) = "Range" Then

' Jika input berupa range biasa

Set cellRange = r

currentRows = cellRange.Rows.Count

currentCols = cellRange.Columns.Count

ElseIf TypeName(r) = "ListObject" Then

' Jika input berupa tabel

Set tbl = r

Set cellRange = tbl.DataBodyRange

currentRows = cellRange.Rows.Count

currentCols = cellRange.Columns.Count
Else

' Abaikan input yang tidak valid

MsgBox "Salah satu parameter bukan range atau tabel! Abaikan.",


vbExclamation

GoTo NextInput

End If

TotalRows = TotalRows + currentRows

TotalCols = Application.WorksheetFunction.Max(TotalCols,
currentCols)

NextInput:

Next r

' Tentukan ukuran array hasil

If TotalRows = 0 Or TotalCols = 0 Then

VSTACK = CVErr(xlErrRef)

Exit Function

End If

ReDim Result(1 To TotalRows, 1 To TotalCols)

' Gabungkan data dari setiap range atau tabel

rowOffset = 0

For Each r In Ranges

If TypeName(r) = "Range" Then

' Proses range biasa

Set cellRange = r

ElseIf TypeName(r) = "ListObject" Then

' Proses tabel

Set tbl = r
Set cellRange = tbl.DataBodyRange

Else

' Abaikan input yang tidak valid

GoTo NextProcess

End If

currentRows = cellRange.Rows.Count

currentCols = cellRange.Columns.Count

For i = 1 To currentRows

For j = 1 To currentCols

Result(rowOffset + i, j) = cellRange.Cells(i, j).Value

Next j

Next i

rowOffset = rowOffset + currentRows

NextProcess:

Next r

' Kembalikan hasil

VSTACK = Result

End Function

You might also like