Sub procedures in user form

mahhdy

Board Regular
Joined
Sep 15, 2016
Messages
86
Hi, I have a user form with several combo boxes which read their data from an excel worksheet.
I added an if function for checking required field data entry, which shows a text message if you miss filling required data. the problem is if you forget to fill all required fields, and press the "OK" button of msg box, the combo items will not show up in the from anymore. (Actually just one of them will show its items!)
I can repeat the code of add.item after if clause, but for avoiding duplication, is there is any way for putting all of the combo add.items scripts in a specific module or sub procedure, so I just call them, when it needed, it will enable me to have shorter and more efficient code.
Can anyone help me in that regard? I know it is a basic thing.
Tnx by the way.
 
Last edited:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi,
I figured it.
it was just:
Call addcmbo ' addcmbo is the name of my procedure.
Now funny thing is the items of that field which had not problem, duplicates. I will fix it and let you guys know.
here is my code:
Sub addcmbo()
Application.ScreenUpdating = False
Sheet2.Activate
' A/C data update
For i = 2 To Sheets("Ref").Cells(1, 2).SpecialCells(xlLastCell).Row
If Sheets("Ref").Cells(i, 2) <> "" Then
RCVNG.cmbac.AddItem (Cells(i, 2))
End If
Next
' User data update ##(the field with diffrent behaviour)##
For i = 2 To Sheets("Ref").Cells(1, 3).SpecialCells(xlLastCell).Row
If Sheets("Ref").Cells(i, 3) <> "" Then
RCVNG.cmbuser.AddItem (Cells(i, 3))
End If
Next

' W/B data update
For i = 2 To Sheets("Ref").Cells(1, 8).SpecialCells(xlLastCell).Row
If Sheets("Ref").Cells(i, 8) <> "" Then
RCVNG.Cmbwb.AddItem (Cells(i, 8))
End If
Next
' w/C data update
For i = 2 To Sheets("Ref").Cells(1, 9).SpecialCells(xlLastCell).Row
If Sheets("Ref").Cells(i, 9) <> "" Then
RCVNG.Cmbwc.AddItem (Cells(i, 9))
End If
Next
' P/# data update
For i = 2 To Sheets("Ref").Cells(1, 10).SpecialCells(xlLastCell).Row
If Sheets("Ref").Cells(i, 10) <> "" Then
RCVNG.Cmbpn.AddItem (Cells(i, 10))
End If
Next
Sheet1.Activate
Application.ScreenUpdating = True
End Sub
'-----------------
Private Sub btnadd_Click()
Dim oNewRow As ListRow
' Form fields Clear
cmbac.Clear
Cmbwb.Clear
Cmbwc.Clear
Cmbpn.Clear
cmbac.Value = ""
Cmbwb.Value = ""
Cmbwc.Value = ""
Cmbpn.Value = ""
Txtqty.Value = ""
txtSN.Value = ""
Txtloc.Value = ""
cmnt.Value = ""

' Data entry sufficiency check
If cmbuser = "" And Cmbpn = "" Then
MsgBox ("Please Fill Required Fields before Save!")
Call addcmbo
Else:
' Data Entry
Sheet1.Activate
ActiveSheet.Cells(1, 3).Select
Set oNewRow = Selection.ListObject.ListRows.Add(AlwaysInsert:=True)
oNewRow.Range.Cells(1, 6).Value = DTPicker.Value
oNewRow.Range.Cells(1, 7).Value = txtrn.Value
oNewRow.Range.Cells(1, 14).Value = cmbuser.Value
oNewRow.Range.Cells(1, 8).Value = cmbac.Value
oNewRow.Range.Cells(1, 10).Value = Cmbwb.Value
oNewRow.Range.Cells(1, 9).Value = Cmbwc.Value
oNewRow.Range.Cells(1, 11).Value = Cmbpn.Value
oNewRow.Range.Cells(1, 12).Value = Txtqty.Value
oNewRow.Range.Cells(1, 13).Value = txtSN.Value
oNewRow.Range.Cells(1, 15).Value = Txtloc.Value
oNewRow.Range.Cells(1, 16).Value = cmnt.Value

End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,203,171
Messages
6,053,875
Members
444,692
Latest member
Queendom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top