ListBox not populating - excel

I am a beginner using VBA in Excel. I am trying to come up with a user form that looks like this. I have all the coding in, but when I launch it from a command button in Excel, the ListBox does not populate. When I try to enter in numbers and click "submit" I get "Run-time error '424':Object required". When I click debug, it takes me to the line
Cells(emptyRow, 1).Value = dotwListBox.Value
I am not sure what is going on. Any help would be appreciated!! Here is my code:
Private Sub cancel_Click()
Unload Me
End Sub
Private Sub clear_Click()
Call UserForm1_Initialize
End Sub
Private Sub submit_Click()
Dim emptyRow As Long
'Make Sheet3 active
Sheet3.Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
'Transfer information
Cells(emptyRow, 1).Value = dotwListBox.Value
Cells(emptyRow, 2).Value = t235tocbTextBox.Value
Cells(emptyRow, 3).Value = t235codbTextBox.Value
Cells(emptyRow, 4).Value = apiphbTextBox.Value
Cells(emptyRow, 5).Value = apiturbiditybTextBox.Value
Cells(emptyRow, 6).Value = apitocbTextBox.Value
Cells(emptyRow, 7).Value = apicodbTextBox.Value
Cells(emptyRow, 8).Value = apibodbTextBox.Value
Cells(emptyRow, 9).Value = longbaydobTextBox.Value
Cells(emptyRow, 10).Value = asudobTextBox.Value
Cells(emptyRow, 11).Value = rasmlssbTextBox.Value
Cells(emptyRow, 12).Value = clarifierturbiditybTextBox.Value
Cells(emptyRow, 13).Value = clarifierphbTextBox.Value
Cells(emptyRow, 14).Value = clarifiernh3bTextBox.Value
Cells(emptyRow, 15).Value = clarifierno3bTextBox.Value
Cells(emptyRow, 16).Value = clarifierenterococcibTextBox.Value
Cells(emptyRow, 17).Value = clarifierphosphorusbTextBox.Value
End Sub
Private Sub UserForm1_Initialize()
'Empty t235tocbTextBox
t235tocb.Value = ""
'Empty t235codTextBox
t235codb.Value = ""
'Fill dotwListBox
With dotwListBox
.AddItem "Monday"
.AddItem "Tuesday"
.AddItem "Wednesday"
.AddItem "Thursday"
.AddItem "Friday"
End With
'Empty apiphbTextBox
aphiphb.Value = "1"
'Empty apiturbiditybTextBox
apiturbidityb.Value = ""
'Empty apitocbTextBox
apitocb.Value = ""
'Empty apicodbTextBox
apicodb.Value = ""
'Empty apibodbTextBox
apibodb.Value = ""
'Empty longbaydobTextBox
longbaydob.Value = ""
'Empty asudobTextBox
asudob.Value = ""
'Empty rasmlssbTextBox
rasmlssb.Value = ""
'Empty clarifierturbiditybTextBox
clarifierturbidityb.Value = ""
'Empty clarifierphbTextBox
clarifierphb.Value = ""
'Empty clarifiernh3bTextBox
clarifiernh3b.Value = ""
'Empty clarifierno3bTextBox
clarifierno3b.Value = ""
'Empty clarifierenterococcibTextBox
clarifierenterococcib.Value = ""
'Empty clarifierphosphorusTextBox
clarifierphosphorusb.Value = ""
End Sub

There can be two reasons:
Your ListBox MultiSelect property is set to 1 (fmMultiSelectMulti) or 2 (fmMultiSelectExtented)
In this case its Value property will be always Null
Your ListBox has no item selected
even if its MultiSelect property is set to 0 (fmMultiSelectSingle) its Value property will return Null if no item is selected
In this case set a check with its ListIndex property, like follows
If dotwListBox.ListIndex <> -1 Then Cells(emptyRow, 1).Value = dotwListBox.Value
Since -1 is the value returned by ListIndex property when no item is selected

Related

Sub not found in VBA

I m trying to create a button and fill the value for column on.5 based on the value in column no.4.
here is the code:
Private Sub CommandButton1_Click()
Dim x As Integer
For x = 3 To 1000
If Cells(x, 4).Value = "Regular" Then
Cells(x, 5).Value = Today()
ElseIf Cells(x, 4).Value = "Temporary" And Cells(x, 5).Value = "" Then
Cells(x, 5).Value = "To be assigned"
Else
Cells(x, 5).Value = ""
End If
End Sub
This gives me the errors that sub or function is not defined.
I am new to VBA and trying to solve this, but could not do it.
Thanks.
VBA is ignoring the End Sub, because you haven't finished closing all of the clauses before it:
Private Sub CommandButton1_Click()
Dim x As Integer
For x = 3 To 1000
If Cells(x, 4).Value = "Regular" Then
Cells(x, 5).Value = Today() 'This should be Now()
ElseIf Cells(x, 4).Value = "Temporary" And Cells(x, 5).Value = "" Then
Cells(x, 5).Value = "To be assigned"
Else
Cells(x, 5).Value = ""
End If
'You are missing a "Next x" here
End Sub
Since the Sub is not complete, it does not count it as valid, so it cannot find it at compile time.

