Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Add string to array

Status
Not open for further replies.

XLnew

Computer
Dec 21, 2008
12
0
0
US
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
 
Replies continue below

Recommended for you

Try this:

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
 
Hi Doug,

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
 
Status
Not open for further replies.
Back
Top