VBA: new 1-D array from selected index on 2-D array

EuroSong

New Member
Joined
Sep 29, 2007
Messages
18
Hello all. I'm trying to find an easy way of converting a selected index within a 2-dimensional array into a 1-dimensional array. For example, if I have the following:

TwoDArr(1,1)="OneOne"
TwoDArr(1,2)="OneTwo"
TwoDArr(2,1)="TwoOne"
TwoDArr(2,2)="TwoTwo"

... I want to do something like this:
OneDArr=TwoDArr(2): with the result being OneDArr(1)="TwoOne" and OneDArr(2)="TwoTwo". See what I mean?

I have created a function to do it the long way round as follows:

Function TwoD_OneD(arr() As Variant, Index As Integer) As Variant
' arr is a 2D array. Index is the number of the first dimension. All the items in the second dimension will be taken into a new array
Dim TempArr()
Dim y As Integer, UB As Integer
UB = UBound(arr, 2)
ReDim TempArr(1 To UB)
For y = 1 To UB
If arr(Index, y) <> "" Then
TempArr(y) = arr(Index, y)
Else
' Reached the end of the actual data (although the array may be potentially larger)
ReDim Preserve TempArr(1 To y - 1)
Exit For ' Curtail the size of the array to only that which actually holds data.
End If
Next y
TwoD_OneD = TempArr
End Function

The above function is called as:
OneDArr=TwoD_OneD(TwoDArr, 2)

It works, but I am not satisfied with it. For a start, it's messy to have to redim a temporary array to the entire size of the input array's second dimension, when the selected index of the first dimension might not necessarily use all the elements of the second dimension. (I mean for example, arr(1,x) could go up to x=10; but arr(2,x) could go up to x=5. However because in the first series the second element goes up to 10, that must also be the size of the second element - even though items 6-10 are blank)

I found a page (How can I get one dimension from a bi-dimensional array in VB.Net? - Stack Overflow) which seemed to suggest that VBA could handle this natively, with a single line (OneDArr=TwoDArr(index)). However when I tried that, I got a compile error. Either Excel's flavour of VBA does not handle this sort of thing, or I'm doing it wrong.

Can anyone suggest a better way of doing this than the messy function I am using?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hello all. I'm trying to find an easy way of converting a selected index within a 2-dimensional array into a 1-dimensional array. For example, if I have the following:

TwoDArr(1,1)="OneOne"
TwoDArr(1,2)="OneTwo"
TwoDArr(2,1)="TwoOne"
TwoDArr(2,2)="TwoTwo"

... I want to do something like this:
OneDArr=TwoDArr(2): with the result being OneDArr(1)="TwoOne" and OneDArr(2)="TwoTwo". See what I mean?

I have created a function to do it the long way round as follows:

Function TwoD_OneD(arr() As Variant, Index As Integer) As Variant
' arr is a 2D array. Index is the number of the first dimension. All the items in the second dimension will be taken into a new array
Dim TempArr()
Dim y As Integer, UB As Integer
UB = UBound(arr, 2)
ReDim TempArr(1 To UB)
For y = 1 To UB
If arr(Index, y) <> "" Then
TempArr(y) = arr(Index, y)
Else
' Reached the end of the actual data (although the array may be potentially larger)
ReDim Preserve TempArr(1 To y - 1)
Exit For ' Curtail the size of the array to only that which actually holds data.
End If
Next y
TwoD_OneD = TempArr
End Function

The above function is called as:
OneDArr=TwoD_OneD(TwoDArr, 2)
Can anyone suggest a better way of doing this than the messy function I am using?
For the result you wanted (a slice across the specifed row), try this...

Code:
Function TwoD_OneD(Arr As Variant, Index As Integer) As Variant
  TwoD_OneD = Application.Index(Arr, Index, 0)
End Function

If you wanted a slice down a specified column, you would use this function instead...

Code:
Function TwoD_OneD(Arr As Variant, Index As Integer) As Variant
  TwoD_OneD = Application.Transpose(Application.Index(Arr, Index, 0))
End Function