Insert userform data into next column on respective rows

I want to add the values from a form to an Excel sheet.
I have the "headers" in column A from A1 down to A20 with A21 a "score" field that runs from B21 to CZ21 that automatically calculates a score based on the values entered above in each respective column.
A1 through 20 has the headers for the questions and I want the values from the form entered initially in B1 through 20 and then C1-20 and so on and so forth.
As an example, the first form response should be entered into rows B1 to B20 with each row having a value. The second form response will be entered into rows C1 to C20 with each row having it's own value.
Column A is a frozen pane.
Private Sub SaveButton_Click()
'Make Sheet2 active
Sheet4.Activate
'Determine empty Row
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
Cells(emptyRow, 2).Value = TextBox1.Value
Cells(emptyRow, 3).Value = Format(Now)
Cells(emptyRow, 4).Value = TextBox3.Value
If CheckBox1.Value = True Then
Cells(emptyRow, 5).Value = CheckBox1
Else
Cells(emptyRow, 5).Value = CheckBox1
End If
If CheckBox2.Value = True Then
Cells(emptyRow, 6).Value = CheckBox2
Else
Cells(emptyRow, 6).Value = CheckBox2
End If
If CheckBox3.Value = True Then
Cells(emptyRow, 7).Value = CheckBox3
Else
Cells(emptyRow, 7).Value = CheckBox3
End If
If CheckBox4.Value = True Then
Cells(emptyRow, 8).Value = CheckBox4
Else
Cells(emptyRow, 8).Value = CheckBox4
End If
If CheckBox9.Value = True Then
Cells(emptyRow, 9).Value = CheckBox9
Else
Cells(emptyRow, 9).Value = CheckBox9
End If
If CheckBox11.Value = True Then
Cells(emptyRow, 10).Value = CheckBox11
Else
Cells(emptyRow, 10).Value = CheckBox11
End If
If CheckBox14.Value = True Then
Cells(emptyRow, 11).Value = CheckBox14
Else
Cells(emptyRow, 11).Value = CheckBox14
End If
If CheckBox16.Value = True Then
Cells(emptyRow, 12).Value = CheckBox16
Else
Cells(emptyRow, 12).Value = CheckBox16
End If
If CheckBox18.Value = True Then
Cells(emptyRow, 13).Value = CheckBox18
Else
Cells(emptyRow, 13).Value = CheckBox18
End If
If CheckBox20.Value = True Then
Cells(emptyRow, 14).Value = CheckBox20
Else
Cells(emptyRow, 14).Value = CheckBox20
End If
If CheckBox22.Value = True Then
Cells(emptyRow, 15).Value = CheckBox22
Else
Cells(emptyRow, 15).Value = CheckBox22
End If
If CheckBox24.Value = True Then
Cells(emptyRow, 16).Value = CheckBox24
Else
Cells(emptyRow, 16).Value = CheckBox24
End If
If CheckBox26.Value = True Then
Cells(emptyRow, 17).Value = CheckBox26
Else
Cells(emptyRow, 17).Value = CheckBox26
End If
If CheckBox27.Value = True Then
Cells(emptyRow, 18).Value = CheckBox27
Else
Cells(emptyRow, 18).Value = CheckBox27
End If
If CheckBox28.Value = True Then
Cells(emptyRow, 19).Value = CheckBox28
Else
Cells(emptyRow, 19).Value = CheckBox28
End If
Cells(emptyRow, 20).Value = TextBox5.Value
Cells(emptyRow, 21).Value = TextBox4.Value
'Clearing data
CheckBox1.Value = "False"
CheckBox2.Value = "False"
CheckBox3.Value = "False"
CheckBox4.Value = "False"
CheckBox9.Value = "False"
CheckBox11.Value = "False"
CheckBox14.Value = "False"
CheckBox16.Value = "False"
CheckBox18.Value = "False"
CheckBox20.Value = "False"
CheckBox22.Value = "False"
CheckBox24.Value = "False"
CheckBox26.Value = "False"
CheckBox27.Value = "False"
CheckBox28.Value = "False"
TextBox1.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""
TextBox5.Value = ""
End Sub
You can do something like this
Private Sub SaveButton_Click()
Dim col as Range
Set col = Sheet4.Range("B1:B20") 'first potential location
'find first unused column
Do While Application.CountA(col) > 0
Set col = col.Offset(0, 1)
Loop
col.cells(1).Value = TextBox1.Value
col.cells(2).Value = Format(Now)
col.cells(3).Value = TextBox3.Value
'etc etc

