[英]“method worksheets of object_global failed” error when calling macro from workbook_open event
I'm getting a "method worksheets of object_global failed" error when I start up this worksheet. 启动此工作表时,出现“ object_global方法工作表失败”错误。 What really confuses me is that the wc_user_update macro runs fine when it is not called from the workbook_open event. 真正令我困惑的是,当未从workbook_open事件调用wc_user_update宏时,它运行良好。 Also, when I comment out the password protection in the workbook open event it still doesn't work. 另外,当我在工作簿打开事件中注释掉密码保护时,它仍然不起作用。 So the problem seems to be in how I am calling the wc_user_update macro from the Workbook_Open event. 因此,问题似乎出在我如何从Workbook_Open事件中调用wc_user_update宏。 Am I missing something obvious? 我是否缺少明显的东西?
Code in ThisWorkbook: 此工作簿中的代码:
Private Sub Workbook_Open()
'Dim ws As Worksheet
'For Each ws In Worksheets
'ws.Protect password:="x", UserInterfaceOnly:=True
'Next ws
wc_user_update
End Sub
Code in a standard module: 标准模块中的代码:
Public Sub wc_user_update()
'Ask user if he wants to update usernames. If yes, prompt for credentials
Dim update As Variant
'On Error GoTo ErrorHandler
With Worksheets("WC_USERS")
update = MsgBox("Usernames have not been updated in " & .Range("wc_names_last_updated_days").Value & " days. Would you like to update now?", vbYesNo)
End With
If update = vbYes Then
'show the windchill authentication userform centered in the excel window
With wc_auth
.StartUpPosition = 0
.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
.Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
.Show
End With
'Do stuff
'update "last updated date" cell
Worksheets("WC_USERS").Range("wc_names_last_updated_date").Value = Date
Unload wc_auth
Else
MsgBox ("usernames not updating")
End If
ErrorHandler:
End Sub
Looks like the problem was that I needed to qualify Worksheets("WC_USERS") with ThisWorkbook. 看来问题在于我需要使用ThisWorkbook来限定Worksheets(“ WC_USERS”)。 I also changed the variable name "update" to "update_choice." 我还将变量名“ update”更改为“ update_choice”。 It's working now. 现在正在工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.