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?
Subodh Tiwari (Neeraj)Connect With a Mentor 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.
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?

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

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.