Excel userform not opening in unchecked form - excel

I need a userform to load the the checkbox values in their unchecked form. They open with the boxes unchecked but they don't seem to be initializing that way. Here is the checkbox code and the open button code I have.
Private Sub cbxAdditional_Click()
If Me.cbxAdditional.Value Then
Me.Width = 524
Me.Height = 238.5
lstDatabase.Width = 480
txbxName.Visible = True
txbxComments.Visible = True
Else
Me.Width = 279
Me.Height = 238.5
lstDatabase.Width = 240
txbxName.Visible = False
txbxComments.Visible = False
End If
End Sub
--open code
Sub openform()
Test.Show
cbxAdditional = False
End Sub
I clearly am writing this wrong, when I opne the user form it starts in the IF form, how do I get it to open in the unchecked form?

Here's a very simple userform:
When you enter a userform, the UserForm_Initialize sub is executed. This sub is NOT automatically created in the code with the userform, but you can create it yourself. Any initialization or set up code should be performed here.
In my example below, I'm showing that the resetting of the checkboxes is a separate sub. I do this as a habit, just in case I have to add a reset button or something.
Option Explicit
Private Sub ClearCheckboxes()
Me.CheckBox1.Value = False
End Sub
Private Sub ResetButton_Click()
ClearCheckboxes
End Sub
Private Sub UserForm_Initialize()
ClearCheckboxes
End Sub

Related

Entire Application Disappears (runs in background) instead of opening worksheet "Sheet1" in VBA

I have 2 Userforms. In userform1, sheet to open is selected together with some textbox. In Userform2, there are additional option boxes and a command button to unload Userform2 and Activate worksheet 1 but it won't load...i used to be able to run it until i added a code to prompt user to complete fields..not sure what is wrong with my code that it won't proceed opeining the worksheet anymore
Userform1 Code:
Private Sub CommandButton1_Click()
If TextBox1.Value = "" Then
MsgBox "Pls. enter data"
Cancel = True
Exit Sub
End If
If Sheet1.Value = True Then
Unload UserForm1
UserForm2.Show
Userform2 Code:
*'to prompt user to complete fields*
Private Sub CommandButton1_Click()
If OptionBox1.Value = False And OptionBox2.Value = False Then
MsgBox "Pls. select option"
Cancel = True
Exit Sub
End If
*'to open worksheet "Sheet1"*
If UserForm1.Sheet1.Value = True Then
Worksheets("Sheet1").Activate
Application.Visible = True
Unload UserForm2
End If
End Sub
Once you unload UseForm1 it's no longer accessible from UserForm2 - you should instead Hide UserForm1 until you're done with it, and only then Unload it.

Hide the Excel Application before Showing Userform

I have got this code below to hide my workbook in Excel VBA, but it doesn't work as expected because it still shows the application as a window there.....
Private Sub Workbook_Open()
Worksheets("Main Page").Activate
'ActiveWindow("GESTAO DE EXPEDIENTES.XLSM").Visible = False
Windows("GESTAO DE EXPEDIENTES.XLSM").Visible = False
LoginDAFForm.Show
End Sub
You need to use the Application's Visible property.
Private Sub Workbook_Open()
Application.Visible = False
LoginDAFForm.Show
End Sub
I personally would recommend that you show the application upon the userform's termination event. If you agree, then add this code to your userform's code module as well:
Private Sub UserForm_Terminate()
Application.Visible = True
End Sub
You should use Application.Visible property to hide application. The code you used hide only the window you mentioned. See the code below
Private Sub Workbook_Open()
Application.Visible = False
LoginDAFForm.Show
End Sub
It is highly recommend to set Application.Visible property to True when you close the userform. Otherwise you will not be able to see any other excel file which are being open. You can use K.Dᴀᴠɪs code for this and I'm just pasting it here
Private Sub UserForm_Terminate()
Application.Visible = True
End Sub

Disable a command button if mandatory fields are empty

