vba 在不丢失数据的情况下合并 Excel 中的单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18784901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:18:31  来源:igfitidea点击:

Merging cells in Excel without loosing data

excelexcel-vbamergeexcel-2007vba

提问by user2775737

First of sorry for making a new thread about this but i wasn't able to comment in existing threads.

首先很抱歉为此创建了一个新线程,但我无法在现有线程中发表评论。

I'm trying to merge a lot of cells exactly like in thisthread, but I'm kind of new to coding and especially excel/VBA so I don't get it to work. I have the same scenario (except I don't have any empty rows) so I just tried to use the code in the existing thread not really understanding the syntax:

我正在尝试像在这个线程中一样合并很多单元格,但我对编码特别是 excel/VBA 有点陌生,所以我没有让它工作。我有相同的场景(除了我没有任何空行)所以我只是尝试在现有线程中使用代码,但并不真正理解语法:

Sub mergecolumn()

Dim cnt As Integer
Dim rng As Range
Dim str As String

For i = ActiveSheet.UsedRange.Rows.Count To 1 Step -1
cnt = Cells(i, 1).MergeArea.Count
Set rng = Range(Cells(i, 2), Cells(i - cnt + 1, 2))

For Each cl In rng
    If Not IsEmpty(cl) Then str = str + vbNewLine + cl
Next
If str <> "" Then str = Right(str, Len(str) - 2)

Application.DisplayAlerts = False
rng.Merge
rng = str
Application.DisplayAlerts = True

str = ""
i = i - cnt + 1
Next i

End Sub

I've tried to run the macro in different ways marking multiple columns, marking multiple rows and marking just some area but I'm always getting:

我试图以不同的方式运行宏来标记多列、标记多行并只标记一些区域,但我总是得到:

Run-time error '13':
Type mismatch

运行时错误“13”:
类型不匹配

When I go to debug screen this is marked:

当我去调试屏幕时,它被标记为:

str = str + vbNewLine + cl

I added the macro through Developer-ribbon->Visual Basic->Insert->Module and just pasted the code there and saved it.

我通过 Developer-ribbon->Visual Basic->Insert->Module 添加了宏,然后将代码粘贴到那里并保存。

Thanks in advance for any help
//Joakim

在此先感谢您的帮助
//Joakim

采纳答案by Siddharth Rout

Here are two versions of the code.

这是代码的两个版本。

VER 1(Doesn't ignore Blank Cells)

VER 1(不忽略空白单元格)

'~~> For Group MERGING (Merge Cells and Keep All text)
Public Sub Sample()
    On Error GoTo ErrMergeAll

    Application.DisplayAlerts = False

    Dim Cl As Range
    Dim strTemp As String

    '~~> Collect values from all the cells and separate them with spaces
    For Each Cl In Selection
        If Len(Trim(strTemp)) = 0 Then
            strTemp = strTemp & Cl.Value
        Else
            strTemp = strTemp & vbNewLine & Cl.Value
        End If
    Next

    strTemp = Trim(strTemp)

    '~~> Merging of cells
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .MergeCells = False
    End With
    Selection.Merge

    '~~> Set new value of the range
    Selection.Value = strTemp

    Application.DisplayAlerts = True
    Exit Sub

ErrMergeAll:
    MsgBox Err.Description, vbInformation
    Application.DisplayAlerts = True
End Sub

VER 2(Ignores Blank Cells)

VER 2(忽略空白单元格)

'~~> For Group MERGING (Merge Cells and Keep All text)
Public Sub Sample()
    On Error GoTo ErrMergeAll

    Application.DisplayAlerts = False

    Dim Cl As Range
    Dim strTemp As String

    '~~> Collect values from all the cells and separate them with spaces
    For Each Cl In Selection
        If Len(Trim(Cl.Value)) <> 0 Then
            If Len(Trim(strTemp)) = 0 Then
                strTemp = strTemp & Cl.Value
            Else
                strTemp = strTemp & vbNewLine & Cl.Value
            End If
        End If
    Next

    strTemp = Trim(strTemp)

    '~~> Merging of cells
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .MergeCells = False
    End With
    Selection.Merge

    '~~> Set new value of the range
    Selection.Value = strTemp

    Application.DisplayAlerts = True
    Exit Sub

ErrMergeAll:
    MsgBox Err.Description, vbInformation
    Application.DisplayAlerts = True
End Sub

SCREENSHOT

截屏

enter image description here

在此处输入图片说明