gpt4 book ai didi

vba - Excel vba : object variable or with block variable not set error when returning Object from function

转载 作者:行者123 更新时间:2023-12-04 21:10:03 25 4
gpt4 key购买 nike

我想使用一个应该返回 Excel.Application 的函数目的:

 Dim ExcelApp As Object
Set ExcelApp = getExcelApp()



Function getExcelApp() As Object
Dim ExcelApp As Object
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = False
ExcelApp.ScreenUpdating = False
ExcelApp.DisplayAlerts = False
ExcelApp.EnableEvents = False

getExcelApp = ExcelApp
End Function

但我得到一个 object variable or with block variable not set错误。有什么问题以及如何完成我想要的?

最佳答案

错误是因为您缺少 Set command (这是必需的,因为您正在分配一个对象)。

但是您正在创建一个对象,对其进行设置,然后将其分配给另一个对象以返回。您可以改为将所有内容直接分配给返回变量。
更简洁的函数是:

Function getExcelApp() As Object
Set getExcelApp = CreateObject("Excel.Application")
With getExcelApp
.Visible = False
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
End Function

关于vba - Excel vba : object variable or with block variable not set error when returning Object from function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45836745/

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