【问题标题】:Macro code from Excel 2003 doesn't work in 2007Excel 2003 中的宏代码在 2007 年不起作用
【发布时间】: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”,这是个问题。大声笑至少我想我用一点点戳头就解决了。谢谢悉达多。

【问题讨论】:

  • LastRowLong 类型,但似乎对Cells.Find() 的调用没有得到Long 值。运行此 VBA 宏的电子表格是否也发生了变化?
  • @Tim 没有任何改变。只需复制代码以在家中使用,以便进行测试,我已经填充了一张表格,用作类似于工作中的日志,但此时它只是失败了。我回家后会尝试 Siddharth 的建议。我知道从 2003 年到 2007/10 年的可移植性存在一些差异,作为新手程序员,我仍在弄清楚这一点。我最终会在家中使用更新版本的 Excel 编写相同的代码,以便在我们切换到更新版本时主动重写我的代码以供工作,因此我准备好进行转换,相对而言不会感到头疼。

标签: vba excel excel-2007


【解决方案1】:

您确定它适用于 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

【讨论】:

  • @Siddharth 我在几个文件上使用此代码,这些文件保存到单独的日志表中。需要最后一行,这在 2003 Excel 中效果很好。但是当我尝试在我的 Excel 2007 上使用代码时它失败了。我已经使用该代码超过 10 个月了,直到 2007 年尝试使用该代码。当我回到家时,我会尝试将 .Row.Rows .
猜你喜欢
  • 2011-08-01
  • 2013-03-01
  • 1970-01-01
  • 2012-06-03
  • 2011-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多