-1

我需要编写一个宏来清理 Excel 文档。我现在可以使用大多数功能,但是在将两行数据合并在一起时遇到了麻烦。我需要先合并两行数据,然后删除第二行。这段代码来自我遇到问题的函数,我正在删除正确的行,但我想首先将我要删除的行与上面的行合并在一起,这样就不会丢失数据;有谁知道是否有一个快速命令可以将整行合并在一起,还是我必须逐个单元格地进行。

For RowCount = Selection.Rows.Count To 1 Step -1

    ' Delete rows that don't have text in col A
    If Selection.Cells(RowCount, 1).Text <> "" And IsNumeric(Selection.Cells(RowCount, 1).Value) Then
        'Is Number


    ElseIf Selection.Cells(RowCount, 1).Text = "Heading" Then
        'Its the Heading
    Else

        'Its not needed!
        '*********************************************************
        'CODE TO MERGE TWO ROWS SHOULD GO HERE********************
        '(MERGE ROWCOUNT-1 WITH ROWCOUNT AND DELETE ROWCOUNT)*****
        '*********************************************************

        Selection.Rows(RowCount).EntireRow.Delete
    End If

' Start next iteration of RowCount loop.
Next RowCount
4

1 回答 1

1

我认为您需要逐个单元格地进行操作。

代替:

'*********************************************************
'CODE TO MERGE TWO ROWS SHOULD GO HERE********************
'(MERGE ROWCOUNT-1 WITH ROWCOUNT AND DELETE ROWCOUNT)*****
'*********************************************************

和:

Dim ColumnCount As Integer, intCnt As Integer
ColumnCount = Selection.columns.count

For intCnt = 1 To ColumnCount

    Dim strRow As String, strPrevRow As String
    strRow = Selection.Cells(RowCount, intCnt).Text
    strPrevRow = Selection.Cells(RowCount, intCnt).offset(-1).Text

    Selection.Cells(RowCount, intCnt).offset(-1) = strRow & " " & strPrevRow

Next
于 2012-06-21T18:57:25.680 回答