Auto code running after opening a file

2avrham

Board Regular
Joined
May 12, 2014
Messages
104
Office Version
  1. 365
Hi,

I have the next code that run well after I click on play button in VB editor.
I need that this code will run automatically after I opened my file.. I tried to copy it to "thisworkbook" tab at VB editor but Im getting repeated error that something is wrong.
What I miss here?

My code:

Option Explicit
Private Dic As Object
Private ws As Worksheet
Sub ComboBox21_Change()
If ws.AutoFilterMode Then ws.AutoFilterMode = False
ws.Range("A5:T5").AutoFilter 5, Me.ComboBox21.Value
End Sub

Sub UserForm_Initialize()
Dim Cl As Range
Set ws = Sheets("Sheet1")
Set Dic = CreateObject("scripting.dictionary")
Dic.comparemode = vbTextCompare
For Each Cl In ws.Range("E6", ws.Range("E" & Rows.Count).End(xlUp))
If Not Dic.Exists(Cl.Value) Then Dic.Add Cl.Value, Nothing
Next Cl
Me.ComboBox21.List = Dic.keys
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
In the thisworkbook module

Code:
Private Sub Workbook_Open()
[COLOR=#00FF00]your code here[/COLOR]

End Sub
 
Upvote 0
Can you please explain what you are trying to achieve?
As you cannot simply put all that code into a workbook_open event.
 
Upvote 0

Forum statistics

Threads
1,214,938
Messages
6,122,346
Members
449,080
Latest member
Armadillos

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