Here is a short macro that you can use to show the results...

Code:
Sub Test()
  Dim TwoDArr(1 To 2, 1 To 2)
  Dim OneDArr As Variant
  TwoDArr(1, 1) = "OneOne"
  TwoDArr(1, 2) = "OneTwo"
  TwoDArr(2, 1) = "TwoOne"
  TwoDArr(2, 2) = "TwoTwo"
  OneDArr = TwoD_OneD(TwoDArr, 2)
  MsgBox "OneDArr(1) equals " & OneDArr(1) & vbLf & vbLf & "OneDArr(2)equals " & OneDArr(2)
End Sub
 
Upvote 0
Hi EuroSong.
. Another approach, similar to Rick’s but based on a more general code for selecting particular rows and columns from an Array.
http://www.mrexcel.com/forum/excel-...her-workbook-based-criteria.html?#post4174322
https://usefulgyaan.wordpress.com/2...cing-an-array-without-loop-application-index/

.. I give two codes. The first is simplified assuming you are looking to get a 1 dimensional array from a column in a 2 dimensional Array. The last couple of lines would then be doing something similar to your function, and something very similar to Rick’s first function. I applied it to getting the output you wanted but increased the size of your example TwoDArr so as to demonstrate the flexibility of the code in picking out any particular rows.
. The second code is slightly more complicated and assumes you may wish to pick out either a row or column from your TwoDArr, but you would always require your OneDArr to ba a 1 dimensional Array.

Alan.


Code:
[color=blue]Sub[/color] ColumnToOneDArray()
[color=blue]Dim[/color] TwoDArr(1 [color=blue]To[/color] 3, 1 To 3) [color=blue]As[/color] [color=blue]Variant[/color]
TwoDArr(1, 1) = "OneOne"
TwoDArr(1, 2) = "OneTwo"
TwoDArr(1, 3) = "OneThree"
TwoDArr(2, 1) = "TwoOne"
TwoDArr(2, 2) = "TwoTwo"
TwoDArr(2, 3) = "TwoThree"
TwoDArr(3, 1) = "ThreeOne"
TwoDArr(3, 2) = "ThreeTwo"
TwoDArr(3, 3) = "ThreeThree"
 
[color=blue]Dim[/color] OneDArr [color=blue]As[/color] [color=blue]Variant[/color]
[color=lightgreen]'Example to give a 1D Array based on rows 1 and 2,  and column 2[/color]
[color=blue]Dim[/color] rws() [color=blue]As[/color] Variant: [color=blue]Let[/color] rws() = Array(1, 2) [color=lightgreen]'Pick out rows required[/color]
[color=blue]Dim[/color] clms() [color=blue]As[/color] Variant: [color=blue]Let[/color] clms() = Application.Transpose(Array(2)) [color=lightgreen]'Pick out just column 2[/color]
OneDArr = Application.Index(TwoDArr, rws(), clms())
 
[color=blue]End[/color] [color=blue]Sub[/color]
[color=lightgreen]'[/color]
'
 
[color=blue]Sub[/color] ColumnOrRow[color=blue]To[/color]OneDArray()
[color=blue]Dim[/color] TwoDArr(1 To 3, 1 To 3) [color=blue]As[/color] [color=blue]Variant[/color]
TwoDArr(1, 1) = "OneOne"
TwoDArr(1, 2) = "[color=blue]On[/color]eTwo"
TwoDArr(1, 3) = "OneThree"
TwoDArr(2, 1) = "TwoOne"
TwoDArr(2, 2) = "TwoTwo"
TwoDArr(2, 3) = "TwoThree"
TwoDArr(3, 1) = "ThreeOne"
TwoDArr(3, 2) = "ThreeTwo"
TwoDArr(3, 3) = "ThreeThree"
 
[color=blue]Dim[/color] OneDArr [color=blue]As[/color] [color=blue]Variant[/color]
 
