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
 
As my demonstration is a starter and as you can easily replace the arrays within my codelines by yours !​
As a reminder [COLUMN(A:E)] is an array so easy to replace with ArrayA …​
And according to the need rather than putting the values in an array just directly allocate a string.​
 
Last edited:
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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)        ' This is what I am talking about, the '*2'    <---
        R = Join(Evaluate("{" & S & "}*{" & T & "}"), C)
        MsgBox S & vbLf & vbLf & T & vbLf & vbLf & R
End Sub
I have added a comment to you submitted code to refer to what I am talking about.
 
Upvote 0
The same : replace my array by your array variable.​
As the better is to not use any array variable but just String …​
 
Upvote 0
@Marc L Sorry, I just can't figure out how to convert your submitted code to a working code.

And by working code, final result should be:

Items in ArrayAString = 1,12,34,8,11
Items in ArrayBString = 7,5,2,9,6
Items in ArraysResultString = 7,60,68,72,66
 
Upvote 0
And according to the need rather than putting the values in an array just directly allocate a string.​
That may be something to look into, :) I'll have to check the larger code.
 
Upvote 0
According to post #14 my demonstration revamped :​
VBA Code:
Sub Demo1r()
    Dim S$, T$, R$
        S = "1,12,34,8,11"
        T = "7,5,2,9,6"
        R = Join(Evaluate("{" & S & "}*{" & T & "}"), ",")
        MsgBox R
End Sub
 
Upvote 0
You have strayed off topic. Topic is about arrays that are manipulated into strings. Your last submission has no arrays.
 
Upvote 0
'Cause arrays are useless here ! But if you really wanna start with arrays you already got all the necessary with my first demonstration as explained in post #11 …​
 
Upvote 0
Got another chance to look at this.

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)
       [B] R = Join(Evaluate("{" & S & "}*{" & T & "}"), C)[/B]
        MsgBox S & vbLf & vbLf & T & vbLf & vbLf & R
End Sub

Perhaps :
VBA Code:
MsgBox Join(Application.Transpose(Evaluate("{" & Join(ArrayA, ";") & "} * {" & Join(ArrayB, ";") & "}")), ",")

Transpose is useless, see post #7 …​

I don't think 'useless' is a friendly term, but I have eliminated the 'transpose' part of the code.

VBA Code:
ArraysResultString = Join(Application.Transpose(Evaluate("{" & Join(ArrayA, ";") & "} * {" & Join(ArrayB, ";") & "}")), ",")    ' Save ResultArray to string

Could be written as:

VBA Code:
    ArraysResultString = Join(Evaluate("{" & ArrayAString & "}*{" & ArrayBString & "}"), ",")                   ' Save ResultArray to string
 
Upvote 0

Forum statistics

Threads
1,214,798
Messages
6,121,630
Members
449,041
Latest member
Postman24

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