gpt4 book ai didi

excel - Selection.End(xlToRight) 不起作用

转载 作者:行者123 更新时间:2023-12-04 21:40:46 24 4
gpt4 key购买 nike

我很难让这条 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' 

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

请帮忙

最佳答案

这是因为您是从 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

关于excel - Selection.End(xlToRight) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12825425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com