[color=lightgreen]'Example to give a 1D Array based on row 1 , and columns 1 and 3[/color]
[color=blue]Dim[/color] rws() [color=blue]As[/color] Variant: [color=blue]Let[/color] rws() = Array(1) [color=lightgreen]'Pick out row or rows required[/color]
[color=blue]Dim[/color] clms() [color=blue]As[/color] Variant: [color=blue]Let[/color] clms() = Application.Transpose(Array(1, 3)) [color=lightgreen]'Pick out column or columns required[/color]
 
[color=blue]On[/color]eDArr = Application.Index(TwoDArr, rws(), clms()) [color=lightgreen]'In this example a 1 column 2 dimensional Array is returned[/color]
On [color=blue]Error[/color] [color=blue]Resume[/color] [color=blue]Next[/color] [color=lightgreen]'This Error handler surpresses any error - ( No "exception is raised" ) and the program contiunues after the line that would have errored: http://www.excelfox.com/forum/f23/difference-between-on-error-goto-0-and-on-error-goto-1-a-894/#post9336[/color]
[color=blue]If[/color] [color=blue]UBound[/color](OneDArr, 2) = 0 [color=blue]Then[/color] [color=lightgreen]'This will error for the case when OneDArr is a one dimensional Array, in which case the next line will be carried out by virtue of the Error Handler[/color]
[color=lightgreen]'We come here on error, which should be the case of OneDArr being a 1 dimensional Array. So we do no modification to the Array[/color]
[color=blue]Else[/color] [color=lightgreen]'Either we errored for the case of a 1 dimensional array, so the last line is carried out, Or it did not error in the case of a 2 dimensional array, but did not have a column Upper bound of 0 so we come here[/color]
OneDArr = Application.Transpose(OneDArr) [color=lightgreen]'Modify the Array to a one Dimensional Array: Transpose Function returns a 1 Dimensional Array whan applied to a 1 column 2 Dimensional Array[/color]
[color=blue]End[/color] [color=blue]If[/color]
On [color=blue]Error[/color] [color=blue]GoTo[/color] 0 [color=lightgreen]'Good practice to use this Statement to turn off ( disable) the current error handler. This is all that is needed in the case of the Error Handler On Error resume Next as no exception has been raised: http://excelmatters.com/2015/03/17/on-error-wtf/   https://app.box.com/s/8zkhjcmbxrqnlnexqpktuy41clgqm4zo[/color]
 
[color=blue]End[/color] [color=blue]Sub[/color]
 
Upvote 0
P.s. .. just checking your exact example again, my last code in this form would give the results you required. – I was not quite sure if you were referring to rows or columns. But I think you will get the point. So here is my second code to give the results you asked for:

Code:
[color=blue]Sub[/color] ColumnOrRowToOneDArray()
[color=blue]Dim[/color] TwoDArr(1 [color=blue]To[/color] 3, 1 To 3) [color=blue]As[/color] [color=blue]Variant[/color]
TwoDArr(1, 1) = "OneOne"
TwoDArr(1, 2) = "OneTwo"
TwoDArr(1, 3) = "OneThree"
TwoDArr(2, 1) = "TwoOne"
TwoDArr(2, 2) = "TwoTwo"
TwoDArr(2, 3) = "TwoThree"
TwoDArr(3, 1) = "Three[color=blue]On[/color]e"
TwoDArr(3, 2) = "ThreeTwo"
TwoDArr(3, 3) = "ThreeThree"
 
[color=blue]Dim[/color] OneDArr [color=blue]As[/color] [color=blue]Variant[/color]
 
[color=lightgreen]'Example to give a 1D Array based on row 1 , and columns 1 and 3[/color]
[color=blue]Dim[/color] rws() [color=blue]As[/color] Variant: [color=blue]Let[/color] rws() = Array(2) [color=lightgreen]'Pick out row or rows required[/color]
[color=blue]Dim[/color] clms() [color=blue]As[/color] Variant: [color=blue]Let[/color] clms() = Application.Transpose(Array(1, 2)) [color=lightgreen]'Pick out column or columns required[/color]
 
