Unable to load userform - vba

I am unable to load an userform on opening workbook. Object is Workbook, procedure is Open and have this code in ThisWorkbook.
Private Sub Workbook_Open()
frmWelcome.Show
End Sub

Here's some code that I use, placed in a module called mMain with the form called fForm. I suggest that you start fresh, with something like this, make sure that works, and proceed from there, changing one thing at a time.
Option Explicit
Public dLog As fForm
Sub main()
Set dLog = New fForm
dLog.Show vbModeless 'doesn't have to be modeless
End Sub

Related

Putting common command button code in ThisWorkbook

After moving command button code into the ThisWorkbook area as opposed to the individual sheet it doesn't appear to get called. Is there a reason for this, is there some way I can have the same code called from multiple buttons each on different worksheets?
The command button is a worksheet object, not a workbook object.
With this being said, you can reuse the code by keeping the command button in your worksheet's object, but place the main code in a standard module (keep it away from your workbook's module).
This would be in a standard module:
Sub mainCode(ws As Worksheet)
'Your Code
End Sub
This would be in your Worksheet's private module:
Private Sub CommandButton1_Click()
mainCode ws 'make a call to the code above
End Sub

Workbook_Open() sheet.activate error in protected view when enable editing

I have problems with sheets.activate contained into Workbook_Open() when the file is in protected mode (because downloaded from internet). As soon as you "Enable Editing" the Workbook_Open() is executed and the error comes out.
If I use this code:
Private Sub Workbook_Open()
Sheets(2).Activate
End Sub
I have this error:
Runtime Error 1004 Activate method of Worksheet class failed
Reading in some other discussion i tried using the Workbook_Activate() method which works if I have only one simple activation in all my project. The previous example could be fixed if I use:
Private Sub Workbook_Open()
a = True
End Sub
Private Sub Workbook_Activate()
If a Then
Sheets(2).Activate
a = False
End If
End Sub
but it partially fix the problem, this code could work but next time i have another sheets.activate in my project the error comes out again, (i.e. if i click a button into a panel or if i run other routines).
This error comes out only the first time you open the file, if you stop the debugger, close the file without saving and reopen the file the error doesn't come out again but i would avoid it to come out the first time
Thanks in advance
This looks like it is a known issue:
Object Model calls may fail from WorkbookOpen event when exiting Protected View
It says ...
Resolution
You can workaround the issue by either :-
If the location from where the workbooks are being open is trusted, add that location to the Excel's Trusted Locations.
Defer Object Model calls to outside of the WorkbookOpen event to WorkbookActivate event.
Code example to defer object model calls
Option Explicit
Public WithEvents oApp As Excel.Application
Private bDeferredOpen As Boolean
Private Sub oApp_WorkbookActivate(ByVal Wb As Workbook)
If bDeferredOpen Then
bDeferredOpen = False
Call WorkbookOpenHandler(Wb)
End If
End Sub
Private Sub oApp_WorkbookOpen(ByVal Wb As Workbook)
Dim oProtectedViewWindow As ProtectedViewWindow
On Error Resume Next
'The below line will throw error (Subscript out of range) if the workbook is not opened in protected view.
Set oProtectedViewWindow = oApp.ProtectedViewWindows.Item(Wb.Name)
On Error GoTo 0 'Reset error handling
If oProtectedViewWindow Is Nothing Then
bDeferredOpen = False
Call WorkbookOpenHandler(Wb)
Else
'Delay open actions till the workbook gets activated.
bDeferredOpen = True
End If
End Sub
Private Sub WorkbookOpenHandler(ByVal Wb As Workbook)
'The actual workbook open event handler code goes here...
End Sub
Check the file properties before you open it. If it comes from the internet, there is usually a "Unprotect" checkbox on the properties page you can check to unprotect it, then it should open like normal.
There is also a "Read-only" checkbox that might help you too. Make sure the file is not read only.
Just do this before you open it.

Userform not triggering Initialize or Activate event

I kept a userform control button in my worksheet to fire up a macro, which in turn shows a user form, In the form I wish to display the opened files in checkboxes(using the Workbooks collection).I wish to run a macro that performs action for the user selected files only.
So for the button in my worksheet, I have assigned the following macro
Private Sub Button2_Click()
Load MyForm
MyForm.Show
End Sub
At first I kept the below code in the module where my macro sub is there.Since it's not working, I right clicked on user form and selected view code and kept the below code there.But still it's showing the same static designed user form, not the dynamic.I kept breakpoint at both load Myform and MYform.Show() and I stepped through code.It never went into intialize or activate method at all.
Private Sub MyForm_Activate()
'for checking the whether this method is called or not I am trying to change caption
MyForm.LabelSelectFile.Caption = "dhfdfldkfldzjf;zdfkz;d"
Dim mymyWorkBook As Workbook
For Each mymyWorkBook In Workbooks
'code for creating checkbox based on the file name displayed by the workbook collection
Next mymyWorkBook
End Sub
I can't understand why that event is not getting triggered.Please help me to overcome this.Thanks in advance
Even though the name of the form is MyForm, you still need to use userform.
'~~> in your worksheet
Private Sub Button2_Click()
MyForm.Show
End Sub
'~~> In the userform code area
Private Sub UserForm_Initialize()
'~~> Your code here
End Sub
or
Private Sub UserForm_Activate()
End Sub
The best is to always select the event from the drop down, rather than typing it