How to make a multi-select listbox selection required in a userform?

I have a userform with a multi-select list box.
The code I wrote expects the user to select two options.
I need that the user gets an error message only if they don't select at least one option.
I started with the code for a single-select list box error message. I tried making it >= -1 <0 <>-1 and none of them allow the user to select just one option.
The line in question is:
If ListBoxNextSteps.ListIndex <> 1 Then
MsgBox "Please select next steps"
Exit Sub
End If
Private Sub CommandButtonSubmit_Click()
'Requires specific fields to be complete before user can submit
If ComboBoxDBN = "" Then
MsgBox "Please select a DBN"
Exit Sub
End If
If TextBoxDate = "" Then
MsgBox "Plese enter a date"
Exit Sub
End If
If CheckBoxCohort = False Then
If TextBoxContactName = "" Then
MsgBox "Please list school officials that you contacted"
Exit Sub
End If
If ListBoxSupportType.ListIndex = -1 Then
MsgBox "Please select a support type"
Exit Sub
End If
If TextBoxDiscussion = "" Then
MsgBox "Please describe your discussion points"
Exit Sub
End If
If TextBoxLearn = "" Then
MsgBox "Please describe what you learned about the school's challenges"
Exit Sub
End If
If (CheckBoxAdminDiff + CheckBoxConflict + CheckBoxShortage + CheckBoxDataSystems + CheckBoxOther) = 0 Then
MsgBox "Please select at least one bucket"
Exit Sub
End If
If (CheckBoxOther = True And TextBoxIfOther = "") Then
MsgBox "Please describe other bucket"
Exit Sub
End If
If ListBoxNextSteps.ListIndex <> 1 Then
MsgBox "Please select next steps"
Exit Sub
End If
If ListBoxResolution.ListIndex = -1 Then
MsgBox "Please select a resolution status"
Exit Sub
End If
End If
'tells form to put responses in a long table
Dim emptyRow As Long
'Make Sheet1 (SchoolSupport) active so it knows where to put the responses
Sheet1.Activate
ActiveSheet.Protect UserInterfaceOnly:=True, AllowFiltering:=True
'Determine emptyRow so it knows where the next entry goes
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
'Transfer information from form fields to table cells
Cells(emptyRow, 1).Value = ComboBoxDBN.Value
Cells(emptyRow, 2).Value = TextBoxDate.Value
Cells(emptyRow, 3).Value = TextBoxContactName.Value
Cells(emptyRow, 6).Value = ListBoxSupportType.Value
Cells(emptyRow, 7).Value = TextBoxDiscussion.Value
Cells(emptyRow, 8).Value = TextBoxBestPractice.Value
Cells(emptyRow, 9).Value = TextBoxLearn.Value
Cells(emptyRow, 15).Value = TextBoxIfOther.Value
'Makes sure multiple selections appear in same cell
If CheckBoxCohort = False Then
Dim s As String, i As Integer
With ListBoxNextSteps
For i = 0 To .ListCount - 1
If .Selected(i) = True Then s = s & .List(i) & ", "
Next i
End With
With Cells(emptyRow, 16)
.Value = Left(s, Len(s) - 1)
End With
End If
Cells(emptyRow, 17).Value = ListBoxResolution.Value
Cells(emptyRow, 18).Value = TextBoxEscalateTo.Value
Cells(emptyRow, 19).Value = ListBoxEscalateLocation.Value
Cells(emptyRow, 20).Value = ListBoxEscalateStatus.Value
Cells(emptyRow, 21).Value = TextBoxPertinentNotes.Value
If CheckBoxUnresponsive.Value = True Then Cells(emptyRow, 4).Value = "Y"
If CheckBoxCohort.Value = True Then Cells(emptyRow, 5).Value = "Y"
If CheckBoxAdminDiff.Value = True Then Cells(emptyRow, 10).Value = "Y"
If CheckBoxConflict.Value = True Then Cells(emptyRow, 11).Value = "Y"
If CheckBoxShortage.Value = True Then Cells(emptyRow, 12).Value = "Y"
If CheckBoxDataSystems.Value = True Then Cells(emptyRow, 13).Value = "Y"
If CheckBoxOther.Value = True Then Cells(emptyRow, 14).Value = "Y"
'Saves workbook
Application.ActiveWorkbook.Save
Unload SchoolSupportForm
End Sub
I expect the ListBoxNextSteps error message to appear only if the user hasn't made any selection, but instead, it appears if the user hasn't made at least 2 selections.
You can use the Selected method of the ListBox object to check whether an item is selected. The following code loops through each item in the listbox. If an item is selected, the boolean variable itemSelected is set to True and the For/Next loop is exited. After exiting the loop, if itemSelected is set to False, the message is displayed and then it exits the sub.
Dim itemSelected As Boolean
Dim i As Long
itemSelected = False
With Me.ListBoxNextSteps
For i = 0 To .ListCount - 1
If .Selected(i) Then
itemSelected = True
Exit For
End If
Next i
End With
If Not itemSelected Then
MsgBox "Please select next steps"
Exit Sub
End If

