1

我有这个脚本,我一直收到一个错误:Run-time error '1004': Method 'Range' of object _'Worksheet' failed

这是脚本:

Option Explicit
    Sub ONJL()

    Dim lastrow As Long
    Dim wsRD As Worksheet 'Raw Data

    Set wsRD = Sheets("Raw Data")

        With wsRD
            Application.ScreenUpdating = False
            lastrow = wsRD.Range("J:J").End(xlUp).Offset(1, 0)
            wsRD.Range("J" & lastrow).Value = "=Today()"
        End With

    End Sub

我认为这就是您要查找最后一行,然后将今天的日期粘贴到最后一个单元格的方式?

4

2 回答 2

2

有几个问题:

  • 使用时with,需要使用.
  • 的结果Offset(x,y)是一个范围,而不是一个数字 - 你对行感兴趣,所以你需要添加.Row
  • 事情必须从End(xlUp)最后一行开始工作

这可能是你的意思:

Sub ONJL()

    Dim lastrow As Long
    Dim wsRD As Worksheet 'Raw Data

    Set wsRD = Sheets("Raw Data")

    With wsRD
        Application.ScreenUpdating = False
        lastrow = .Range("J" & .Rows.Count).End(xlUp).Row
        .Range("J" & lastrow + 1).Formula = "=Today()"
    End With

End Sub

并且不要忘记在某处打开屏幕更新。

于 2012-07-09T15:37:20.153 回答
2

lastrow被声明为 Long 变量,但Offset返回一个Range对象。属性为您提供行的Row数值。

Sub ONJL()

  Dim lastrow As Long
  Dim wsRD As Worksheet  'Raw Data

  Set wsRD = Sheets("Raw Data")

  With wsRD
    Application.ScreenUpdating = False
    lastrow = .Cells(Rows.Count, 10).End(xlUp).Offset(1, 0).Row
    .Range("J" & lastrow).Formula = "=Today()"
  End With

Application.ScreenUpdating = True

End Sub
于 2012-07-09T16:01:09.713 回答