Having an MS Office UserForm detect which subroutine called it

In a VBA project of mine I am/will be using a series of reasonably complex userforms, many of which are visually identical but have different subroutines attached to the buttons. As a result I'm not overly keen on the idea of duplicating them multiple times in order to get different functionality out of the same layout. Is it possible to have a userform detect which subroutine called it and use this in flow control? I would like to be able to do something like this:
Private Sub UserForm_Initialize()
If [the sub that called the userform is called "foo"] then
Call fooSub
else
Call barSub
End If
End Sub
My backup plan is to have the calling subroutine set a global variable flag and have the userform check that, but that seems like a rather crude and clumsy solution.
Thanks everyone,
Louis
You can use the tag property of the form. Load the form, set the property, then show the form:
Sub PassCallerToForm()
Load UserForm1
UserForm1.Tag = "foo"
UserForm1.Show
End Sub
Now that the property is set, you can determine what to do in the form:
Private Sub UserForm_Activate()
If Me.Tag = "foo" Then
Call fooSub
Else
Call barSub
End If
End Sub
You can also use public variables:
' in userform
Public Caller As String
Private Sub UserForm_Click()
MsgBox Caller
Caller = Now()
Me.Hide
End Sub
' in caller
Sub callUF()
Dim frm As New UserForm1
frm.Caller = "Test Caller"
frm.Show
MsgBox frm.Caller ' valid after Me.Hide
Set frm = Nothing
End Sub
Personally, I would not have one userform doing two disparate activities. The code would get hard to read pretty quickly, I think. Copying the layout of a userform is pretty trivial.
To copy a userform: Open a blank workbook. In the Project Explorer, drag the userform to the new workbook. Rename the userform in the new workbook. Now drag it back to the original workbook. Change the code in the userform copy.
If you absolutely don't want separate userforms, I recommend setting up a property of the userform. Userforms are just classes except they have a user interface component. In the userform module
Private mbIsFoo As Boolean
Public Property Let IsFoo(ByVal bIsFoo As Boolean): mbIsFoo = bIsFoo: End Property
Public Property Get IsFoo() As Boolean: IsFoo = mbIsFoo: End Property
Public Sub Initialize()
If Me.IsFoo Then
FooSub
Else
BarSub
End If
End Sub
I always write my own Initialize procedure. In a standard module:
Sub OpenForm()
Dim ufFooBar As UFooBar
Set ufFooBar = New UFooBar
ufFooBar.IsFoo = True
ufFooBar.Initialize
ufFooBar.Show
End Sub

Excel VBA - Assign value from UserForm ComboBox to Global Variable

I have a userform with a basic combobox and command button. When the user hits the command button, I want the UserForm to close, and the value of the combobox to be saved in a variable that can be accessed by a subroutine contained within "ThisWorkbook".
In the UserForm code:
Public employee_position As String
Public Sub CommandButton1_Click()
employee_position = Me.ComboBox1.Value
Unload Me
End Sub
In the "ThisWorkbook" Code
Private Sub GetUserFormValue()
Call Userform_Initialize
EmployeePosition.Show
MsgBox employee_position
End Sub
When "GetUserFormValue()" runs, the UserForm comes up, you can select a value in the combobox and press the command button, but when the MsgBox comes up, it displays "" (Nothing)
What am I doing wrong here?
When you Unload Me, I think you lose all information associated with the module (including the global variable). But if you use Me.Hide rather than Me.Unload, then you can access the value of the form after the routine returns. So try this:
-- userform code includes:
Public Sub CommandButton1_Click()
Me.Hide
End Sub
-- main module includes:
Private Sub GetUserFormValue()
Call Userform_Initialize
EmployeePosition.Show
MsgBox EmployeePosition.ComboBox1.Value
Unload EmployeePosition
End Sub
I think that should work.
I had the same problem, and this is how I resolved it:
If the main code is in a worksheet, and the variable is declared as public in that worksheet (e.g. in Microsoft Excel Objects -> Sheet1 (Sheet1)), the result from "Unload Me" cannot be passed from a UserForm to the worksheet code.
So to solve my problem, I inserted a new Module, and declared my public variable there. I didn't even have to move my code from the worksheet to the module... just the declaration of the public variable.
I hope this works for you too!
Andrew