Insert userform data into next column on respective rows - excel

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

Related

Extracting Data from String Cell; Type mismatch for integer

I wrote the following code to determine cable size and type from one cell then use that information to display a number into another cell:
Dim rowNum3 As Integer, Num_of_string As Integer, cablesize1 As String, firstNum As Integer
Num_of_string = 4
For rowNum3 = 4 To 50
cablesize1 = Cells(rowNum3, 9).Value
firstNum = Left(cablesize1, Num_of_string)
If cablesize1 Like "*AL*" = True Then
If cablesize1 Like "*1/0*" = True Then
Cells(rowNum3, 10).Value = Worksheets("Tables").Cells(32, 7).Value
ElseIf cablesize1 Like "*2/0*" = True Or cablesize1 Like "*3/0*" = True Then
Cells(rowNum3, 10).Value = Worksheets("Tables").Cells(33, 7).Value
ElseIf cablesize1 Like "*4/0*" = True Or cablesize1 Like "*250*" = True Then
Cells(rowNum3, 10).Value = Worksheets("Tables").Cells(34, 7).Value
ElseIf Application.Intersect(CInt(Left(cablesize1, Num_of_string)), Range(Cells(35, 4).Value, Cells(35, 5).Value)) = True Then
Cells(rowNum3, 10).Value = Worksheets("Tables").Cells(35, 7).Value
ElseIf Application.Intersect(CInt(Left(cablesize1, Num_of_string)), Range(Cells(36, 4).Value, Cells(36, 5).Value)) = True Then
Cells(rowNum3, 10).Value = Worksheets("Tables").Cells(36, 7).Value
ElseIf Application.Intersect(CInt(Left(cablesize1, Num_of_string)), Range(Cells(37, 4).Value, Cells(37, 5).Value)) = True Then
Cells(rowNum3, 10).Value = Worksheets("Tables").Cells(37, 7).Value
ElseIf Application.Intersect(CInt(Left(cablesize1, Num_of_string)), Range(Cells(38, 4).Value, Cells(38, 5).Value)) = True Then
Cells(rowNum3, 10).Value = Worksheets("Tables").Cells(38, 7).Value
End If
ElseIf cablesize1 Like "*AL*" = False Then
If cablesize1 Like "*2 *" = True Then
Cells(rowNum3, 10).Value = Worksheets("Tables").Cells(32, 6).Value
ElseIf cablesize1 Like "*1 *" = True Or cablesize1 Like "*1/0*" = True Then
Cells(rowNum3, 10).Value = Worksheets("Tables").Cells(33, 6).Value
ElseIf cablesize1 Like "*2/0*" = True Or cablesize1 Like "*3/0*" = True Then
Cells(rowNum3, 10).Value = Worksheets("Tables").Cells(34, 6).Value
ElseIf cablesize1 Like "* 350 *" = True Then
Cells(rowNum3, 10).Value = Worksheets("Tables").Cells(35, 6).Value
ElseIf Application.Intersect(CInt(Left(cablesize1, Num_of_string)), Range(Cells(36, 2).Value, Cells(36, 3).Value)) = True Then
Cells(rowNum3, 10).Value = Worksheets("Tables").Cells(36, 6).Value
ElseIf Application.Intersect(CInt(Left(cablesize1, Num_of_string)), Range(Cells(37, 2).Value, Cells(37, 3).Value)) = True Then
Cells(rowNum3, 10).Value = Worksheets("Tables").Cells(37, 6).Value
ElseIf Application.Intersect(CInt(Left(cablesize1, Num_of_string)), Range(Cells(38, 2).Value, Cells(38, 3).Value)) = True Then
Cells(rowNum3, 10).Value = Worksheets("Tables").Cells(38, 6).Value
End If
Else
Cells(rowNum3, 10).Value = "NA"
End If
Next rowNum3
I'm getting an error here:
ElseIf Application.Intersect(CInt(Left(cablesize1, Num_of_string)), Range(Cells(35, 4).Value, Cells(35, 5).Value)) = True Then
There is a type mismatch on Cint I tried different ways to convert the string to a number but it wont work
Sample cell where I get the string from (I use 'AL' at the end to determine whether it is copper or aluminum) the number in the beginning is what i try to convert
table where I get info from

Invalid Property Value error for Userform