When pressing the OK Button for my EXcel VBA Form the info does not get added to the data table correctly

I am trying to format an 'Ok' button so that all the information in my form fills in the next empty row in my data table. Here is what I have so far:
Private Sub OKButton_Click()
Dim emptyRow As Long
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
'Transfer Information
Cells(emptyRow, 1).Value = BoatModelComboBox.Value
Cells(emptyRow, 2).Value = BoatIDTextBox.Value
Cells(emptyRow, 3).Value = NameofDefectComboBox.Value
Cells(emptyRow, 4).Value = InspectionAreaComboBox.Value
Cells(emptyRow, 5).Value = DateTextBox.Value
Cells(emptyRow, 6).Value = OccurenceTextBox.Value
Cells(emptyRow, 7).Value = DefectOriginComboBox.Value
If BoatCheckOptionButton1.Value = True Then
Cells(emptyRow, 8).Value = "TRUE"
Else
Cells(emptyRow, 8).Value = "FALSE"
End If
If TireKickOptionButton1.Value = True Then
Cells(emptyRow, 9).Value = "TRUE"
Else
Cells(emptyRow, 9).Value = "FALSE"
End If
Cells(emptyRow, 10).Value = TypeofInspectionComboBox.Value
Cells(emptyRow, 11).Value = MonthComboBox.Value
thanks for the help!
Try getting the empty row like this instead
emptyRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row + 1

How To: Excel VBA Command Button Enters UserForm Values to Current Row?

I currently have a UserForm "UserForm1" that takes values (Lesson, Instructor, Start Time, etc.) from ComboBoxes and saves them to a designated sheet "InstructorHours". The CommandButton "Save" saves the selected values in the next available line on the "InstructorHours" sheet. What I'm having trouble with is having the CommandButton save the same information into a currently selected row on another Worksheet "Calendar". Ideally I would like to click within a row on my "Calendar" sheet and input the values from the User from into the row I've selected.
Private Sub CommandButton1_Click()
'Copy input values to sheet.
Dim lRow As Long
'next available row
Dim ws As Worksheet
Set ws = Worksheets("InstructorHours")
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
.Cells(lRow, 1).Value = Me.ComboBox1.Value
.Cells(lRow, 2).Value = Me.ComboBox2.Value
.Cells(lRow, 3).Value = Me.ComboBox3.Value
.Cells(lRow, 4).Value = Me.ComboBox4.Value
.Cells(lRow, 5).Value = Me.ComboBox5.Value
.Cells(lRow, 6).Value = Me.ComboBox6.Value
.Cells(lRow, 7).Value = Me.TextBox1.Value
End With
'Clear input controls.
Me.ComboBox1.Value = ""
Me.ComboBox2.Value = ""
Me.ComboBox3.Value = ""
Me.ComboBox4.Value = ""
Me.ComboBox5.Value = ""
Me.ComboBox6.Value = ""
End Sub
Any and all help is much appreciated. Thank you.
In general, if you want to work on something active, you would want something "Active" (e.g. ActiveSheet or ActiveCell) for your active-selection activities.
I would recommend another Command Button for the following (Writing this up off the top of my head, haven't verified the code):
Private Sub CommandButton2_Click()
'Input based on the selection
ActiveCell.Value = Me.ComboBox1.Value
ActiveCell.Offset(1,0).Value = Me.ComboBox2.Value
ActiveCell.Offset(2,0).Value = Me.ComboBox3.Value
ActiveCell.Offset(3,0).Value = Me.ComboBox4.Value
ActiveCell.Offset(4,0).Value = Me.ComboBox5.Value
ActiveCell.Offset(5,0).Value = Me.ComboBox6.Value
ActiveCell.Offset(6,0).Value = Me.TextBox1.Value
'Clear input controls.
Me.ComboBox1.Value = ""
Me.ComboBox2.Value = ""
Me.ComboBox3.Value = ""
Me.ComboBox4.Value = ""
Me.ComboBox5.Value = ""
Me.ComboBox6.Value = ""
End Sub
That should provide, at least, a starting point.

Resources