UserForm with Combo Boxes

michaeltsmith93

Board Regular
Joined
Sep 29, 2016
Messages
83
Hi,

I've created my first userform, and there's something wrong, but I'm not sure what. Please let me know if you need more info than the code that's here.

Here is everything in my VBA Editor. I'd also appreciate it if someone could tell me how to put a box of code in here rather than plain text.

Private Sub CommandButton1_Click()


End Sub


Private Sub Cancel_Click()
Unload Me
End Sub


Private Sub Label3_Click()


End Sub


Private Sub Name_Change()


End Sub


Private Sub OOK_Click()


End Sub


Private Sub OK_Click()
Dim ERow As Long
Dim ws As Worksheet
Set ws = Worksheets("PartsData")


'find first empty row
ERow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

'check for input
If Trim(Me.Name.Value) Or Trim(Me.StudyRole.Value) Or Trim(Me.MatrixRole.Value) Or Trim(Me.Department.Value) = "" Then
MsgBox "Please fill all fields."
Exit Sub
End If


With ws


.Cells(ERow, 1).Value = Me.Name.Value
.Cells(ERow, 2).Value = Me.StudyRole.Value
.Cells(ERow, 4).Value = Me.MatrixRole.Value
.Cells(ERow, 5).Value = Me.Department.Value

End With


Me.Name.Value = ""
Me.StudyRole.Value = ""
Me.MatrixRole.Value = ""
Me.Department.Value = ""


End Sub


Private Sub UserForm_Click()


End Sub


Private Sub UserForm_Initialize()


Dim MRole As Range
Dim Dept As Range


For Each MRole In Range("MatrixRoles")
With Me.MatrixRole
.AddItem MRole.Value
End With
Next MRole


For Each Dept In Range("Depts")
With Me.Department
.AddItem Dept.Value
End With
Next Dept


End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Thank you. Here it is again, but prettier.

Code:
Private Sub CommandButton1_Click()


End Sub


Private Sub Cancel_Click()
    Unload Me
End Sub


Private Sub Label3_Click()


End Sub


Private Sub Name_Change()


End Sub


Private Sub OOK_Click()


End Sub


Private Sub OK_Click()
Dim ERow As Long
Dim ws As Worksheet
Set ws = Worksheets("PartsData")


'find first empty row
ERow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
    
'check for input
If Trim(Me.Name.Value) Or Trim(Me.StudyRole.Value) Or Trim(Me.MatrixRole.Value) Or Trim(Me.Department.Value) = "" Then
    MsgBox "Please fill all fields."
    Exit Sub
End If


With ws


    .Cells(ERow, 1).Value = Me.Name.Value
    .Cells(ERow, 2).Value = Me.StudyRole.Value
    .Cells(ERow, 4).Value = Me.MatrixRole.Value
    .Cells(ERow, 5).Value = Me.Department.Value
    
End With


Me.Name.Value = ""
Me.StudyRole.Value = ""
Me.MatrixRole.Value = ""
Me.Department.Value = ""


End Sub


Private Sub UserForm_Click()


End Sub


Private Sub UserForm_Initialize()


Dim MRole As Range
Dim Dept As Range


For Each MRole In Range("MatrixRoles")
    With Me.MatrixRole
        .AddItem MRole.Value
    End With
Next MRole


For Each Dept In Range("Depts")
    With Me.Department
        .AddItem Dept.Value
    End With
Next Dept


End Sub
 
Upvote 0
It indicates a compile error, and closing that window takes me to the debugger. It highlights "Private Sub OK_Click()" in yellow and ".Name" in "If Trim(Me.Name.Value) in blue.
 
Upvote 0
I would recommend changing the name of that control to something else, as "Name" is a keyword & the compiler could be getting confused.
 
Upvote 0
I wondered about that and forgot to look it up. That seems to have corrected one error. I now have another. I'll make a separate post, as per your suggestion earlier, Flump. Thank you for your continued help.
 
Upvote 0
If it's still in connection with this code, you can post it here.
You only need to start a new thread if it's a totally different question
 
Upvote 0
As you have already started a new thread, then you'll need to continue in that.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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