【发布时间】:2015-07-24 23:44:13
【问题描述】:
此代码在 Excel 2003 中运行良好,但在 Excel 2007 中失败。我在其中没有看到什么导致它崩溃?当它到达 "LastRow =".. 这是我的错误信息: 运行时错误 13 类型不匹配
Dim LastRow As Long
Dim LastColumn As Integer
Dim LastCell As Range, NextCell As Range
' ****************************************************
' Finds LastRow and LastColumn
With Worksheets("DB")
' Find Last Row/Col
If WorksheetFunction.CountA(Cells) > 0 Then
' Search for any entry, by searching backwards by rows
LastRow = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
' Search for any entry, by searching backwards by columns
LastColumn = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
End If
Set NextCell = Worksheets("DB").Cells(LastRow + 1, (LastColumn))
End With
' ****************************************************
发现错误。猜猜我复制了 Lastrow 并打算将第二个更改为列。但这仍然不能解决第一个块的问题。将最后一部分编辑为列时的操作我看到我可能在 .Rows 中输入了一个额外的“s”看起来应该是 .Row 我会看到什么时候我回家了,因为我在工作中的硬拷贝没有显示“s”。猜猜这就是我回家后尝试“记住”代码时得到的结果。 “s”还是“s”,这是个问题。大声笑至少我想我用一点点戳头就解决了。谢谢悉达多。
【问题讨论】:
-
LastRow是Long类型,但似乎对Cells.Find()的调用没有得到Long值。运行此 VBA 宏的电子表格是否也发生了变化? -
@Tim 没有任何改变。只需复制代码以在家中使用,以便进行测试,我已经填充了一张表格,用作类似于工作中的日志,但此时它只是失败了。我回家后会尝试 Siddharth 的建议。我知道从 2003 年到 2007/10 年的可移植性存在一些差异,作为新手程序员,我仍在弄清楚这一点。我最终会在家中使用更新版本的 Excel 编写相同的代码,以便在我们切换到更新版本时主动重写我的代码以供工作,因此我准备好进行转换,相对而言不会感到头疼。
标签: vba excel excel-2007