No Loop through all items in an array assistance

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
4,546
Office Version
  1. 2007
Platform
  1. Windows
I found a line of code that goes through all items in arrays without looping. The line of code writes the result to a range in a worksheet.

I would like to know if there is a way to write the results, separated by commas, to a string.

I would like three strings in total. One string = to the contents of ArrayA, second string = contents of ArrayB, and a third string = to the results currently displayed to a worksheet.

I can do all of that with loops, but I was wondering if there is a way to change the no loop line of code to save to the strings that I mentioned.

Here is the code example that shows the looping version as well as the one line of code that doesn't loop:

VBA Code:
Sub NoLoopThroughArrayValues()

    Dim ArrayCount  As Long
    Dim I           As Long
    Dim ArrayA      As Variant
    Dim ArrayB      As Variant
'
    ArrayA = Array(1, 2, 3, 4, 5)
    ArrayB = Array(2, 4, 6, 8, 10)
    ArrayCount = UBound(ArrayA) + 1
'
'
' Loop version for arrays
    For I = 1 To ArrayCount
        Range("D" & I) = ArrayA(I - 1) * ArrayB(I - 1)
    Next
'
'
' Or No Loop version for arrays equivalent
    Range("E1").Resize(ArrayCount) = Evaluate("{" & Join(ArrayA, ";") & "} * {" & Join(ArrayB, ";") & "}")
'
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Not entirely sure what you are asking for but maybe this will help...

CommaDelimitedText = Join(ArrayA, ",")
 
Upvote 0
Solution
I would like to know if there is a way to write the results, separated by commas, to a string.
Perhaps :
VBA Code:
MsgBox Join(Application.Transpose(Evaluate("{" & Join(ArrayA, ";") & "} * {" & Join(ArrayB, ";") & "}")), ",")
 
Upvote 0
@Akuini... why are you using Evaluate? Unless I totally misunderstood, ArrayA and ArrayB are one-dimensional arrays... Join will work on them directly (like I show in Message #2 for ArrayA).
 
Upvote 0
@Rick Rothstein
Hm, I think what OP wants as the result is arrayA multiplied by arrayB. So I just used his code:
VBA Code:
Range("E1").Resize(ArrayCount) = Evaluate("{" & Join(ArrayA, ";") & "} * {" & Join(ArrayB, ";") & "}")
and put the result to string not to a range.
But I might be wrong. :cry:
 
Upvote 0
I would like three strings in total
According to Excel basics :​
VBA Code:
Sub Demo1()
  Const C = ","
    Dim S$, T$, R$
        S = Join([COLUMN(A:E)], C)
        T = Join([COLUMN(A:E)*2], C)
        R = Join(Evaluate("{" & S & "}*{" & T & "}"), C)
        MsgBox S & vbLf & vbLf & T & vbLf & vbLf & R
End Sub
 
Last edited:
Upvote 0
Not entirely sure what you are asking for but maybe this will help...

CommaDelimitedText = Join(ArrayA, ",")
That worked very well @Rick Rothstein. Thank you so much!

Perhaps :
VBA Code:
MsgBox Join(Application.Transpose(Evaluate("{" & Join(ArrayA, ";") & "} * {" & Join(ArrayB, ";") & "}")), ",")
That worked for the resultstring @Akuini. Thank you so much!

Hm, I think what OP wants as the result is arrayA multiplied by arrayB. So I just used his code:
Correct, there was a multiplication being done on the two arrays.

Current code:

VBA Code:
Sub NoLoopThroughArrayValues()

    Dim ArrayCount          As Long
    Dim I                   As Long
    Dim ArrayAString        As String
    Dim ArrayBString        As String
    Dim ArraysResultString  As String
    Dim ArrayA              As Variant
    Dim ArrayB              As Variant
'
    ArrayA = Array(1, 2, 3, 4, 5)
    ArrayB = Array(2, 4, 6, 8, 10)
    ArrayCount = UBound(ArrayA) + 1
'
'
' Loop version for arrays
    For I = 1 To ArrayCount
        Range("D" & I) = ArrayA(I - 1) * ArrayB(I - 1)
    Next
'
'
' Or No Loop version for arrays equivalent
    Range("E1").Resize(ArrayCount) = Evaluate("{" & Join(ArrayA, ";") & "} * {" & Join(ArrayB, ";") & "}")
'
'
    ArrayAString = Join(ArrayA, ",")                                                                            ' Save all items in ArrayA to a string
    ArrayBString = Join(ArrayB, ",")                                                                            ' Save all items in ArrayB to a string
    ArraysResultString = Join(Application.Transpose(Evaluate("{" & Join(ArrayA, ";") & "} * {" & Join(ArrayB, ";") & "}")), ",")    ' Save ResultArray to string
'
Debug.Print "Items in ArrayAString = " & ArrayAString
Debug.Print "Items in ArrayBString = " & ArrayBString
Debug.Print "Items in ArraysResultString = " & ArraysResultString
'
End Sub
 
Last edited:
Upvote 0
Transpose is useless, see post #7 …​
 
Upvote 0
According to Excel basics :​
VBA Code:
Sub Demo1()
  Const C = ","
    Dim S$, T$, R$
        S = Join([COLUMN(A:E)], C)
        T = Join([COLUMN(A:E)*2], C)
        R = Join(Evaluate("{" & S & "}*{" & T & "}"), C)
        MsgBox S & vbLf & vbLf & T & vbLf & vbLf & R
End Sub
@Marc L I haven't tested your code but it appears to make an assumption that should not be made. That is partially my fault though. I made up the values in each array just to provide some values. The actual values will be unknown until they are actually put into the arrays.

Example could have been:
ArrayA = Array(1, 12, 34, 8, 11)
ArrayB = Array(7, 5, 2, 9, 6)
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,922
Members
449,094
Latest member
teemeren

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