VBA - variable gets ComboBox value else gets TextBox value error - vba

I am writing a VBA code that is opened from a UserForm, which includes 2 ComboBoxes and 2 TextBoxes to form a 2D matrix by user's choice.
What I'm trying to do is, have a variable get the combobox value (one variable for each combobox) and if the selection is null, then the variables get the value of the textboxes.
I've googled the expression I need to use for that and stumbled upon IsNull, but the code doesn't work. What I tried was:
If IsNull(Matrix_Size_1.value) = False And IsNull(Matrix_Size_2.value) = False Then
lWidth = Matrix_Size_1.value
lHeight = Matrix_Size_2.value
ElseIf IsNull(Matrix_Size_1.value) = True And IsNull(Matrix_Size_2.value) = True Then
lWidth = Matrix_Hand_1.value
lHeight = Matrix_Hand_2.value
End If
Of course I also have to filter more plausible situations as: if one combobox is empty then msgbox, if everything is empty then msgbox but I'll handle it once I'll understand why my code doesn't work.
But the error I get if I DO NOT choose anything from the comboboxes is Type mismatch even though the If code has an option for both of the comboboxes to be empty.
I'd appreciate your help.

If Comboboxes .Style property is set as fmStyleDropDownList then use this
If Matrix_Size_1.ListIndex = -1 And Matrix_Size_2.ListIndex = -1 Then
If Comboboxes .Style property is set as fmStyleDropDownCombo then use this
If Len(Trim(Matrix_Size_1.Value)) = 0 And Len(Trim(Matrix_Size_2.Value)) = 0 Then
You can check the Style property in the design mode by right clicking on the combobox and clicking on properties.
Edit
From the comments, I have understood that you actually want this
Dim lWidth As Single, lHeight As Single
If Len(Trim(Matrix_Size_1.Value)) = 0 And Len(Trim(Matrix_Size_2.Value)) = 0 Then
lWidth = Val(Matrix_Hand_1.Value) '<~~> Val will take care of mismatch caused by empty boxes
lHeight = Val(Matrix_Hand_2.Value)
Else
lWidth = Val(Matrix_Size_1.Value)
lHeight = Val(Matrix_Size_2.Value)
End If

Related

Change the visibility of UserForm elements via For loop

