宏excel将值复制到空白单元格并粘贴到下一页

时间:2015-02-18 15:19:47

标签: excel vba copy paste

我有一张包含许多行的工作表,并且行数可能因不同的类似文件而异。我需要获取一个脚本,我可以复制行中的值,直到下一行为空并将其粘贴到下一页谢谢你。

1 个答案:

答案 0 :(得分:1)

单击要考虑的第一行中的任何单元格并运行此小宏:

Sub marine()
    Dim rng As Range, sh As Worksheet, i As Long, j As Long
    Dim sName As String, wf As WorksheetFunction
    Dim r1 As Long, r2 As Long
    Set wf = Application.WorksheetFunction
    Set rng = ActiveCell
    Set sh = ActiveSheet
    sName = sh.Name
    r1 = rng.Row

    For i = 1 To Sheets.Count
        If Sheets(i).Name = sName Then
            j = i + 1
            Exit For
        End If
    Next i

    For i = r1 To Rows.Count
        If wf.CountA(Cells(i, 1).EntireRow) = 0 Then
            Range(Cells(r1, 1), Cells(i - 1, 1)).EntireRow.Copy Sheets(j).Range("A1")
            Exit Sub
        End If
    Next i

End Sub