VBA 3D Array - remove rows in two dimensions

Vinci2504

New Member
Joined
Jan 13, 2020
Messages
7
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
  2. Mobile
  3. Web
Hi Eweryone,

It my first post on the forum, but I am reading forum from long time. Is nice to have a that good kind of support. Always trying solve problems myself, but I have the problem with 3D array.

I am preparing the app to remove rows from 3D array in which conditions is fulfill.
I have the error from app in line: NewArray(col, rowNew, 0) = BaseArray(col, row, 0) ' Error: Subscript out of range
Is someone who can support in this case?

The table is looks like:
Row/Col111213....21
11 / RND1 / RND1 / RND1 / RND1 / RND
22 / RND2 / RND2 / RND2 / RND2 / RND
....... / RND... / RND... / RND... / RND... / RND
1010 / RND10 / RND10 / RND10 / RND10 / RND

The code is:
VBA Code:
'App for preparation of 3D Array
'Column = 11:21
'Row = 1:10
'dimension 0: Row
'Dimension 1: Int(50 * Rnd) + 1
Sub ArrayTestRemove2()

Dim BaseArray(11 To 21, 1 To 10, 1)    ' Declare array variables.
Dim NewArray As Variant
'Dim i, j, j1, k As Long
Dim col, row, rowNew As Long

'--- Print
Debug.Print "Minimum number of Row: "; LBound(BaseArray, 2) 'Minimum number of Row
Debug.Print "Maximum number of Row: "; UBound(BaseArray, 2) 'Maximum number of Row
Debug.Print "Minimum number of Col: "; LBound(BaseArray, 1) 'Minimum number of Row
Debug.Print "Maximum number of Col: "; UBound(BaseArray, 1) 'Maximum number of Row
    
'--- Prepare Array in two dimensions (col, row, page)
    For col = LBound(BaseArray, 1) To UBound(BaseArray, 1)
        For row = LBound(BaseArray, 2) To UBound(BaseArray, 2)
            BaseArray(col, row, 0) = row
            BaseArray(col, row, 1) = row & "." & Int(50 * Rnd) + 1
        Next row
    Next col
    
'--- print Array
    For row = LBound(BaseArray, 2) To UBound(BaseArray, 2)
        For col = LBound(BaseArray, 1) To UBound(BaseArray, 1)
           Debug.Print col; row; "|"; BaseArray(col, row, 0); BaseArray(col, row, 1)
           'Debug.Print MyArray(i, j, 1)
        Next col
    Next row
    
'--- Print
Debug.Print "Array(col=1, row=10, page=0): "; BaseArray(11, 10, 0)

'--- Remove rows when Value in Column 11 > 9 in both dimensions
ReDim NewArray(LBound(BaseArray, 2) To UBound(BaseArray, 2))
rowNew = 1
    For row = 1 To UBound(BaseArray, 2)
        For col = LBound(BaseArray, 1) To UBound(BaseArray, 1)
            If BaseArray(11, row, 0) <> 9 Then
                rowNew = rowNew + 1
                NewArray(col, rowNew, 0) = BaseArray(col, row, 0) ' Error: Subscript out of range
                NewArray(col, rowNew, 1) = BaseArray(col, row, 1)
            End If
        Next col
    Next row
ReDim Preserve NewArray(LBound(BaseArray, 2) To rowNew)
    
'--- Finish
Debug.Print "Done"

End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi & welcome to MrExcel.
This line ReDim NewArray(LBound(BaseArray, 2) To UBound(BaseArray, 2)) will give you a 1d array of 1 to 10
but you are referring to it as though it was a 3d array.
 
Upvote 0
Thank you for feedback. I overlooked definition of boundlist.

I make some modification based on your feedback, but I can't still get 'Redim Preverse' to the correct result.

Current code is working until 'ReDim Preserve NewArray(11 To 21, LBound(NewArray, 2) To rowNew, 1)'.
Everything is working as I expect, but still 'empty' rows are not removed.
Any idea?

VBA Code:
'App for preparation of 3D Array
'Column = 11:21
'Row = 1:10
'dimension 0: Row
'Dimension 1: Int(50 * Rnd) + 1
Sub ArrayTestRemove2()

Dim BaseArray(11 To 21, 1 To 10, 1)    ' Declare array variables.
Dim NewArray As Variant
'Dim i, j, j1, k As Long
Dim col, row, rowNew As Long

'--- Print
Debug.Print "Minimum number of Row: "; LBound(BaseArray, 2) 'Minimum number of Row
Debug.Print "Maximum number of Row: "; UBound(BaseArray, 2) 'Maximum number of Row
Debug.Print "Minimum number of Col: "; LBound(BaseArray, 1) 'Minimum number of Col
Debug.Print "Maximum number of Col: "; UBound(BaseArray, 1) 'Maximum number of Col
    
'--- Prepare Array in two dimensions (col, row, page)
    For col = LBound(BaseArray, 1) To UBound(BaseArray, 1)
        For row = LBound(BaseArray, 2) To UBound(BaseArray, 2)
            BaseArray(col, row, 0) = row
            BaseArray(col, row, 1) = row & "." & Int(50 * Rnd) + 1
        Next row
    Next col
    
'--- print Array
    For row = LBound(BaseArray, 2) To UBound(BaseArray, 2)
        For col = LBound(BaseArray, 1) To UBound(BaseArray, 1)
           Debug.Print col; row; "|"; BaseArray(col, row, 0); BaseArray(col, row, 1)
           'Debug.Print MyArray(i, j, 1)
        Next col
    Next row
    
'--- Print
Debug.Print "Array(col=1, row=10, page=0): "; BaseArray(11, 10, 0)

'--- Remove rows when Value in Column 11 > 9 in both dimensions
'ReDim NewArray(LBound(BaseArray, 2) To UBound(BaseArray, 2))
'ReDim NewArray(11 To 21, LBound(BaseArray, 2) To UBound(BaseArray, 2), 0)
ReDim NewArray(11 To 21, LBound(BaseArray, 2) To UBound(BaseArray, 2), 0 To 1)
rowNew = 0
    'For col = LBound(BaseArray, 1) To UBound(BaseArray, 1) ' 11 To 21
        For row = LBound(BaseArray, 2) To UBound(BaseArray, 2) ' 1 to 10
            If BaseArray(11, row, 0) <> 9 Then
                rowNew = rowNew + 1
                For col = LBound(BaseArray, 1) To UBound(BaseArray, 1) ' 11 To 21
                    NewArray(col, rowNew, 0) = BaseArray(col, row, 0)
                    NewArray(col, rowNew, 1) = BaseArray(col, row, 1)
                Next col
            End If
        Next row
    'Next col
    
'--- Print support
Debug.Print LBound(NewArray, 2)
Debug.Print rowNew

'Redefine Array size
ReDim Preserve NewArray(11 To 21, LBound(NewArray, 2) To rowNew, 1)
 
'--- Finish
Debug.Print "Redim Preserve has been done"

End Sub
 
Upvote 0
I make some modification based on your feedback, but I can't still get 'Redim Preverse' to the correct result.
When using ReDim, you can only change the limits of the last dimension... your code is attempting to change the limits of the next to last dimension.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top