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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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