Forum Discussion
spazyp45
Dec 20, 2023Copper Contributor
VBA HELP WITH AN ARRAY
Still learning and struggle with array's -- looking for help to shorten the below listed code. As is it works perfectly and does what I need it to, just takes too long. Hoping an array would make it ...
- Dec 22, 2023
spazyp45 This appears to be the reverse procedure to your previous question: still learning- looking for help with excel vba
The following method loads the source data to an array, then outputs multiple values at once using a secondary array:
Private Sub LoadScheduleData() Dim wsSource As Worksheet, lastRow As Long, rowId As Long Set wsSource = Sheets("Sheet4") lastRow = wsSource.Cells(wsSource.Rows.Count, 1).End(xlUp).Row For rowId = 3 To lastRow If wsSource.Cells(rowId, 1).Value = TextBox1.Value Then Dim wsOutput As Worksheet, data As Variant Set wsOutput = Sheets("Sheet2") data = wsSource.Cells(rowId, 1).Resize(, 500).Value With wsOutput ' output to individual cells (with no pattern) .Cells(15, 27).Value = data(1, 8) .Cells(3, 1).Value = data(1, 27) .Cells(3, 7).Value = data(1, 28) .Cells(3, 8).Value = data(1, 29) .Cells(4, 2).Value = data(1, 30) .Cells(4, 7).Value = data(1, 31) .Cells(6, 3).Value = data(1, 40) ' output data: row 5; col 5 to 12 Dim arr() As Variant, j As Long, k As Long ReDim arr(1 To 1, 1 To 8) k = 31 For j = 1 To 8 arr(1, j) = data(1, j + k) Next j .Cells(5, 5).Resize(, UBound(arr, 2)).Value = arr ' output data: row 7 to 52; col 1, 4 to 12 Dim col() As Variant, i As Long ReDim col(1 To 46, 1 To 1) ReDim arr(1 To 46, 1 To 9) For i = 1 To 46 k = k + 10 col(i, 1) = data(1, k) For j = 1 To 9 arr(i, j) = data(1, j + k) Next j Next i .Cells(7, 1).Resize(UBound(col, 1)).Value = col .Cells(7, 4).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr End With ' exit loop after loading data Exit For End If Next rowId End Sub
djclements
Dec 22, 2023Silver Contributor
spazyp45 This appears to be the reverse procedure to your previous question: still learning- looking for help with excel vba
The following method loads the source data to an array, then outputs multiple values at once using a secondary array:
Private Sub LoadScheduleData()
Dim wsSource As Worksheet, lastRow As Long, rowId As Long
Set wsSource = Sheets("Sheet4")
lastRow = wsSource.Cells(wsSource.Rows.Count, 1).End(xlUp).Row
For rowId = 3 To lastRow
If wsSource.Cells(rowId, 1).Value = TextBox1.Value Then
Dim wsOutput As Worksheet, data As Variant
Set wsOutput = Sheets("Sheet2")
data = wsSource.Cells(rowId, 1).Resize(, 500).Value
With wsOutput
' output to individual cells (with no pattern)
.Cells(15, 27).Value = data(1, 8)
.Cells(3, 1).Value = data(1, 27)
.Cells(3, 7).Value = data(1, 28)
.Cells(3, 8).Value = data(1, 29)
.Cells(4, 2).Value = data(1, 30)
.Cells(4, 7).Value = data(1, 31)
.Cells(6, 3).Value = data(1, 40)
' output data: row 5; col 5 to 12
Dim arr() As Variant, j As Long, k As Long
ReDim arr(1 To 1, 1 To 8)
k = 31
For j = 1 To 8
arr(1, j) = data(1, j + k)
Next j
.Cells(5, 5).Resize(, UBound(arr, 2)).Value = arr
' output data: row 7 to 52; col 1, 4 to 12
Dim col() As Variant, i As Long
ReDim col(1 To 46, 1 To 1)
ReDim arr(1 To 46, 1 To 9)
For i = 1 To 46
k = k + 10
col(i, 1) = data(1, k)
For j = 1 To 9
arr(i, j) = data(1, j + k)
Next j
Next i
.Cells(7, 1).Resize(UBound(col, 1)).Value = col
.Cells(7, 4).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
End With
' exit loop after loading data
Exit For
End If
Next rowId
End Sub
spazyp45
Dec 25, 2023Copper Contributor
You caught me. Your amazing thank you.
- djclementsDec 26, 2023Silver Contributor
spazyp45 You're very welcome. For more information on arrays, I highly recommend visiting Excel Macro Mastery's website and/or YouTube channel:
Website: Excel VBA Array – The Complete Guide