I have tried going through multiple threads to find an answer that I need but not able to sort it out yet.
I have certain combo boxes in my userform and I have a button to upload the details. Once the details are uploaded in my excel sheet, I want the form to clear all the contents and reset to blanks. The data is getting updated perfectly in the excel file, however each time I get an error stating Invalid Property Value. I want to be able to upload the details without getting the error. I have tried setting the combo box style to list, however, it still gives me the same error
Also if the user selects a value in the combo box and later deletes it then the same message is again popped up and the user can't move to another filed until he selects a value from the list. I want the user to be able to delete the entry or select only from the list (that's why I have the match required set to True).
Can someone please guide
PFB the code :
Private Sub CmdUploadDatabaseDetails_Click()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wb = Workbooks.Open("C:\Users\anup.patil\Desktop\Dashboard
Testing.xlsx")
wb.Activate
IsEntryBlank = CheckIfBlanksDatabase
If IsEntryBlank = True Then
MsgBox "Please fill all the mandatory details"
wb.Close False
cwb.Activate
Me.CBMonth.SetFocus
Exit Sub
End If
Sheets("Database").Select
Range("B1").End(xlDown).Select
Selection.Copy
ActiveCell.Offset(1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Lastrow = Sheets("Database").Cells(Rows.Count, "A").End(xlUp).Row
Cells(Lastrow + 1, 1).Select
ActiveCell.Value = Me.CBMonth.Value
ActiveCell.Offset(, 2).Value = Me.TBParentCo.Text
ActiveCell.Offset(, 3).Value = Me.TBSubsidaryCo.Text
ActiveCell.Offset(, 4).Value = Me.CBCustomerCat.Text
ActiveCell.Offset(, 5).Value = Me.TBContactName.Text
ActiveCell.Offset(, 6).Value = Me.TBDesignation.Text
ActiveCell.Offset(, 7).Value = Me.TBDept.Text
ActiveCell.Offset(, 8).Value = Me.CBVertical.Text
ActiveCell.Offset(, 9).Value = Me.CBSubVertical.Text
ActiveCell.Offset(, 10).Value = Me.TBOperatingLoc.Text
ActiveCell.Offset(, 11).Value = Me.TBNearbyHKVBr.Text
ActiveCell.Offset(, 12).Value = Me.TBOperatingLocAddr.Text
ActiveCell.Offset(, 13).Value = Me.CBOperatingLocState.Text
ActiveCell.Offset(, 15).Value = Me.CBDecisionMakingUnit.Text
ActiveCell.Offset(, 16).Value = Me.TBHOCentralized.Text
ActiveCell.Offset(, 17).Value = Me.TBMobileNo.Text
ActiveCell.Offset(, 18).Value = Me.TBPhoneNo.Text
ActiveCell.Offset(, 19).Value = Me.TBEmail.Text
ActiveCell.Offset(, 20).Value = Me.CBRelationshipBuild.Text
ActiveCell.Offset(, 21).Value = Me.TBMemberOfAssoc.Text
ActiveCell.Offset(, 22).Value = Me.TBListOfEmpanelled.Text
ActiveCell.Offset(, 23).Value = Me.CBGiftAllowed.Text
ActiveCell.Offset(, 24).Value = Me.CBGiftDeliveryMode.Text
ActiveCell.Offset(, 25).Value = Me.TBSurvPotential.Text
Me.CBMonth.ListIndex = -1
Me.CBMonth.Value = ""
Me.TBParentCo.Value = ""
Me.TBSubsidaryCo.Value = ""
Me.CBCustomerCat.Value = ""
Me.TBContactName.Value = ""
Me.TBDesignation.Value = ""
Me.TBDept.Value = ""
Me.CBVertical.Value = ""
Me.CBSubVertical.Value = ""
Me.TBOperatingLoc.Value = ""
Me.TBNearbyHKVBr.Value = ""
Me.TBOperatingLocAddr.Value = ""
Me.CBOperatingLocState.Value = ""
Me.CBDecisionMakingUnit.Value = ""
Me.TBHOCentralized.Value = ""
Me.TBMobileNo.Value = ""
Me.TBPhoneNo.Value = ""
Me.TBEmail.Value = ""
Me.CBRelationshipBuild.Value = ""
Me.TBMemberOfAssoc.Value = ""
Me.TBListOfEmpanelled.Value = ""
Me.CBGiftAllowed.Value = ""
Me.CBGiftDeliveryMode.Value = ""
Me.TBSurvPotential.Value = ""
Me.CBMonth.SetFocus
wb.Close True
MsgBox "Details uploaded successfully"
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Set MatchingRequired to False for the combobox.

Excel VBA - Runtime error 438 : Object doesn't support this property or method

I'm new to VBA and I have the following code.
I want to sum in the variable SumDef the values of all the textboxes in a frame of a userform, if the textbox is not void.
But I get
runtime error 438 : Object doesn't support this property or method
Private Sub CommandButton2_Click()
'PROBLEEEEEM
Dim SumDef As Integer
Dim txt As Control
SumDef = 0
For Each txt In Me.Frame3.Controls
If TypeName(txt) = "TextBox" And IsNumeric(txt.Value) = True Then
SumDef = SumDef + CInt(txt.Text)
End If
Next txt
If TextBox2.Value <> vbNullString And CInt(TextBox2.Text) <= SumDef Then
cell2.Offset(, 7).Value = TextBox2.Text
cell2.Offset(, 19).Value = TextBox3.Text
cell2.Offset(, 8).Value = TextBox4.Text
cell2.Offset(, 9).Value = TextBox5.Text
cell2.Offset(, 10).Value = TextBox6.Text
cell2.Offset(, 11).Value = TextBox7.Text
cell2.Offset(, 12).Value = TextBox8.Text
cell2.Offset(, 13).Value = TextBox9.Text
cell2.Offset(, 14).Value = TextBox10.Text
cell2.Offset(, 15).Value = TextBox11.Text
cell2.Offset(, 16).Value = TextBox12.Text
cell2.Offset(, 17).Value = TextBox13.Text
cell2.Offset(, 18).Value = TextBox14.Text
MsgBox "Bac enregistré avec succes", vbInformation, "ENR FAB 07"
Me.CommandButton3.Enabled = True
Me.CommandButton2.Enabled = False
Else
MsgBox "Nombre de gants défauts est invalide", vbExclamation, "ENR FAB 07"
TextBox2.Value = vbNullString
End If
End Sub
Nest your If statement like this instead as it's trying to evaluate the .Value on all controls otherwise, and not all controls have a .Value property:
If TypeName(txt) = "TextBox" Then
If IsNumeric(txt.Value) = True Then
SumDef = SumDef + CInt(txt.Text)
End If
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

ListBox not populating

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

Resources