Workbook Open Code Not Working

Grant22

New Member
Joined
Dec 28, 2016
Messages
48
Hello all, I am getting an error when I open a workbook. In a nutshell, for each sheet not named one of the sheets in the code I want to activate that sheet and add the months of the year in the combo box. Each sheet has a combobox1 on it, so I'm not sure why this isn't working. Any help is greatly appreciated. See below for the error message as well as the code:

Method or Data Member Not Found
Code:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Name <> "Home" And ws.Name <> "Emp's" And ws.Name <> "Metrics" _
        And ws.Name <> "Branch Performance" And ws.Name <> "EMP Performance" Then
        ws.Activate
    With ws.ComboBox1
        .Clear
        .AddItem ""
        .AddItem "January"
        .AddItem "February"
        .AddItem "March"
        .AddItem "April"
        .AddItem "May"
        .AddItem "June"
        .AddItem "July"
        .AddItem "August"
        .AddItem "September"
        .AddItem "October"
        .AddItem "November"
        .AddItem "December"
    End With
    ws.ComboBox1.Value = ""
    Next ws
End If
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You have your "Next ws" and "End If" statements reversed. Basically, you close them in the opposite order they were opened (so the last one opened is the first one closed), i.e.
Code:
For Each ws In Worksheets
    If ws.Name <> "Home" And ws.Name <> "Emp's" And ws.Name <> "Metrics" _
        And ws.Name <> "Branch Performance" And ws.Name <> "EMP Performance" Then
        ws.Activate
        With ws.ComboBox1
            .Clear
            .AddItem ""
            .AddItem "January"
            .AddItem "February"
            .AddItem "March"
            .AddItem "April"
            .AddItem "May"
            .AddItem "June"
            .AddItem "July"
            .AddItem "August"
            .AddItem "September"
            .AddItem "October"
            .AddItem "November"
            .AddItem "December"
        End With
        ws.ComboBox1.Value = ""
[COLOR=#ff0000]    End If[/COLOR]
[COLOR=#ff0000]Next ws[/COLOR]
If you indent them properly, it should be more evident ("End If" should line up with "If", "Next" should line up with "For").
 
Last edited:
Upvote 0
I dunno if it will completely fix your problem, but your "Next ws" line is inside of your "IF" code.

I'd flip it to this:

Code:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In Worksheets
    If ws.Name <> "Home" And ws.Name <> "Emp's" And ws.Name <> "Metrics" _
            And ws.Name <> "Branch Performance" And ws.Name <> "EMP Performance" Then
            ws.Activate
        With ws.ComboBox1
            .Clear
            .AddItem ""
            .AddItem "January"
            .AddItem "February"
            .AddItem "March"
            .AddItem "April"
            .AddItem "May"
            .AddItem "June"
            .AddItem "July"
            .AddItem "August"
            .AddItem "September"
            .AddItem "October"
            .AddItem "November"
            .AddItem "December"
        End With
        ws.ComboBox1.Value = ""
[COLOR=#ff0000]    End If
Next ws[/COLOR]
End Sub
 
Upvote 0
Thanks for catching that guys, I didn't even notice that issue yet. But I'm still getting the compile error.
 
Upvote 0
I am not sure why, but it apparently does not like to reference the sheet that way with the ComboBox object.
But this seems to not have any compile errors:
Code:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In Worksheets
    If ws.Name <> "Home" And ws.Name <> "Emp's" And ws.Name <> "Metrics" _
            And ws.Name <> "Branch Performance" And ws.Name <> "EMP Performance" Then
            ws.Activate
        With Sheets(ws.Name).ComboBox1
            .Clear
            .AddItem ""
            .AddItem "January"
            .AddItem "February"
            .AddItem "March"
            .AddItem "April"
            .AddItem "May"
            .AddItem "June"
            .AddItem "July"
            .AddItem "August"
            .AddItem "September"
            .AddItem "October"
            .AddItem "November"
            .AddItem "December"
        End With
        Sheets(ws.Name).ComboBox1.Value = ""
    End If
Next ws
End Sub
 
Upvote 0
Another way
Code:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In Worksheets
   If ws.name <> "Home" And ws.name <> "Emp's" And ws.name <> "Metrics" _
         And ws.name <> "Branch Performance" And ws.name <> "EMP Performance" Then
      ws.Activate
      With ws.OLEObjects("ComboBox1").Object
         .Clear
         .AddItem ""
         .AddItem "January"
         .AddItem "February"
         .AddItem "March"
         .AddItem "April"
         .AddItem "May"
         .AddItem "June"
         .AddItem "July"
         .AddItem "August"
         .AddItem "September"
         .AddItem "October"
         .AddItem "November"
         .AddItem "December"
      End With
     ws.OLEObjects("ComboBox1").Object.Value = ""
   End If
Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,449
Members
449,160
Latest member
nikijon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top