当" Lastrow"是什么让Sub停在第12135行?数据是第19195行?

时间:2016-06-17 12:12:51

标签: vba excel-vba excel

下面写的Sub旨在打开工作簿并将工作表复制到模板中,然后关闭工作簿,使模板保持打开状态。它有效,但是直到第19195行才有数据,但只复制了12135行数据。我在Sub中的问题是什么?

 Sub CopySheetsl()

Dim wb As Workbook, wb1 As Workbook
Dim CopySht As Worksheet
Dim LastRow As Long

Set wb = Workbooks.Open("L:\ABC\test\macro\test.xlsx")
Set wb1 = Workbooks("macro.xlsm")

LastRow = range("A:A").Find("", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

wb1.Sheets("Sheet1").range("A1", "N1" & LastRow) = wb.Sheets("Sheet1").range("A1", "N1" & LastRow).Value
wb1.Sheets("Sheet2").range("C1", "AN1" & LastRow) = wb.Sheets("Sheet2").range("A1", "AL1" & LastRow).Value

 wb.Close


 End Sub

2 个答案:

答案 0 :(得分:2)

这找不到最后一行,它找到一个空单元格。

Dim ws as Worksheet : Set ws = wb1.Sheets("Sheet1")
LastRow = ws.Cells(ws.rows.count, 1).End(xlUp).Row ' last populated row in column A

除非您完全确定两张纸的行数相同,否则您还需要为Sheet2重新计算它。

答案 1 :(得分:0)

来自Ron De Bruin的网站

    Public Function fndLast(choice As Long, rng As Range)
    'Ron de Bruin, 5 May 2008
    ' 1 = last row
    ' 2 = last column
    ' 3 = last cell
        Dim lrw As Long
        Dim lcol As Long

        Select Case choice

        Case 1:
            On Error Resume Next
            fndLast = rng.Find(What:="*", _
                            After:=rng.Cells(1), _
                            LookAt:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Row
            On Error GoTo 0

        Case 2:
            On Error Resume Next
            fndLast = rng.Find(What:="*", _
                            After:=rng.Cells(1), _
                            LookAt:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByColumns, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Column
            On Error GoTo 0

        Case 3:
            On Error Resume Next
            lrw = rng.Find(What:="*", _
                           After:=rng.Cells(1), _
                           LookAt:=xlPart, _
                           LookIn:=xlFormulas, _
                           SearchOrder:=xlByRows, _
                           SearchDirection:=xlPrevious, _
                           MatchCase:=False).Row
            On Error GoTo 0

            On Error Resume Next
            lcol = rng.Find(What:="*", _
                            After:=rng.Cells(1), _
                            LookAt:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByColumns, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Column
            On Error GoTo 0

            On Error Resume Next
            fndLast = rng.Parent.Cells(lrw, lcol).Address(False, False)
            If Err.Number > 0 Then
                fndLast = rng.Cells(1).Address(False, False)
                Err.Clear
            End If
            On Error GoTo 0

        End Select
    End Function