I have an array of constant size populated with ComboBoxes. When the user clicks on another UserForm element (a CheckBox in my case) the visibility each ComboBox in this array should change. When I run the below snippet of code, I get 424 Object Required error. I am most likely making a simple syntactic mistake, but any guidance on how to properly write this would be appreciated.
Private Sub MyCheckBox_Click()
Dim CheckBoxStatus as Boolean: CheckBoxStatus = MyUserForm.MyCheckBox.Value
Dim ComboBoxArray(0 To 4)
ComboBoxArray(0) = MyUserForm.ComboBox1 ' This line repeated and modified for the other 4 array members
If CheckBoxStatus Then
For i = LBound(ComboBoxArray) To UBound(ComboBoxArray)
CompetitorComboBoxes(i).Visible = True ' THIS IS WHERE THE ERROR IS THROWN
Next i
Else
' Appropriate else code to set the visibilities to False
End Sub
You need to Set object references so:
Set ComboBoxArray(0) = MyUserForm.ComboBox1
Dim ComboBoxArray(0 To 4) creates an array with 5 elements not the 4 you would need for your 4 boxes so you should Dim ComboBoxArray(0 To 3) (or just Dim ComboBoxArray(3)) otherwise the last element will be empty and calling .Visible on it will fail.
(I'm assuming CompetitorComboBoxes is a typo for ComboBoxArray)

VBA Checkbox to Listbox (uncheck option to remove array)

I am struggling with a simple thing and cannot resolve it. I have a userform that user can populate from a textbox manually. I decided to add a checkbox as well to allow the user to populate the same listbox with a specific list of items. To do it,I made a simple checkbox with array. It works perfectly fine. But obviously keeps adding the items every time you check and uncheck it.
Private Sub Checkbox1_Click()
Dim mylist(7) As String
Dim i As Long
mylist(0) = "time"
mylist(1) = "hour"
mylist(2) = "how"
mylist(3) = "test"
mylist(4) = "number"
mylist(5) = "sent"
mylist(6) = "memo"
mylist(7) = "value"
For i = 0 To 7
If CheckBox1.Value = True Then
Finallist.AddItem mylist(i)
End If
Next i
End Sub
I can populate the list when the checkbox is checked with the code above, but struggling to remove the array items from the list when the user unchecks the listbox. I simply need to remove the same items from listbox when user unchecks the same checkbox.
I tried the following solution after the code, but seem to be making something very wrong with it, I understand. Just totally stuck....Could someone help me please?
If checkobx.value=false then
For i = 0 To 7
For j = 0 To FinalList.ListCount - 1
If InStr(Final.List(j), mylist(i)) > 0 Then
Finallist.RemoveItem mylist(i)
End If
Next j
Next i
end if
Try this (explanations in comments and untested):
If CheckBox1.Value Then ‘ if checkbox checked
For i = 0 To 7
NegKeyList.AddItem interrlist(i)
Next
Else ‘otherwise
Dim i As Long
For i = NegKeyList.ListCount - 1 To 0 Step -1 ‘ loop through listbox items from last one backwards
If Not IsError(Application.Match(NegKeyList.List(i), interrlist,0)) Then NegKeyList.RemoveItem i ‘ if current listbox item matches any interrlist array item then remove it from listbox
Next
End If

Excel VBA to set RadioButton GroupNames

I am creating a quiz. It works well for the part I have but is a bit lengthy. The RadioButtons I am using are set at the start through the Properties window. I have a "Reset" CommandButton that I am using to clear the quiz.
' Question Block 1
' THIS method does work
ActiveSheet.OptionButton1.Enabled = True
ActiveSheet.OptionButton2.Enabled = True
ActiveSheet.OptionButton3.Enabled = True
ActiveSheet.OptionButton4.Enabled = True
ActiveSheet.OptionButton1.Value = False
ActiveSheet.OptionButton2.Value = False
ActiveSheet.OptionButton3.Value = False
ActiveSheet.OptionButton4.Value = False
' Question Block 1 (Alternate Method)
' THIS method does NOT work
ActiveSheet.OptionButton1.GroupName = "Q1"
ActiveSheet.OptionButton2.GroupName = "Q1"
ActiveSheet.OptionButton3.GroupName = "Q1"
ActiveSheet.OptionButton4.GroupName = "Q1"
ActiveSheet.OptionButton.GroupName("Q1").Enabled = True
ActiveSheet.OptionButton.GroupName("Q1").Value = False
The first block of code works fine. The second one does not. What am I doing wrong with trying to assign the RadioButtons to a group in this manner? I've tried numerous approaches and most end with error messages saying object does not support this property or method, method or member not found, object required and on and on. Scratching my head. --Edited I am NOT using this in a UserForm. The RadioButtons are just placed on the Excel Sheet.
Found an answer that seems to work really well for my purposes. I was looking to do this by GroupNames property but as long as it works there are a number of ways to solve any problem. Hope someone else can benefit from this code.
' Defines OptionButtons as Objects
Dim OptBtn As OLEObject
' Re-Enables RadioButtons
For Each OptBtn In Sheet1.OLEObjects
If TypeName(OptBtn.Object) = "OptionButton" Then
OptBtn.Object.Enabled = True
End If
Next
' Resets RadioButtons to False (removes the dot)
For Each OptBtn In Sheet1.OLEObjects
If TypeName(OptBtn.Object) = "OptionButton" Then
OptBtn.Object.Value = False
End If
Next

Manipulating ListBoxes as Objects

I am working on a VBA userform that includes ListBoxes.
So far, when I had to manipulate one or more, I always proceeded like this in my subs, with dlg as the dialogbox name, and it did not pose any problem, given that I never wanted to do anything complicated:
Dim List1 As Object
...
List1 = dlg.GetControl("CBXname")
...
List1.addItem("String",index)
...
Now I would like to do the following in this Sub
...
If (List1.Exists(Cell1.String) = False) Then
List1.addItem(Cell1.String,k)
End If
...
List1.Clear
...
But I can do neither since List1 is an Object. However, if I decide to declare List1 as a Listbox instead, I do not know how to get the proper control on the ListBox from the dialogbox (the current getcontrol gives me an error).
One of the issues with your code is that listbox objects do not have an "exists" property. To check if a value already exists in your listbox items, you will need to loop through the items.
dim i as integer
for i = 0 to List1.listcount - 1
if List1.column(0, i) = myvalue then
'myvalue exists in List1, skip
else
List1.additem myvalue
end if
next i
Where myvalue is whatever value you are trying to add to the listbox. But that brings us to the second issue in your code which is where you add "Cell1.String". If you are trying to add a value from a worksheet range you will need to refer to that range's value, as worksheet ranges do not have a "string" property as you use it here. Ie. Cell1 = Range("A1").value
As for getting control of the listbox, you can simply refer to the objects name as an object of the form. For example, dlg.List1, if the object's name is List1.
Here is a general purpose routine you can call for any list box. The calling code assumes a list box called ListBox1, a text box called TextBox1, and a Command Button called CommandButton. When you click on the button, it searches the listbox for the text from textbox1.
Private Function ExistsInListbox(ByRef aListBox As msforms.ListBox, ByVal Item As String) As Boolean
Dim booFound As Boolean
booFound = False
Dim t As Integer
ExistsInListbox = False
For t = 0 To aListBox.ListCount - 1 'correction, aListBox not ListBox1
If Item = aListBox.List(t) Then
'if we find a match, short-circuit the loop
booFound = True
Exit For
End If
Next
ExistsInListbox = booFound
End Function
private sub CommandButton_click()
Dim answer As String
Dim val As Boolean
val = ExistsInListbox(Me.ListBox1, TextBox1.Text)
If val Then
answer = "found"
Else
answer = "Not Found"
End If
MsgBox "found-" & answer
End Sub

Compile error with If statement on radiobutton.value

I'm trying to perform an action 'if a radioboxed have been checked", but I'm getting an error:
Compile error: Method or data member not found.
I've created a userform with four radiobuttons (using Controls toolbox), and a commandbutton. The userform loads in an excelsheet (with the click of a separate button), and it is possible to check the radiobutton. if the radiobutton is checked and I click the command button I want some action to happen, but it wont compile my code.
Private Sub cmdCSV_Click()
Dim JurBen As Integer
With Thisworkbook
If .lblRKinst.Value = True Then
JurBen = 1
MsgBox "hurray"
ElseIf .lblRKkon.Value = True Then
JurBen = 2
ElseIf lblForinst = True Then
JurBen = 3
ElseIf lblForkon = True Then
JurBen = 4
Else: Exit Sub
MsgBox ("Choose an option")
End If
It seems to dislike the "value" statement, which works fine with checkboxes? I've tried with "enabled" and without anything. I seem to be the only person on the internet with this problem...
As I've used loads of time on this tiny issue, and seem to be stuck, any help would be greatly appreciated!
If the Radioboxes are on UserForm then if you want to check their value then 1. the UserForm must be loaded at that time and 2. you need to refer to the UserForm.
Example:
if UserForm1.OptionButton1.Value = true then
The radiobox (OptionButton1 in my example) is member of UserForm and not of ThisWorkbook.
As written by Matteo NNZ I was simply referencing the label and not the radiobutton next to it.
No problem what so ever, as the code above works fine.