OneDArr = Application.Index(TwoDArr, rws(), clms()) [color=lightgreen]'In this example a 1 column 2 dimensional Array is returned[/color]
On [color=blue]Error[/color] [color=blue]Resume[/color] [color=blue]Next[/color] [color=lightgreen]'This Error handler surpresses any error - ( No "exception is raised" ) and the program contiunues after the line that would have errored: http://www.excelfox.com/forum/f23/difference-between-on-error-goto-0-and-on-error-goto-1-a-894/#post9336[/color]
[color=blue]If[/color] [color=blue]UBound[/color](OneDArr, 2) = 0 [color=blue]Then[/color] [color=lightgreen]'This will error for the case when [color=blue]On[/color]eDArr is a one dimensional Array, in which case the next line will be carried out by virtue of the Error Handler[/color]
[color=lightgreen]'We come here on error, which should be the case of OneDArr being a 1 dimensional Array. So we do no modification to the Array[/color]
[color=blue]Else[/color] [color=lightgreen]'Either we errored for the case of a 1 dimensional array, so the last line is carried out, Or it did not error in the case of a 2 dimensional array, but did not have a column Upper bound of 0 so we come here[/color]
OneDArr = Application.Transpose(OneDArr) [color=lightgreen]'Modify the Array to a one Dimensional Array: Transpose Function returns a 1 Dimensional Array whan applied to a 1 column 2 Dimensional Array[/color]
[color=blue]End[/color] [color=blue]If[/color]
On [color=blue]Error[/color] [color=blue]GoTo[/color] 0 [color=lightgreen]'Good practice to use this Statement to turn off ( disable) the current error handler. This is all that is needed in the case of the Error Handler On Error resume Next as no exception has been raised: http://excelmatters.com/2015/03/17/on-error-wtf/   https://app.box.com/s/8zkhjcmbxrqnlnexqpktuy41clgqm4zo[/color]
 
[color=blue]End[/color] [color=blue]Sub[/color]
 
Upvote 0
Hi Rick - thanks very much, that's exactly the trick! I thought there should be some easier way than manually going through the loop.
Code:
Btw sorry about not quoting my code in the correct format - I didn't read the sticky. Now I did :)

Edit - Doc, you too, thank you. Didn't see your reply until after I just posted here. With respect to rows/columns, the reason I didn't specify is because it'd be a misnomer: my 2D array does not relate to worksheet rows/columns, it's all just VBA manipulating userform input, so my concept was generic. But the analogy with rows/columns is a good one to refer to the dimensions, I'll keep it in mind!
 
Last edited:
Upvote 0
Actually - just noticed that Rick's example still returns a 1-D array with the same size as the second ("column") dimension of the original 2-D array, regardless of whether the selected row actually has data across all of those columns. For example if I have:
Code:
TwoDArr(1, 1) = "OneOne"
TwoDArr(1, 2) = "OneTwo"
'
TwoDArr(2, 1) = "TwoOne"
TwoDArr(2, 2) = "TwoTwo"
TwoDArr(2, 3) = "TwoThree"
TwoDArr(2, 4) = "TwoFour"
TwoDArr(2, 5) = "TwoFive"
TwoDArr(2, 6) = "TwoSix"
... and then I select a slice of just row 1, it will still return a 1-D array with Ubound 6 (because that was the maximum column size of the original array) - even though the selected row only has 2 elements with actual data in them. I'm back to having to do this again:

Code:
Function TwoD_OneD(Arr As Variant, Index As Integer) As Variant
    Dim TempArr()
    TempArr = Application.Index(Arr, Index, 0)
    Dim UB As Integer, i As Integer, EndOfData As Integer
    UB = UBound(TempArr)
    For i = 1 To UB
        If TempArr(i) = "" Then
            EndOfData = i - 1
            Exit For
        End If
    Next i
    ReDim Preserve TempArr(1 To EndOfData)
    TwoD_OneD = TempArr
End Function

Unless there's a shorter way of doing it? :)
 
Upvote 0
Actually - just noticed that Rick's example still returns a 1-D array with the same size as the second ("column") dimension of the original 2-D array, regardless of whether the selected row actually has data across all of those columns.

and then I select a slice of just row 1, it will still return a 1-D array with Ubound 6 (because that was the maximum column size of the original array) - even though the selected row only has 2 elements with actual data in them. I'm back to having to do this again:

Unless there's a shorter way of doing it? :)
Yes, there is a shorter way, but you need to clarify something first. Okay, so if the last "so many" array elements in the row that was sliced were empty, you want the 1D array's upper bound to be the last element with data... fine. But what if the first so many elements are empty... do you want the 1D array to start at the first element with actual data or is it important to retain the empty elements so that the filled ones hold their element position?
 
Upvote 0
Yes, there is a shorter way, but you need to clarify something first. Okay, so if the last "so many" array elements in the row that was sliced were empty, you want the 1D array's upper bound to be the last element with data... fine. But what if the first so many elements are empty... do you want the 1D array to start at the first element with actual data or is it important to retain the empty elements so that the filled ones hold their element position?
While I am asking... what about empty elements in the middle of the array's row slice... should they be retained or "squeezed out" of existence also?
 
Upvote 0
Yes, there is a shorter way, but you need to clarify something first. Okay, so if the last "so many" array elements in the row that was sliced were empty, you want the 1D array's upper bound to be the last element with data... fine. But what if the first so many elements are empty... do you want the 1D array to start at the first element with actual data or is it important to retain the empty elements so that the filled ones hold their element position?
While I am asking... what about empty elements in the middle of the array's row slice... should they be retained or "squeezed out" of existence also?

For the specific data I'm working with, there will never be any empty places at the beginning or in the middle of the series. The data looks like this:
Code:
A|X
B|X X X X
C|X X
D|X X X

So all the "rows" are always populated from the beginning. In the above example the 2D array would have dimensions 4x4, but if I selected row C I would want a 1D array of length 2, not 4.

I'm anticipating your "easy way" answer - however I'm also curious to see what other answers there would be if my data was not contiguous from the beginning, and if I did want to "squeeze out" empty spaces in the middle?
 
Upvote 0
. @ EuroSong.

Actually - just noticed that Rick's example still returns a 1-D array with the same size as the second ("column") dimension of the original 2-D array, regardless of whether the selected row actually has data across all of those columns. …….
......
. …….
Unless there's a shorter way of doing it?

Yes, there is a shorter way, but you need to clarify.... Okay, so if the last "so many" array elements in the row that was sliced were empty, you want the 1D array's upper bound to be the last element with data... fine. But what if the first so many elements are empty... do you want the 1D array to start at the first element with actual data or is it important to retain the empty elements so that the filled ones hold their element position?

While I am asking... what about empty elements in the middle of the array's row slice... should they be retained or "squeezed out" of existence also?


……….
Rich (BB code):
A|X
B|X X X X
C|X X
D|X X X
……. In the above example the 2D array would have dimensions 4x4, but if I selected row C I would want a 1D array of length 2, not 4.

I'm anticipating your "easy way" answer - however I'm also curious to see what other answers there would be if my data was not contiguous from the beginning, and if I did want to "squeeze out" empty spaces in the middle?

. I too would be very keen to see Rick’s "easy way" answer. I struggled to find one! I am learning that almost anything is possible with VBA, but it takes the experience of people like Rick to know how!!

. My solution ended up very similar to yours, maybe just slightly simpler.
. So while I am here, for completeness:
.
. Let me firstly simplify my Function to obtain a specified Row to make it more comparable to Rick’s first Function: ( Ignore the second sub which is for my benefit for future reference to remember how I got there! )

Code:
[color=lightgreen]'[/color]
[color=blue]Function[/color] SliceRow(Arr [color=blue]As[/color] [color=blue]Variant[/color], rwIndex [color=blue]As[/color] [color=blue]Long[/color], strclms [color=blue]As[/color] [color=blue]String[/color]) [color=blue]As[/color] [color=blue]Variant[/color]
SliceRow = Application.Transpose(Application.Index(Arr, rwIndex, Application.Transpose(Split(Trim(strclms), " "))))
[color=blue]End[/color] [color=blue]Function[/color]
[color=lightgreen]'[/color]
'
'
''
[color=lightgreen]'[/color]
'
'
'
[color=blue]Sub[/color] RowToOneDArray()
[color=blue]Dim[/color] TwoDArr(1 [color=blue]To[/color] 3, 1 To 3) [color=blue]As[/color] [color=blue]Variant[/color]
TwoDArr(1, 1) = "OneOne"
TwoDArr(1, 2) = "OneTwo"
TwoDArr(1, 3) = "OneThree"
TwoDArr(2, 1) = "TwoOne"
TwoDArr(2, 2) = "TwoTwo"
TwoDArr(2, 3) = "TwoThree"
TwoDArr(3, 1) = "ThreeOne"
TwoDArr(3, 2) = "ThreeTwo"
TwoDArr(3, 3) = "ThreeThree"
 
[color=blue]Dim[/color] OneDArr [color=blue]As[/color] [color=blue]Variant[/color]
 
[color=lightgreen]'Example to give a 1D Array based on row 2 , and columns 1 and 3[/color]
[color=blue]Dim[/color] rwIndex [color=blue]As[/color] Long: [color=blue]Let[/color] rwIndex = 2 [color=lightgreen]'Index of row required[/color]
[color=blue]Dim[/color] strclms [color=blue]As[/color] String: [color=blue]Let[/color] strclms = "1 2 ": [color=blue]Let[/color] strclms = Trim(strclms) [color=lightgreen]'Columns required as string[/color]
[color=blue]Dim[/color] clms1D() [color=blue]As[/color] String: [color=blue]Let[/color] clms1D() = Split(strclms, " ") [color=lightgreen]'! Dimensional Array made from string of Array Column Indicies[/color]
[color=blue]Dim[/color] clms() [color=blue]As[/color] Variant: [color=blue]Let[/color] clms() = Application.Transpose(clms1D()) [color=lightgreen]'Pick out column or columns required[/color]
 
OneDArr = Application.Index(TwoDArr, rwIndex, clms()) [color=lightgreen]'In this example a 1 column 2 dimensional Array is returned[/color]
 
OneDArr = Application.Transpose(OneDArr) [color=lightgreen]'Modify the Array to a one Dimensional Array: Transpose Function returns a 1 Dimensional Array whan applied to a 1 column 2 Dimensional Array[/color]
 
 
[color=blue]End[/color] [color=blue]Sub[/color]


. As Yous see the function is at least a one liner, so similar to Rick’s, but allowing the extra advantage of being able to select specific columns
. Here would be the corresponding test Sub to get the results you wanted:

Code:
[color=lightgreen]'[/color]
[color=blue]Sub[/color] TestFunctionSliceRow()
[color=blue]Dim[/color] TwoDArr(1 [color=blue]To[/color] 3, 1 To 3) [color=blue]As[/color] [color=blue]Variant[/color]
TwoDArr(1, 1) = "OneOne"
TwoDArr(1, 2) = "OneTwo"
TwoDArr(1, 3) = "OneThree"
TwoDArr(2, 1) = "TwoOne"
TwoDArr(2, 2) = "TwoTwo"
TwoDArr(2, 3) = "TwoThree"
TwoDArr(3, 1) = "ThreeOne"
TwoDArr(3, 2) = "ThreeTwo"
TwoDArr(3, 3) = "ThreeThree"
 
[color=blue]Dim[/color] OneDArr [color=blue]As[/color] [color=blue]Variant[/color]
 
[color=lightgreen]'Example to give a 1D Array based on row 2 , and columns 1 and 2[/color]
 
OneDArr = SliceRow(TwoDArr, 2, "1 2 ")
 
[color=blue]End[/color] [color=blue]Sub[/color]

........

. To ignore blanks I would either use the same function with this code:

Code:
[color=lightgreen]'[/color]
[color=blue]Sub[/color] SliceRowIgnoreBlanks()
[color=blue]Dim[/color] TwoDArr(1 [color=blue]To[/color] 3, 1 [color=blue]To[/color] 3) [color=blue]As[/color] [color=blue]Variant[/color]
TwoDArr(1, 1) = "OneOne"
TwoDArr(1, 2) = "OneTwo"
TwoDArr(1, 3) = "OneThree"
TwoDArr(2, 1) = "TwoOne"
TwoDArr(2, 2) = "TwoTwo"
TwoDArr(2, 3) = "" [color=lightgreen]'Blank to be ignored[/color]
TwoDArr(3, 1) = "ThreeOne"
TwoDArr(3, 2) = "ThreeTwo"
TwoDArr(3, 3) = "ThreeThree"
 
[color=blue]Dim[/color] OneDArr [color=blue]As[/color] [color=blue]Variant[/color]
[color=blue]Dim[/color] rwIndex [color=blue]As[/color] Long: [color=blue]Let[/color] rwIndex = 2 [color=lightgreen]'Index of row required[/color]
[color=blue]Dim[/color] strclms [color=blue]As[/color] [color=blue]String[/color] [color=lightgreen]'String to contain Indices for Row[/color]
[color=blue]Dim[/color] j [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'[/color]
    [color=blue]For[/color] j = 1 To [color=blue]UBound[/color](TwoDArr(), rwIndex) 'UBound(TwoDArr(), 2)
        [color=blue]If[/color] TwoDArr(rwIndex, j) <> "" [color=blue]Then[/color] strclms = strclms & j & " "
    [color=blue]Next[/color] j
   
OneDArr = SliceRow(TwoDArr, 2, strclms)
 
[color=blue]End[/color] [color=blue]Sub[/color]
......
. Or:
. Alternatively I modify my Function which looks then looks similar to yours:

Code:
[color=lightgreen]'[/color]
[color=blue]Function[/color] SliceRowIgnorBlanks(Arr [color=blue]As[/color] [color=blue]Variant[/color], rwIndex [color=blue]As[/color] [color=blue]Long[/color]) [color=blue]As[/color] [color=blue]Variant[/color]
[color=blue]Dim[/color] strclms [color=blue]As[/color] [color=blue]String[/color] [color=lightgreen]'String to contain Indices for Row[/color]
[color=blue]Dim[/color] j [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'[/color]
    [color=blue]For[/color] j = 1 [color=blue]To[/color] [color=blue]UBound[/color](Arr, 2)
        [color=blue]If[/color] Arr(rwIndex, j) <> "" [color=blue]Then[/color] strclms = strclms & j & " "
    [color=blue]Next[/color] j
 
SliceRowIgnorBlanks = Application.Transpose(Application.Index(Arr, rwIndex, Application.Transpose(Split(Trim(strclms), " "))))
[color=blue]End[/color] [color=blue]Function[/color]

......
. And a test Sub to check this function

Code:
[color=lightgreen]'[/color]
[color=blue]Sub[/color] TestSliceRowIgnorBlanks()
[color=blue]Dim[/color] TwoDArr(1 [color=blue]To[/color] 3, 1 To 3) [color=blue]As[/color] [color=blue]Variant[/color]
TwoDArr(1, 1) = "OneOne"
TwoDArr(1, 2) = "OneTwo"
TwoDArr(1, 3) = "OneThree"
TwoDArr(2, 1) = "TwoOne"
TwoDArr(2, 2) = "TwoTwo"
TwoDArr(2, 3) = "" [color=lightgreen]'Blank to be ignored[/color]
TwoDArr(3, 1) = "ThreeOne"
TwoDArr(3, 2) = "ThreeTwo"
TwoDArr(3, 3) = "ThreeThree"
 
[color=blue]Dim[/color] OneDArr [color=blue]As[/color] [color=blue]Variant[/color]
   
OneDArr = SliceRowIgnorBlanks(TwoDArr, 2)
 
[color=blue]End[/color] [color=blue]Sub[/color]


. Hope that makes a worthwhile contribution to the Thread. And look forward like you to Rick’s further input
Alan
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,485
Members
448,967
Latest member
visheshkotha

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