show/hide textbox if comboxbox is null or not - excel

first of all i want to say thanks for every person who respond me for my previous quesiton,
i have a combobox (list) and a textbox(key1), but i want to enable textbox IF combobox is not null, I tried to to this by this code
Private Sub TextBox1_Change()
If IsNullOrEmpty(ComboBox1.Text) Then
TextBox1.Visible = False
TextBox1.Enabled = False
Else
TextBox1.Visible = True
TextBox1.Enabled = True
End If
End Sub
but the result is always the textbox1 is disabled, even if i choose from the list or combobox<>null

This is simpler than you are making it.
Instead, test for the length of the ComboBox1's text property being greater than 0. That will give you a boolean value of True/False. You can then use that boolean result to set any other boolean value property (like the Enabled or Visible property) all in one line.
Also, you need to put it on the Change event of ComboBox1, not TextBox1.
Private Sub ComboBox1_Change()
TextBox1.Enabled = (Len(ComboBox1.Text)>0)
End Sub
Or
Private Sub ComboBox1_Change()
TextBox1.Visible = (Len(ComboBox1.Text)>0)
End Sub

Related

Auto Select Checkbox triggered by another checkbox

I am trying to auto select a checkbox if any another checkbox is selected. All this checkboxes are on the same sheet and basically i want check box 7 to tick if checkbox 3,4 or 5 is selected.
Private Sub Worksheet_Change(ByVal Target As Range)
If Sheets("Start Page").CheckBox3 = True Or Sheets("Start Page").CheckBox4 = True Or
Sheets("Start Page").CheckBox5 = True Then
Sheets("Start Page").CheckBox7 = True
Else
End If
End Sub
Any help would be greatly appreciated.
Even if your checkboxes are linked to a cell (LinkedCell-property), the Worksheet-Change-event is not triggered when you click on a Checkbox.
You need to catch the Click-Event of the checkboxes. For every checkbox, put a Click-event routine into the sheet module. To prevent that the logic if or if not to set the "calculated" checkbox is repeated, let those event handler call a common routine that does the calculation.
Private Sub CheckBox3_Click()
Call SetMyCheckBox
End Sub
Private Sub CheckBox4_Click()
Call SetMyCheckBox
End Sub
Private Sub CheckBox5_Click()
Call SetMyCheckBox
End Sub
Sub SetMyCheckBox()
Me.CheckBox7.Value = Me.CheckBox3.Value Or Me.CheckBox4.Value Or Me.CheckBox5.Value
End Sub
You should, by the way, consider to give your checkboxes more meaningful names.

Using text box value if user selects combobox value?

I have made a combo box within a userform:
private sub Userform_Activate
cmbLocation.AddItem "Field"
cmbLocation.AddItem "Remote"
cmbLocation.AddItem "Other"
end sub
when a user selects Other I'd like a text box to populate for free form text input. When the user does this, I'd like that text to be the .value that is populating into the worksheet table.
is this even possible?
Create a textbox and set it to .visible = False when the userform is activated. Then using an if statement you can output the data from either the combobox or the textbox.
Private Sub UserForm_Activate()
Me.txtLocation.Visible = False
Me.cmbLocation.AddItem "Field"
Me.cmbLocation.AddItem "Remote"
Me.cmbLocation.AddItem "Other"
End Sub
Private Sub cmbLocation_Change()
If Me.cmbLocation.Value = "Other" Then
Me.txtLocation.Visible = True
Else
Me.txtLocation.Visible = False
End If
End Sub
Private Sub CommandButton1_Click()
If Me.txtLocation.Visible = True Then
ThisWorkbook.Sheet1.Range("A1").Value = Me.txtLocation.Value
Else
Thisworkbook.Sheet1.Range("A1").Value = Me.cmbLocation.Value
End If
End Sub
Modify sheet and range as necessary

How to make two textboxes in a userform mutually exclusive

I have two textboxes in a userform.
I need that if the user has entered some number in one textbox and then places the cursor in the other textbox, the first text box data is deleted and vice versa.
I was able to lock the other textbox if one has data in it, as a workaround.
I tried to replicate the lock method for deleting the value, but it does not work.
Sub checkTB()
If ConversionForm.Controls("UnitFromEntry").Text <> "" Then
ConversionForm.Controls("UnitToEntry").Locked = True
Else
ConversionForm.Controls("UnitFromEntry").Locked = True
End If
End Sub
I call this sub in the before update event of the text box.
Private Sub UnitFromEntry_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Call checkTB
End Sub
Try this
Private Sub UnitToEntry_Enter()
UnitToEntry.Locked = False
UnitFromEntry.Locked = True
UnitFromEntry.Text = ""
End Sub
Private Sub UnitFromEntry_Enter()
UnitFromEntry.Locked = False
UnitToEntry.Locked = True
UnitToEntry.Text = ""
End Sub

