1

我很难让这条 VBscript 行与 excel 对象一起工作:

set fso=CreateObject("Scripting.FileSystemObject") 
Set WShell = CreateObject("WScript.Shell") 
Set objExcel = createobject("Excel.application") 

objexcel.Visible = true 
objexcel.Application.ScreenUpdating = True 

objexcel.Workbooks.Open dir & masterFileName 
objexcel.Activeworkbook.Worksheets("xActive_User_Ratio").Activate 
objexcel.Range("A1").Select 
objexcel.Range(Selection, Selection.End(xlToRight)).Select 

当我运行此代码时,出现错误:

Object required: 'Selection' 

我究竟做错了什么?任何示例都会非常有帮助。

请帮忙

4

1 回答 1

4

这是因为您是从 Excel 外部运行它的。

使用objExcel.Selection而不仅仅是Selection. 这样您的代码就知道 Selection 与 Excel 应用程序相关联。此外,您需要定义 xlToRight 或将其替换为它的数值。

更好的是,我会像这样使用并重写整个事情:

Set fso = CreateObject("Scripting.FileSystemObject")
Set WShell = CreateObject("WScript.Shell")
Set objexcel = CreateObject("Excel.application")
xlToRight = -4161 ' -4161 is the value of xlToRight
With objexcel
    .Visible = True
    .Application.ScreenUpdating = True
    'using variables for workbook and worksheet to be explicit
    Set wb = .Workbooks.Open(Dir & masterFileName)
    Set ws = wb.Worksheets("xActive_User_Ratio")
    ws.Activate
    ws.Range("A1").Select
    ws.Range(.Selection, .Selection.End(xlToRight)).Select        
End With
于 2012-10-10T18:00:46.410 回答