I am creating by code a userform that contain labels, textboxes and comboboxes per lines (see picture below). The number of line is variable (so is the number of comboboxes). I also create 3 buttons: OK, Cancel, Add. "Cancel" unload the form, "Add" opens another Userform to update the source of the comboboxes and "OK" put the infos, the user selected in the combobox, into a cell in the worksheet. All the buttons Call another Sub to execute:
With Userface.codemodule
.insertlines 1, "end sub"
.insertlines 1, "call Cansub"
.insertlines 1, "Private Sub Canbt_Click()"
End With
'Validate and register
With Userface.codemodule
.insertlines 1, "end sub"
.insertlines 1, "Call Oksub"
.insertlines 1, ""
.insertlines 1, "Public Sub Okbt_Click()"
End With
'add another category and/or reason
With Userface.codemodule
.insertlines 1, "end sub"
.insertlines 1, "call Addsub"
.insertlines 1, "Private Sub Addbt_Click()"
End With
Application.VBE.MainWindow.Visible = True
VBA.UserForms.Add(Userface.Name).Show
Everything is working but the "OK" button.
I don't want to use with userface.codemodule.insertlines 1,""
To write the whole thing. that is why I am calling a sub.
Here is the Public OKsub() the button calls:
Dim Addentr As Range
Dim Line As Long
Dim Cbtxt As String
Line = 0
Line = Range(Range("J1").End(xlDown).Offset(1, 0), Range("A1").End(xlDown)).Rows.Count
Set Addentr = ActiveWorkbook.Sheets("Combined").Range(Range("J1").End(xlDown).Offset(1, 0), Range("A1").End(xlDown))
For i = 0 To Line - 1
Cbtxt = UserForm1.Controls("Cbo_A" & i).Value
MsgBox "Combobox CBo_A" & i & " = " & Cbtxt
If IsEmpty(Cbtxt) = False Then
Addentr.Cells(i + 1, 10).Value = Cbtxt
End If
Cbtxt = UserForm1.Controls("Cbo_B" & i).Text
If IsEmpty(Cbtxt) = False Then
Addentr.Cells(i + 1, 11).Value = Cbtxt
End If
Next i
MsgBox "Entries fulfilled"
The code runs without error BUT the "Cbtxt" value is empty as the msgbox shows here Most of the "solutions" I found speak of doing this once by giving the value to a public variable before moving to the worksheet. But, in my case, the number of input is changing so I have to include that into a loop. I cannot dedicated one variable per combobox (or can I?... or not).
I hope you will understand me guys. Thanks for the support!
If isempty()= false then...
and it is appearing. that prove teh Cbtxt isn't empty but the variable is likeCbtxt = " "
– MCircular