为什么在VBScript中执行End(xlDown)时会出现“未知运行时错误”?

时间:2017-09-13 17:11:49

标签: excel-vba vbscript vba excel

为什么在VBScript中执行End(xlDown)会发生“未知运行时错误”?

Dim objExcel, objSheet, pbjPath, objBook
objPath="C:\Users\Documents\automateexcel.xls"

Set objExcel = CreateObject("Excel.Application")
Set objBook = objExcel.Workbooks.Open(objPath)
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)

objSheet.Cells(1,1).Value = "Punter"
objExcel.Range("A1").End(xlDown).Offset(1,1).Value = "14"

objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close

编写行objExcel.Range("A1").End(xlDown).Offset(1,1).Value = "14"以填充第一列中的第一个空单元格,其值为14.我能够硬编码到工作表中的任何单元格中。但是这种填充空单元格的xlDown方法导致了未知的运行时错误。

  

C:\ Try \ automateexcel.vbs(9,1)
  Microsoft VBScript运行时错误:未知的运行时错误

1 个答案:

答案 0 :(得分:-3)

    Dim objExcel As Object, objSheet As Worksheet, pbjPath As Object, objBook As Object
    Dim rcount As Long

    objPath = "C:\Users\Documents\automateexcel.xls"

    Set objExcel = CreateObject("Excel.Application")
    Set objBook = objExcel.Workbooks.Open(objPath)
    Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)

    objSheet.Cells(1, 1).Value = "Punter"
    rcount = objSheet.Cells(Rows.Count, "A").End(xlUp).Row
    If rcount = 1 Then objSheet.Range("A1").Offset(1, 0).Value = "14" Else _
        objSheet.Range("A1").End(xlDown).Offset(1, 0).Value = "14"

    objExcel.ActiveWorkbook.Save
    objExcel.ActiveWorkbook.Close
End Sub