查找三 (3) 个单独工作表中的最后一行。 VBA Excel 2007

发布于 2024-11-27 14:04:10 字数 1132 浏览 1 评论 0原文

我一直在尝试编写一些代码,用于在 Excel 的工作表之间复制和插入数据。此过程的一部分涉及查找三张纸中每一张的最后一行或倒数第二行。但是在找到第一个工作表中的最后一行并激活第二个工作表后,程序不断给出错误 13:类型不匹配错误。我应该如何解决这个问题?

  Sub DeleteRow()

 Dim StartRow, Lastrow2, NuRow As Long
StartRow = 3
 Dim LastColumn As Long
 Dim LastRow As Long
 Dim LastCell As Long

Sheets("Sheet1").Activate
    'Search for any entry, by searching backwards by Rows.

LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows,  SearchDirection:=xlPrevious).Row

NuRow = LastRow - 1
Range(StartRow & ":" & NuRow).Select
Rows(StartRow & ":" & NuRow).Delete

Sheets("Sheet2").Activate
LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Range(1 & ":" & LastRow).Select
Selection.Copy

Sheets("Sheet1").Select
Selection.Insert Shift:=xlDown



    'Delete first two and last lines

Range("A1").Select
Rows("1:2").Delete
Range("A65536").End(xlUp).EntireRow.Delete Shift:=xlUp

Sheets("Sheet2").Activate
'Error 13: Type Mismatch -->Lastrow2 = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row  

I've been trying to write some codes for copying and inserting data between sheets on excel. And part of this process involves finding the last row or 2nd to last row of each of the three sheets. But after finding the last row in the first sheet, and after activating the 2nd sheet, the program keeps giving me an Error 13: type mismatch error. How should I resolve this problem??

  Sub DeleteRow()

 Dim StartRow, Lastrow2, NuRow As Long
StartRow = 3
 Dim LastColumn As Long
 Dim LastRow As Long
 Dim LastCell As Long

Sheets("Sheet1").Activate
    'Search for any entry, by searching backwards by Rows.

LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows,  SearchDirection:=xlPrevious).Row

NuRow = LastRow - 1
Range(StartRow & ":" & NuRow).Select
Rows(StartRow & ":" & NuRow).Delete

Sheets("Sheet2").Activate
LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Range(1 & ":" & LastRow).Select
Selection.Copy

Sheets("Sheet1").Select
Selection.Insert Shift:=xlDown



    'Delete first two and last lines

Range("A1").Select
Rows("1:2").Delete
Range("A65536").End(xlUp).EntireRow.Delete Shift:=xlUp

Sheets("Sheet2").Activate
'Error 13: Type Mismatch -->Lastrow2 = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半岛未凉 2024-12-04 14:04:10

下面是一些基于我对您想要执行的操作的解释的代码。您不需要选择单元格来使用它们,而且 99.9% 的情况下您确实不应该这样做。

Dim StartRow As Long

StartRow = 3

With Sheets("Sheet1")
    LastRow = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    .Rows(StartRow & ":" & LastRow - 1).Delete
End With

With Sheets("Sheet2")
    LastRow = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

    .Rows(1).Resize(LastRow).Copy
    .Rows(1).Resize(LastRow).Insert Shift:=xlDown

    .Rows("1:2").Delete

    .Range("A" & Rows.Count).End(xlUp).EntireRow.Delete Shift:=xlUp
End With

另外,在标注变量时,您应该这样标注:

Dim StartRow as long, Lastrow2 as long, NuRow As Long

在原始代码中,由于您只标注了最后一个变量,startrow 和 lastrow2 将是不长的变体数据类型。

Below is some code based on my interpretation of what you are trying to do. You don't need to select cells to work with them and 99.9% of the time you really shouldn't.

Dim StartRow As Long

StartRow = 3

With Sheets("Sheet1")
    LastRow = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    .Rows(StartRow & ":" & LastRow - 1).Delete
End With

With Sheets("Sheet2")
    LastRow = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

    .Rows(1).Resize(LastRow).Copy
    .Rows(1).Resize(LastRow).Insert Shift:=xlDown

    .Rows("1:2").Delete

    .Range("A" & Rows.Count).End(xlUp).EntireRow.Delete Shift:=xlUp
End With

Also when dimensioning variables you should dimension like this:

Dim StartRow as long, Lastrow2 as long, NuRow As Long

In your orginal code as you have only dimensioned the last variable, startrow and lastrow2 will be variant datatypes not long.

我要还你自由 2024-12-04 14:04:10

如果您有连续的数据,则可以使用 CurrentRegion,或者如果您知道自己在 A 列中一直有数据,则可以使用

lrow = Range("A65000").end(xlup).row

来查找最后一行。

If you have contiguous data, you can use CurrentRegion, or if you know you have data all the way down in e.g. Col A, then use

lrow = Range("A65000").end(xlup).row

to find the last row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文