Month View in Userform - Close automatically

I want to click in a certain cell and for the Userform with the Monthview to appear , then once I have selected a date and it is inserted into the cell, i want the userform to close automatically
This code is in Work Sheet
Private Sub worksheet_selectionchange(ByVal target As Range)
If Not Application.Intersect(Range("l14"), target) Is Nothing Then
UserForm1.Show
End If
End Sub
This code is in the UserForm - MonthView1
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
ActiveCell.Value = DateClicked
End Sub
Any assistance would be Grateful
UserForm1.Show is toxic; you're essentially storing state in global scope, and that will inevitably cause issues down the line. Make a new instance of the form instead:
If Not Application.Intersect(Range("l14"), target) Is Nothing Then
With New UserForm1
.Show
End With
End If
Now, a form/dialog exists to collect user input, not to consume that input and change cell values. What happens to that input should be up to the code that's using that form, not up to the form itself. Expose the selected Date value with a property.
The user can do 2 things: either they select a date and you have a SelectedDate, or they clicked that [X] button and dismissed the form: You need to be able to tell what the user did. Handle QueryClose for that, and expose an IsCancelled property.
Selecting a date, or cancelling out, should hide the form, not destroy it.
Option Explicit
Private selectedValue As Date
Private cancelled As Boolean
Public Property Get IsCancelled() As Boolean
IsCancelled = cancelled
End Property
Public Property Get SelectedDate() As Date
SelectedDate = selectedValue
End Property
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
selectedValue = DateClicked
Me.Hide
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
Cancel = True
cancelled = True
Me.Hide
End If
End Sub
And now you have a date picker form that you can reuse whenever you need it, because it doesn't know or care about what happens to the value that was selected:
If Not Application.Intersect(Range("l14"), target) Is Nothing Then
With New UserForm1 ' todo: rename form to "DatePickerDialog" or something
.Show
If Not .IsCancelled Then
target.Value = .SelectedDate
End If
End With
End If
You could add unload me in that sub:
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
ActiveCell.Value = DateClicked
Unload me
End Sub

Invalid Qualifier - When trying to Fire Checkbox click

I assigned a macro to my checkbox, so that when you tick it, it sets a certain value into a cell. I however get a invalid qualifier error on my first Checkbox1.value.
Here's the code:
Option Explicit
Public CheckBox1 As Boolean
Sub CheckBox1_Click()
If CheckBox1.Value = True Then Range("Q10").Value = 1
If CheckBox1.Value = False Then Range("Q10").Value = 0
End Sub
Thanks!
I'm assuming you have used an ActiveX control with the name CheckBox1, if so you are having the problem because you've also defined the public boolean CheckBox1.
You're code is unhappy since the public declaration of the CheckBox1 boolean is overwriting the control variable CheckBox1 and a boolean doesn't have a .Value property. If you want to store the use of the checkbox value in a boolean then change your public variable name from Checkbox1, otherwise delete this declaration entirely.
Edit:
Based on your comment, you are not linking the checkbox from your worksheet. The code has to know what CheckBox1. Since you've deleted the boolean it now does not know what Checkbox1 is. Consequently you need to tell it where the checkbox is.
You can do it a number of ways. If it is a form checkbox (not ActiveX like I first thought) then the following code will work:
Sub Checkbox1()
Dim cb As Object, myCheckBoxValue As Boolean
'Get the object from the worksheet
Set cb = ActiveSheet.Shapes("Check Box 1").OLEFormat.Object
'Assign it's value to the boolean (1 is checked, -4146 is unchecked, 2 is mixed (grey box))
If cb.Value = 1 Then myCheckBoxValue = True Else myCheckBoxValue = False
'Output to worksheet
If myCheckBoxValue Then
ActiveSheet.Range("Q10").Value = 1
Else
ActiveSheet.Range("Q10").Value = 0
End If
Set cb = Nothing
End Sub
You can also cut this down significantly and the following will work:
Sub Checkbox1()
If ActiveSheet.Shapes("Check Box 1").OLEFormat.Object.Value = 1 Then
ActiveSheet.Range("Q10").Value = 1
Else
ActiveSheet.Range("Q10").Value = 0
End If
End Sub

Resources