Create Array by deleting rows and column of another Array

carbon_13

New Member
Joined
Mar 21, 2010
Messages
3
Hello,

As the title suggests, I am trying to create a 2D array by deleting particular rows and columns in another array. The remaining values would make up the new array. Both arrays would be of n x n size. Is there a way to do this, I've looked and looked and can't find out how. Thanks

I should probably say that I am wanting to do accomplish this in VBA.

carbon_13
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
deleting elements in an array (if you actually mean 'delete' as in reindex the array so that the remaining values form a contiguous array) is a very slow and ineligant way to achieve an outcome. its the sort of thing one did on a C64 in the 80s to save RAM. luckily, we have burst through the 64Kb barrier and can do things quicker.

you would be much better off starting with 2 new arrays and allocating each element of the original array to one of either new ones based on whatever your criteria are.

the new arrays will not both be n x n i wouldnt have thought?
 
Upvote 0
I guess I should paint a picture of what am wanting to do.

So I will have a starting matrix (2D array) of n x n, lets say 6 x 6. I have another matrix (1D) that will be up to 1 x (n-1), lets say 1 x 3 with values of < 1 ,3 ,6 >. These values tell me that my desired new matrix (2D) will have the remaining values of the original 2D matrix with rows 1, 3, 6 and columns 1, 3, 6 deleted.
 
Upvote 0
put a 6x6 array in A1:F6, and the filter array in I1:I3

run the code and result appears at A11:C13

you can play with this to do the variable case as you need, or you can ask for further assistance.

Code:
Sub ArrayChop()
    Dim ArraySize As Long, TargetSize As Long
    ArraySize = 6
    TargetSize = 3
    ReDim Orig(ArraySize, ArraySize)
    ReDim Result(ArraySize, ArraySize)
    ReDim Result2(ArraySize, ArraySize)
    
    'get Orig() from sheet1
    For Row = 1 To ArraySize
        For col = 1 To ArraySize
            Orig(Row, col) = Cells(Row, col)
        Next col
    Next Row
    
    ' rows - create Result()
    For Row = 1 To TargetSize
        For col = 1 To ArraySize
            Result(Row, col) = Orig(Cells(Row, 9), col)
        Next col
    Next Row
    
    'columns - create Result2()
    For Row = 1 To TargetSize
        For col = 1 To TargetSize
            Result2(Row, col) = Result(Row, Cells(col, 9))
        Next col
    Next Row
    
    'put Result2() to sheet1
    For Row = 1 To TargetSize
        For col = 1 To TargetSize
            Cells(Row + ArraySize + 4, col) = Result2(Row, col)
        Next col
    Next Row
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

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