【问题标题】:Finding Last Row - Referencing Excel Object Library in VBScript查找最后一行 - 在 VBScript 中引用 Excel 对象库
【发布时间】:2013-01-10 18:11:04
【问题描述】:

我编写了一个简短的脚本,当我登录到我的计算机时,它会在电子表格中记录时间,但我无法正确定义最后一行。如果我在VBA 中写入,下面的代码会执行得很好,但在执行VBScript 时会收到错误消息。

VBScript 将Rows 来自Rows.Count 作为变量,因此它会引发错误,因为我打开了Option Explicit。如果我尝试将其限定为.Rows.Count,它认为xlup 是一个变量。

我是否需要引用 VBScript 中的 Excel 对象库,还是应该完全使用不同的语法来查找 VBScript 中的 LR?

Option Explicit

Dim xlapp
Dim oWB
Dim oWS
Dim LR

Set xlapp = CreateObject("excel.application")
Set oWB = xlapp.Workbooks.Open("H:\HR Stuff\time_management.xls")
Set oWS = oWB.Sheets("Sheet1")

With oWS
    LR = .Range("A" & Rows.Count).End(xlUp).Row
         .Cells(LR, 1).Value = Now
End With

oWB.Close True

xlapp.Quit
Set xlapp = Nothing

【问题讨论】:

    标签: excel vbscript vba


    【解决方案1】:

    xlUp 是 VBA 已知的常量,但 VBScript 不知道。你必须自己定义它:

    Const xlUp = <Use Excel interactively to get the correct magic Number>*
    
    • 意思类似于“Debug.Print xlUp”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      相关资源
      最近更新 更多