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

时间:2015-07-24 04:59:51

标签: excel vba excel-vba excel-2007

此代码在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 ,并且正在将第二个更改为列。但是仍然没有解决第一块的问题。编辑最后一部分到列的Opps我看到我可能输入了额外的" s"在 .Rows 看起来应该 .Row 我会看到我什么时候回家,因为我在工作中的硬拷贝显示没有" s&#34 ;。猜猜这是我在尝试"记住"我回家的代码。到" s"或者不是" s",这就是问题。大声笑至少我认为我解决了它有点戳头。谢谢Siddharth。

1 个答案:

答案 0 :(得分:2)

您确定它适用于Excel 2003吗?

您必须使用.Row代替.Rows。见This

您的代码也会失败,因为您的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