Excel 2003中的macros代码在2007年不起作用

此代码在Excel 2003中运行良好,但在Excel 2007中失败。我没有看到它在崩溃吗? 它到达“LastRow =” 时出错 。这是我的错误消息: 运行时错误13types不匹配

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 ' **************************************************** 

发现一个错误。 猜猜我复制了拉斯特罗 ,并打算将第二个更改为列。 但是这仍然不能解决第一块的问题。 Opps,同时编辑最后一部分的列我看到,我可能在.Rows中键入一个额外的“s”看起来应该是.Row我会看到,当我回家时,因为我的硬拷贝工作显示没有“S” 。 猜猜这就是当我回家时试着“记住”代码时所得到的结果。 对于“是”还是“不”,这是个问题。 大声笑至less我觉得我解决了一点点捅头。 感谢Siddharth。

你确定它适用于Excel 2003吗?

您必须使用.Row而不是.Rows 。 看到这个

你的代码也会失败,因为你的LastColumn=0

这是你正在尝试?

 Sub Sample() Dim LastRow As Long Dim LastColumn As Integer Dim NextCell As Range With Worksheets("DB") If WorksheetFunction.CountA(Cells) > 0 Then '~~> Find Last Row LastRow = Cells.Find(What:="*", After:=[A1], _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious).Row '~~> Find Last Column LastColumn = Cells.Find(What:="*", After:=[A1], _ SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious).Column End If '~~> Set the cell to the first empty cell after the last cell '~~> which has data Set NextCell = Worksheets("DB").Cells(LastRow + 1, (LastColumn)) '~~> Display the address of that cell MsgBox NextCell.Address End With End Sub