Write macros to let Excel automatically merge selected cell contents

Source: Internet
Author: User

When you use Excel to organize the table data, you want to be able to automatically merge the contents of all the cells in the selected area, but the Excel software comes with the merge function does not meet this requirement, what should do?

We can write a "macro" that subtly saves data in all cells in a selection, iterates through a variable, and then prints the result to the merged cell.

The code is as follows:

Sub Merge Cells ()

' Turn off screen updates and warning tips

application.screenupdating = False

Application.DisplayAlerts = False

Dim i%

' Cycle the cell data in the selected range and merge it into Strtotal

For i = 1 to Selection.count

Strtotal = strtotal & Selection.Cells (i)

Next

' Set the selection format

With Selection

. MergeCells = True

. HorizontalAlignment = Xlcenter

. VerticalAlignment = Xlcenter

. Value = "' & Strtotal ' before merging data plus ' number

End With

' Restore screen updates and warning tips

Application.DisplayAlerts = True

application.screenupdating = True

End Sub

Assign the above macro to a custom toolbar button. Then we can test the results, such as in a few nonadjacent cells in a sequence of different data input (can be text, letters or numbers), and finally select all the data range, click the button that you have just customized, you can see the effect of the merge.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.