查找三 (3) 个单独工作表中的最后一行。 VBA Excel 2007
我一直在尝试编写一些代码,用于在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是一些基于我对您想要执行的操作的解释的代码。您不需要选择单元格来使用它们,而且 99.9% 的情况下您确实不应该这样做。
另外,在标注变量时,您应该这样标注:
在原始代码中,由于您只标注了最后一个变量,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.
Also when dimensioning variables you should dimension like this:
In your orginal code as you have only dimensioned the last variable, startrow and lastrow2 will be variant datatypes not long.
如果您有连续的数据,则可以使用 CurrentRegion,或者如果您知道自己在 A 列中一直有数据,则可以使用
来查找最后一行。
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
to find the last row.