"method worksheets of object_global failed" error when calling macro from workbook_open event

StackOverflow https://stackoverflow.com/questions/23678560

  •  23-07-2023
  •  | 
  •  

Frage

I'm getting a "method worksheets of object_global failed" error when I start up this worksheet. What really confuses me is that the wc_user_update macro runs fine when it is not called from the workbook_open event. 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. 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
War es hilfreich?

Lösung

Looks like the problem was that I needed to qualify Worksheets("WC_USERS") with ThisWorkbook. I also changed the variable name "update" to "update_choice." It's working now.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top