gpt4 book ai didi

vba - 下标超出范围 - 打开两个 excel 文档

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

这是我的问题:

Private Sub Workbook_Open()
'the name of the current user
Dim UserName As String
'on opening, find out who this is (and convert to lower case)
Dim wb As Workbook
Set wb = Workbooks("Staffing Log - HQ - 2018 - 2019.xlsm")
wb.Activate


UserName = LCase(Environ("UserName"))
On Error Resume Next

Application.Visible = False
'ActiveWindow.Visible = False
Sheets("GoodDBData").Visible = True ERROR OCCURS HERE
end sub

当我在打开另一个工作簿的同时打开此工作簿时,我不断使下标超出范围。就好像即使我告诉程序使用 wb.Activate 激活正确的工作簿,它也无法识别它。

知道为什么吗?
谢谢

最佳答案

您的代码在 ThisWorkbook 私有(private)代码表中。在私有(private)代码表中,任何不完全限定的引用都会将父级恢复到该工作簿/工作表。

Sheets("GoodDBData").Visible = True

这默认为包含代码的工作簿。如果您在 Workbooks("Staffing Log - HQ - 2018 - 2019.xlsm") 中寻找 GoodDBData 工作表,则限定父工作簿。
wb.workSheets("GoodDBData").Visible = True

Recommended reading

我有点不清楚您为什么打算隐藏应用程序实例但希望此工作表可见。

关于vba - 下标超出范围 - 打开两个 excel 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52302876/

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