Combine cells

I need a macro that will combine values in the same row into one cell in the same row.
For example there is data in cells A1-D5 and I want the macro to combine cell A1 & C1 into one cell in E1. And so on.
The macro should be linked to a button.
Gary LensProject ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
If I understood your requirement correctly, you may simply try this....
Sub CombineCells()
Dim lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
With Range("E1:E" & lr)
    .Formula = "=A1&C1"
    .Value = .Value
End With
End Sub

Open in new window

In the attached, click the button called "Combine Cells" to run the code.
CombineCells.xlsm
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewCommented:
Just curious, why do you want a macro for this as opposed to just placing the formula in column E to join those values?  Keep in mind there is no "undo" from changes made via macros, and they add some security overhead to the workbook when you distribute it.  But maybe there is a reason you need this?


»bp
1
Gary LensProject ManagerAuthor Commented:
@Subodh Tiwari (Neeraj) how would I apply a (-) symbol to separate the combination?
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Just replace line#5 with this...
.Formula = "=A1&"" - ""&C1"

Open in new window

1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.