简体   繁体   English

合并Excel和VBA中的行

[英]Merge rows in Excel & VBA

I want to merge rows in Excel: the content to merge can be in different columns, "C" or "D" in my example. 我想在Excel中合并行:要合并的内容可以在不同的列中,在我的示例中为“ C”或“ D”。 Any way I can do this using VBA? 我可以使用VBA做到这一点吗? The file has ~20k rows. 该文件有约2万行。

My File: http://i.imgur.com/yDPdaQC.png 我的文件: http //i.imgur.com/yDPdaQC.png

文件

Goal: http://i.imgur.com/SZ5t9oX.png 目标: http //i.imgur.com/SZ5t9oX.png

文件

Edit with more details: 编辑更多详细信息:

Some sentences from the C & D columns are divided in 2,3 and sometimes 4 rows. C&D列中的某些句子分为2,3行,有时分为4行。 I would like to merge those strings at the "top" cell from their respective column, when "A" and "B" have a value. 当“ A”和“ B”具有值时,我想将这些字符串合并到其各自列的“顶部”单元格中。

Thanks for your help! 谢谢你的帮助!

you can use this. 你可以用这个

Sub Merge()
    Dim ws As worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
    Dim ws2 As worksheet: Set ws2 = ThisWorkbook.Sheets("Sheet2")

    Dim sheet2Rng As Range: Set sheet2Rng = ws2.UsedRange
    Dim startRow As Integer: startRow = LastRow(ws) + 1
    Dim ws2RowCount As Integer: sheet2Rng.Rows.Count

    ChangeEvents False
    ws.Range("A" & startRow).Resize(ws2RowCount, 4).value = sheet2Rng.value
    ChangeEvents True
End Sub

Public Function LastRow(worksheet As worksheet) As Integer
    LastRow = worksheet.Cells(Rows.Count, 1).End(xlUp).Row
End Function

Sub ChangeEvents(value As Boolean)
    Application.EnableEvents = value
End Sub

Can you clarify? 你能澄清一下吗? Are you trying to: 您是否要:

  • Create merged cells: C1 with D1, C2 with D2, etc? 创建合并的单元格:C1与D1,C2与D2,等等? This will lose the contents of the D column. 这将丢失D列的内容。
  • Take the texts in column D and append them to the end of the column C cells; 将D列中的文本添加到C列单元格的末尾;
  • Create a new column which contains the C + D appended texts 创建一个包含C + D附加文本的新列

Something like this: 像这样:

Sub SquishRows()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim rng As Range, rr As Range
    Dim rowdata As Variant
    Dim i As Integer, idx As Integer, j as Integer

    Set sh1 = Worksheets("Sheet1")
    Set sh2 = Worksheets("Sheet2")
    sh1.Activate

    Set rng = Range("A2").Resize(sh1.UsedRange.rows.Count - 1, sh1.UsedRange.Columns.Count)

    ReDim rowdata(Application.CountA(rng.Columns(1)), rng.Columns.Count - 1)

    idx = 0
    For i = 1 To rng.rows.Count
        Set rr = rng.rows(i)
        If Len(rr.Cells(1).Text) And Len(rr.Cells(2).Text) Then
            idx = idx + 1
            For j = 1 To rng.Columns.Count
                rowdata(idx, j - 1) = rr.Cells(j).Text
            Next
        Else
            For j = 3 To rng.Columns.Count
                If Len(rr.Cells(j).Text) Then
                    rowdata(idx, j - 1) = rowdata(idx, j - 1) & " " & rr.Cells(j).Text
                End If
            Next
        End If
    Next

    'push data to Sheet2
    sh2.Range("A1").Resize(UBound(rowdata, 1) + 1, UBound(rowdata, 2) + 1).Value = rowdata

    'add in header row
    sh2.Range(sh1.UsedRange.rows(1).Address).Value = sh1.UsedRange.rows(1).Value

    sh2.Activate
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM