Userform worksheet_open does not work - vba

Private Sub Workbook_Open()
UserForm2.Show
End Sub
Very simple VBA code.
How come my UserForm2 not opening when the workbook gets opened?

Put the Workbook_Open() sub in ThisWorkbook module, like #Rich Holton said.

Related

vba - why does userform come up twice after closing thisworkbook

I created a simple userform for testing, but one issue comes up and i have no idea how to resolve it.
This is the case:
When I open .xlsm file for the first time and close the userform and workbook, this closes both of them.
But when I continue to click the .xlsm file again, close the userform and the workbook, it (the userform) pops up again.
It seems the workbook doesn't close.
Then i need to close the userform again to close this workbook.
I would like help to understand why the userform comes up again?
The userform only has one listbox controls.
The following code is in a module:
Private Sub Auto_Open()
UserForm1.Show
MsgBox "Close2"
ThisWorkbook.Close
End Sub
The userform contains the following code:
Private Sub UserForm_Initialize()
Application.Visible = False
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
MsgBox "Close1"
End Sub
You could try to put this into the workbook code section as followed.
And not put it in a separate module.
Private Sub Workbook_Open()
Application.Visible = False
userform1.Show
MsgBox "Close2"
ThisWorkbook.Close
End Sub
you can remove the code for the user form initialize.
The thing is as its hidden you might open it twice cause you do cause it does not respond directly.

Limiting user interaction with the workbook - hide workbook / show only userform

Is it possible to get the same effect using ThisWorkbook.Application.Visible = False but only for one Workbook. I mean, I'd like to limiting user interaction only to UserForm, but I need have an access to anothers workbooks. At the moment this function cause hide workbook, but after open some another excel file - all object from userform are not available.
Private Sub Workbook_Open()
ThisWorkbook.Application.Visible = False
Starter.Show modeless
End Sub
Thanks for your support.
Please, create a form, let us say "Starter", having (at least) a button ("btExit"), copy the next code in its code module and show it. If the form in discussion already has some code in Initialize and Terminate events, please add the next code lines, too:
Option Explicit
Private Sub btExit_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
ThisWorkbook.Windows(1).Visible = False
End Sub
Private Sub UserForm_Terminate()
ThisWorkbook.Windows(1).Visible = True
End Sub
So, you can simple use workbook Open event in this way:
Private Sub Workbook_Open()
Starter.Show vbModeless
End Sub

VBA: Prevent Excel from updating links on open

I have a workbook with multiple external links,
I don't want these to update when the workbook is opened, by me or any other user.
I have tried to achieve this with the simple work book open command below:
Private Sub Workbook_Open()
Workbook.UpdateLinks = xlUpdateLinksNever
End Sub
Am I missing something ?
Put the code in the ThisWorkbook:
Private Sub Workbook_Open()
Me.UpdateLink = xlUpdateLinksNever
End Sub

Auto_open & Workbook_open with Connection refresh in VBA

I have a script that refreshes my data connection to a SQL server. I need to the macro to run when the workbook is opened but for some reason it will not. I guess it has to do with the connection refresh. I tried with both Workbook_Open and Auto_Open() and neither are working. Other than the data load the only thing I am doing is filtering and copying data, nothing exotic.
Does the data refresh need permissions outside of the script, is that the problem?
Here are the first couples lines.
Sub Auto_Open()
ActiveWorkbook.Connections("Connection Name").Refresh
other stuff
end sub
You are putting the code in 'ThisWorkbook' object, right.
Private Sub Workbook_Open()
ActiveWorkbook.RefreshAll
End Sub
Private Sub Workbook_Open()
'Step 1: Use the RefreshAll method
Workbooks(ThisWorkbook.Name).RefreshAll
End Sub
user6058587 was close, but you just need to add this to ThisWorkbook:
Private Sub Workbook_Open()
'Step 1: Use the RefreshAll method
Workbooks(ThisWorkbook.Name).RefreshAll
End Sub

VBA Events: load workbook before running code using workbook_open

I want to run a VBA macro AFTER the workbook has finished opening. I tried to use workbook_open but this runs before the workbook has finished opening. This doesn't work for me as I need to loop through each sheet like so...
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
'do stuff on each sheet
Next ws
End Sub
Does anyone know if there is an event which runs once the workbook has finished opening? Or have any other suggestions on how I can achieve this?
I had a similar problem that I solved using Application.OnTime.
From the linked MSDN Library Article:
Schedules a procedure to be run at a specified time in the future
(either at a specific time of day or after a specific amount of time
has passed).
You could try using this method to provide the workbook with enough time to open before running the DoStuff procedure:
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:00:01"), "DoStuff"
End Sub
Ensure the DoStuff procedure is not in a worksheet module:
Private Sub DoStuff()
'Implementation...
End Sub
The time can be adjusted but one second was satisfactory for me.
Put your code in the Workbook_Activate event. It happens after the Open event.
Private Sub Workbook_Activate()
' Code goes here
End Sub
Try using ThisWorkbook rather than ActiveWorkbook:
Private Sub Workbook_Open()
Dim osht As Worksheet
For Each osht In ThisWorkbook.Worksheets
Debug.Print osht.Name
Next osht
End Sub