×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• 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.

(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

(OP)
Hello.

Just noticed an error in my question.

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

XLnew

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
http://newtonexcelbach.wordpress.com/

(OP)
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

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.

Resources

Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a partâ€™s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

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:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!