I am creating a form out of excel-ActiveX but I have no idea how I can make the command button disabled if all mandatory fields (textbox and combobox) are empty. I've attached the sample form for your reference.
sample LA Form
Use the *_Change event for each control (and optionally the UserForm_Activate event) to set the .Enabled property for the Control Button.
For example, on a simple UserForm with 1 ComboBox (ComboBox1), 2 TextBoxes (TextBox1 and TextBox2) and 1 CommandButton (CommandButton1), the following code will ensure that CommandButton1 is disabled unless there is data in each of the other 3 controls:
Option Explicit
Private Sub CheckMandatoryFields()
'Is a separate sub to make it easy to edit for all Controls at the same time
If Len(TextBox1.Value) > 0 And Len(TextBox2.Value) > 0 And Len(ComboBox1.Value) > 0 Then
CommandButton1.Enabled = True
Else
CommandButton1.Enabled = False
End If
End Sub
Private Sub CommandButton1_Click()
MsgBox "Button is enabled!"
End Sub
Private Sub ComboBox1_Change()
CheckMandatoryFields
End Sub
Private Sub TextBox1_Change()
CheckMandatoryFields
End Sub
Private Sub TextBox2_Change()
CheckMandatoryFields
End Sub
Private Sub UserForm_Activate()
CheckMandatoryFields
End Sub
If your Buttons and Controls are in a Worksheet, then you need to change UserForm_Activate to Worksheet_Activate. If they are in different worksheets, or some in a Worksheet and others in a UserForm, then you will need to Fully Qualify your references (e.g. Sheet1.Textbox1.Value)
If you are using Form Controls in a Worksheet instead of ActiveX controls, then you can't use the _Change events, and the way you Reference the objects is different:
Sheet1.Shapes("TextBox1").TextFrame2.TextRange.Text 'The value of TextBox1
Sheet1.Shapes("ComboBox1").ControlFormat.Value 'Which Number item in ComboBox1 is selected
Sheet1.Shapes("ComboBox1").ControlFormat.List(n) 'The value of the nth item in ComboBox1
A Form Control button does not have an Enabled property - however, if you have a "Do Nothing" macro, you can bodge it like so:
Sub DoNothing()
'As it says
End Sub
Sub MakeASound()
Beep
End Sub
Sub ToggleButton()
If Sheet1.Shapes("Button 1").OnAction = "Sheet1!DoNothing" Then 'Disable Button 1
Sheet1.Shapes("Button 1").OLEFormat.Object.Font.ColorIndex = 16 'Font colour = Grey
Sheet1.Shapes("Button 1").OnAction = "Sheet1!DoNothing"
Else 'Enable Button 1
Sheet1.Shapes("Button 1").OLEFormat.Object.Font.ColorIndex = 1 'Font colour = Black
Sheet1.Shapes("Button 1").OnAction = "Sheet1!MakeASound"
End If
End Sub

Excel form listbox issue when using locked property

I'm having an issue with the way a listbox behaves on an Excel form. Steps to reproduce the issue:
Create a user form with one listbox control
Use following code with this user form:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.ListBox1.Locked = True
Me.ListBox1.Locked = False
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 10
Me.ListBox1.AddItem i
Next i
End Sub
When the form is first shown, I am able to navigate the list box normally, using arrow keys and page keys. However, after the double-click event is triggered, all keyboard navigation has no effect, including tabbing to other controls (if they're on the form). Clicking on the listbox does seem to work, and the focus outline is shown correctly, but there's something wrong with the way the focus is handled after the listbox is locked and then unlocked. What is going on?
Using Office 2013 32-bit edition.
I managed to replicate this issue, and setting the focus somewhere else before locking and unlocking the listbox worked for me:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.TextBox1.SetFocus 'or some other control.
Me.ListBox1.Locked = True
Me.ListBox1.Locked = False
Me.ListBox1.SetFocus
End Sub
2 solutions I tested that allows you to use Arrow Keys to navigate.
Given that there isn't a single click event handler, try call the Single Click Event after the DblClick (works all the time):
Private Sub ListBox1_Click()
Debug.Print "ListBox1_Click()"
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "ListBox1_DblClick()"
With Me.ListBox1
.Locked = True
.Locked = False
End With
ListBox1_Click
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 10
Me.ListBox1.AddItem i
Next i
End Sub
Setting the Cancel = False at the end of DblClick. (Sometimes does not work!)
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "ListBox1_DblClick()"
With Me.ListBox1
.Locked = True
.Locked = False
End With
Cancel = False
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 10
Me.ListBox1.AddItem i
Next i
End Sub
Changing the zoom in the sheet that has the listbox worked for me.
Private Sub Worksheet_Activate()
Dim temp As Double
Application.ScreenUpdating = False
'Change worksheet zoom setting for the active window
temp = ActiveWindow.Zoom
ActiveWindow.Zoom = temp + 10
ActiveWindow.Zoom = temp
Application.ScreenUpdating = True
End Sub

How to use VBA to read the values of a checkbox from an Excel userform

I have created a userform that contains two checkboxes. I would like to be able to do different things depending on whether each box is checked or unchecked. However, it seems like no matter what I do, it will always tell me the original value of the checkboxes (false and false). Here is the code attached to clicking CommandButton1:
Private Sub CommandButton1_Click()
ReadData
End Sub
And here ReadData:
Sub ReadData()
Dim myForm As UserForm
Set myForm = UserForms.Add("ComplaintEntryForm")
Debug.Print (myForm!CheckBox1.Name)
Debug.Print (myForm!CheckBox1.Value)
Debug.Print (myForm!CheckBox2.Name)
Debug.Print (myForm!CheckBox2.Value)
End Sub
No matter how the boxes are checked, the immediate window always shows this:
VBA.UserForms.Add("ComplaintEntryForm").Show
CheckBox1
False
CheckBox2
False
I have a screenshot of the whole operation but it won't let me upload it because I'm a new user.
Try this method to load and show the form (this goes in a normal module):
Sub main()
Dim myForm As ComplaintEntryForm
Set myForm = New ComplaintEntryForm
myForm.Show
Set myForm = Nothing
End Sub
In the UserForm's own module, add the following:
Private Sub CheckBox1_Change()
readData
End Sub
Private Sub CheckBox2_Change()
readData
End Sub
Private Sub UserForm_Initialize()
Me.CheckBox1.Value = True
Me.CheckBox2.Value = False
End Sub
Private Sub readData()
Debug.Print Me.CheckBox1.Name
Debug.Print Me.CheckBox1.Value
Debug.Print Me.CheckBox2.Name
Debug.Print Me.CheckBox2.Value
End Sub
I've initialized the two checkboxes to specific values in the Initialize event. This means we are certain about the state the form will start in

Resources