Add string to array
Add string to array
(OP)
Hello All,
I am new to VBA, and have the following question:
I have an exsisting array A={1;2;3;4;5}
I would now want to add the string "e" to all elements of the array. i.e A={1y;2y;3y;4y;5y}.
How can I achieve this in VBA?
At the moment, for generating the array (row vector), I have a for loop - something like:
++++++++++++++++++++++++++++++++
For i As Double 1 To 5 Step 1
A=i
Next i
++++++++++++++++++++++++++++++++
Sorry if the question is real basic.
Thanks.
XLNew
I am new to VBA, and have the following question:
I have an exsisting array A={1;2;3;4;5}
I would now want to add the string "e" to all elements of the array. i.e A={1y;2y;3y;4y;5y}.
How can I achieve this in VBA?
At the moment, for generating the array (row vector), I have a for loop - something like:
++++++++++++++++++++++++++++++++
For i As Double 1 To 5 Step 1
A=i
Next i
++++++++++++++++++++++++++++++++
Sorry if the question is real basic.
Thanks.
XLNew
RE: Add string to array
Just noticed an error in my question.
I meant I wanted to add the string "y" not "e"
XLnew
RE: Add string to array
Sub TArray()
Dim A As Variant, i As Long
A = Range("a1:e1")
For i = 1 To 5
A(1, i) = A(1, i) & "y"
Next i
Range("a2:e2").Value = A
End Sub
Note that if you are reading the array from the spreadsheet the easiest (and quickest) way to do it as as shown, but this gives you a 2D array, in this case with a single row.
If you are generating your array somewhere else and you need to have it as a 1D array then just change the two A(1,i) to A(i). If you do that the line Range("a2:e2").Value = A won't work. If you need to write a 1D array to the spreadsheet you have to do it element by element with a For...Next loop, or Forall ... Next.
Doug Jenkins
Interactive Design Services
http://newtonexcelbach.wordpress.com/
RE: Add string to array
Thanks a lot. That solved the problem :)
I am used to using Matlab - so VBA is kind of a new project for me. Once again, thanks for the quick solution.
Best,
XLNew