VBA - 查找工作表中的下一个空行

时间:2014-11-19 23:41:47

标签: excel vba

如何找到工作簿中的下一个空行,我想要粘贴我在另一个工作簿中提前复制的一些内容。 这就是我现在得到的:

Sub Retânguloarredondado1_Click()

Dim InputFile As Workbook
Dim OutputFile As Workbook
Dim Inputpath As String
Dim Outputpath As String '
Dim TP As Worksheet
Dim copyRange As Range
Dim pasteRange As Range
Dim cel As Range
Dim test As Range
Dim MyAr() As Variant
Dim n As Long
Dim LastRow As Long

' Set path for Input & Output
fileInputpath = "C:\Users\Nuno Bonaparte\Desktop\"
Outputpath = "C:\Users\Nuno Bonaparte\Desktop\"

'## Open both workbooks first:
Set InputFile = ActiveWorkbook
Set OutputFile = Workbooks.Open(Outputpath & "file2.xlsm")
Set TP = OutputFile.Worksheets("Folha1")
Set copyRange = InputFile.Sheets("file2").Range("A1,B3,C5,D7,E9")
Set pasteRange = OutputFile.Sheets("Folha1").Range("A1")



'~~> Get the count of cells in that range
        n = copyRange.Cells.Count

        '~~> Resize the array to hold the data
        ReDim MyAr(1 To n)

        n = 1

        '~~> Store the values from that range into
        '~~> the array
        For Each cel In copyRange.Cells
            MyAr(n) = cel.Value
            n = n + 1
        Next cel


'Now, paste to OutputFile worksheet:
OutputFile.Sheets("Folha1").Activate
pasteRange.Cells(1, 1).Resize(1, UBound(MyAr)).Value = _
    MyAr

'Close InputFile & OutputFile:
'InputFile.Close
OutputFile.Save
OutputFile.Close

End Sub

代码工作正常,但我想找到下一个空行并粘贴新内容

谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

With OutputFile.Sheets("Folha1").Range("A:A").Find(vbNullString, [A1])
    .Resize(1, Ubound(MyAr)) = MyAr
End With

上面的代码查找A1之后的第一个空单元格并应用resize并在该单元格中指定值 这是你在尝试的吗?你也可以check this out to give you more idea。 HTH。