Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Add string to array

Add string to array

Add string to array

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
Next i

Sorry if the question is real basic.



RE: Add string to array


Just noticed an error in my question.

I meant I wanted to add the string "y" not "e"


RE: Add string to array

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

RE: Add string to array

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.



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close