Raghav Chamadiya
New Member
- Joined
- May 31, 2020
- Messages
- 13
- Office Version
- 2016
- Platform
- Windows
Hi, so I am trying to create a dynamic VBA array, with 3 columns, and dynamic rows. This is my code so far:
And this is the database:
Ideally code should, search for DS packaging in the F column, and create an array, with 1st column as DS packaging, 2nd column as corresponding, Start Date, and 3rd column as corresponding End Date.
I know I have inverted rows and columns while defining array, and writing loop, thats because I read somewhere I can only preserve the last dimension in an array.
The current output from my code is:
The Matrix Join function simply, creates this matrix type view of the array:
Please help. I am stuck on this from way too long.
VBA Code:
Sub Test4()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.AutomationSecurity = msoAutomationSecurityLow
Dim nwb As Workbook
Set nwb = Workbooks.Open("DB location")
Dim arr() As Variant
Dim strColumn As String
Dim strColumn2 As String
strColumn = "F"
strColumn2 = "C"
strColumn3 = "E"
Dim iVal As Integer
iVal = Application.WorksheetFunction.CountIf(nwb.Sheets("Data").Range("F2:F8"), "DS packaging")
ReDim arr(2, iVal)
With nwb.Sheets("Data")
lngLastRow = .Cells(.rows.Count, strColumn).End(xlUp).row
For i = 0 To iVal - 1
For lngRow = 2 To lngLastRow
If .Cells(lngRow, strColumn).Value = "DS packaging" Then
ReDim Preserve arr(2, i)
arr(0, i) = .Cells(lngRow, strColumn).Value
arr(1, i) = CDate(.Cells(lngRow, strColumn2).Value)
arr(2, i) = CDate(.Cells(lngRow, strColumn3).Value)
End If
Next lngRow
Next i
End With
nwb.SaveAs Filename:="DB location"
MsgBox (arr(1, 1))
MsgBox (MatrixJoin(arr))
End Sub
And this is the database:
Ideally code should, search for DS packaging in the F column, and create an array, with 1st column as DS packaging, 2nd column as corresponding, Start Date, and 3rd column as corresponding End Date.
I know I have inverted rows and columns while defining array, and writing loop, thats because I read somewhere I can only preserve the last dimension in an array.
The current output from my code is:
The Matrix Join function simply, creates this matrix type view of the array:
VBA Code:
Function MatrixJoin(M As Variant, Optional delim1 As String = vbTab, Optional delim2 As String = vbCrLf) As String
Dim i As Long, j As Long
Dim row As Variant, rows As Variant
ReDim rows(LBound(M, 1) To UBound(M, 1))
ReDim row(LBound(M, 2) To UBound(M, 2))
For i = LBound(M, 1) To UBound(M, 1)
For j = LBound(M, 2) To UBound(M, 2)
row(j) = M(i, j)
Next j
rows(i) = Join(row, delim1)
Next i
MatrixJoin = Join(rows, delim2)
End Function
Please help. I am stuck on this from way too long.