SharmaAntriksh
New Member
- Joined
- Nov 8, 2017
- Messages
- 31
Name | Serial Number | Date | Price | Address | Comments |
A | 1 | 1/1/2019 | 123 | abc | xyz |
B | 2 | 1/1/2019 | 123 | abc | xyz |
A | 3 | 1/1/2019 | 123 | abc | xyz |
C | 4 | 1/1/2019 | 123 | abc | xyz |
<tbody>
</tbody>
i have data that looks like this, i am trying to load this into an array and populate a new array with the data where name is "A" i want the new array to only have the columns "Name", "Price", and "Comments". i am able to extract the rows with the help of below code but i am not sure how to keep only the required columns while the loop is running
Code:
Sub WorkingWithArrays()
Dim OriAry() As Variant 'Will store original data
Dim NewAry() As Variant 'will have the data after satisfying a condition
Dim i As Integer, Counter As Integer, k As Integer
OriAry = Sheet2.Range("A1:F5")
For i = LBound(OriAry, 1) To UBound(OriAry, 1)
If OriAry(i, 1) = "A" Then
Counter = Counter + 1
ReDim Preserve NewAry(LBound(OriAry, 2) To UBound(OriAry, 2), 1 To Counter)
For k = LBound(OriAry, 2) To UBound(OriAry, 2)
NewAry(k, Counter) = OriAry(i, k)
Next k
End If
Next i
Sheet3.Range("A2", Sheet3.Range("A2").Offset(Counter - 1, UBound(NewAry, 1) - 1)) = Application.Transpose(NewAry)
End Sub
Last edited: