Create as many levels of a loop as there are dimensions of a string

Jubinell

Board Regular
Joined
Jan 17, 2008
Messages
166
*Edit* sorry the title should have been:
Create as many levels of a loop as there are dimensions of an array

Sorry for the confusion!

---

Hi,

This is somewhat difficult to explain, but please look at the following UDF:

Code:
Public Function ConcSep_Array(InArray() As Variant, Optional Sep As String) As String
Dim OutStr As String
Dim i As Integer

For i = LBound(InArray, 1) To UBound(InArray, 1)
    If InArray(i) <> "" Then OutStr = OutStr & InArray(i) & Sep
Next i

ConcSep_Array = Left(OutStr, Len(OutStr) - Len(Sep))
End Function
This UDF takes a one-dimensional Excel array then stacks its elements one after another separated by the variable Sep in a continuous string of text ConcSep_Array. The result is then returned to Excel as text.

Now this works great with a one-dimensional InArray(). If InArray() were 2-dimensional, I would need to have 2 loops, like this:

Code:
For i = LBound(InArray, 1) To UBound(InArray, 1)
    For j = LBound(InArray, 2) To UBound(InArray, 2)
        If InArray(i, j) <> "" Then OutStr = OutStr & InArray(i, j) & Sep
    Next j
Next i
The question is...I don't know how many dimensions InArray() may have. Hence, I'd like to have as many loop levels as there are dimensions of InArray().

Can this be done?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Thank you Andrew for the speedy reply. Supposed I got the number of dimensions of the input array. Now how do I create as many loop levels as there are dimensions?
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,033
Members
448,940
Latest member
mdusw

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