VBA Userform TextBox value dependant on ComboBox selection

anteus

New Member
Joined
Jun 29, 2017
Messages
9
Dear All,
I am trying to run a userform where a TextBox value is based on the selection of a ComboBox, i.e. the user would select a service and theform would bring up a predefined price from a table with vlookup. The codes currently look like this:

Private Sub CboService_Change()
With Me
.TextRev = Application.VLookup(Me.CboService.Value, Worksheets("Param").Range("Services"), 2, 0)
End With
End Sub

and

Private Sub CommandButton1_Click()


Dim lRow As Long
Dim lEmployee As String
Dim lCustomer As String
Dim lTime As String
Dim lService As Long
Dim lServ2 As String
Dim lProduct As String
Dim lQuant As Integer
Dim lRev As Integer
Dim lPay As String
Dim ws As Worksheet
Set ws = Worksheets(2)


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


lEmployee = Me.CboUser.ListIndex
m1 = Application.VLookup("m1", Worksheets("Param").Range("SysM"), 2, 0)


'check for a valid Employee entry
If Me.CboUser.Value = "" Then
MsgBox m1
Exit Sub
End If


lCustomer = Me.TextGuest.Value
lTime = Me.TextTime.Value
lService = Me.CboService.ListIndex
lProduct = Me.CboProduct.ListIndex
'check for separate product line


m8 = Application.VLookup("m8", Worksheets("Param").Range("SysM"), 2, 0)
If Me.CboService.Value <> "" And Me.CboProduct.Value <> "" Then
MsgBox m8
Exit Sub
End If


lQuant = Me.TextQuant.Value


lRev = Me.TextRev.Value
'check for Revenue entry
m9 = Application.VLookup("m9", Worksheets("Param").Range("SysM"), 2, 0)
If lRev = 0 Then
MsgBox m9
Exit Sub
End If


lPay = Me.CboPymnt.Value
'check for a valid Payment mode entry
m3 = Application.VLookup("m3", Worksheets("Param").Range("SysM"), 2, 0)
If lPay = "" Then
MsgBox m3
Exit Sub
End If


'copy the data to the database
'use protect and unprotect lines,
' with your password
' if worksheet is protected
With ws
' .Unprotect Password:="password"
.Cells(lRow, 2).Value = Me.CboUser.Value
.Cells(lRow, 3).Value = lCustomer
.Cells(lRow, 4).Value = Format(Date, "yyyy.mm.dd")
.Cells(lRow, 5).Value = lTime
.Cells(lRow, 6).Value = lServ2
.Cells(lRow, 7).Value = Me.CboProduct.Value
.Cells(lRow, 8).Value = lQuant
.Cells(lRow, 9).Value = lRev
.Cells(lRow, 10).Value = lPay
' .Protect Password:="password"
End With


'clear the data
Me.CboUser.Value = ""
Me.CboService.Value = ""
Me.CboProduct.Value = ""
Me.CboPymnt.Value = ""
Me.CboUser.SetFocus
End Sub


The system currently gives the infamous error message of: Run-time error '-2147352571 (80020005)": Could not set the Value property. Type mismatch.
Any advice/help/hint would be greatly appreciated :-)
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Dear All,
I am trying to run a userform where a TextBox value is based on the selection of a ComboBox, i.e. the user would select a service and theform would bring up a predefined price from a table with vlookup. The codes currently look like this:

Private Sub CboService_Change()
With Me
.TextRev = Application.VLookup(Me.CboService.Value, Worksheets("Param").Range("Services"), 2, 0)
End With
End Sub

and

Private Sub CommandButton1_Click()


Dim lRow As Long
Dim lEmployee As String
Dim lCustomer As String
Dim lTime As String
Dim lService As Long
Dim lServ2 As String
Dim lProduct As String
Dim lQuant As Integer
Dim lRev As Integer
Dim lPay As String
Dim ws As Worksheet
Set ws = Worksheets(2)


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


lEmployee = Me.CboUser.ListIndex
m1 = Application.VLookup("m1", Worksheets("Param").Range("SysM"), 2, 0)


'check for a valid Employee entry
If Me.CboUser.Value = "" Then
MsgBox m1
Exit Sub
End If


lCustomer = Me.TextGuest.Value
lTime = Me.TextTime.Value
lService = Me.CboService.ListIndex
lProduct = Me.CboProduct.ListIndex
'check for separate product line


m8 = Application.VLookup("m8", Worksheets("Param").Range("SysM"), 2, 0)
If Me.CboService.Value <> "" And Me.CboProduct.Value <> "" Then
MsgBox m8
Exit Sub
End If


lQuant = Me.TextQuant.Value


lRev = Me.TextRev.Value
'check for Revenue entry
m9 = Application.VLookup("m9", Worksheets("Param").Range("SysM"), 2, 0)
If lRev = 0 Then
MsgBox m9
Exit Sub
End If


lPay = Me.CboPymnt.Value
'check for a valid Payment mode entry
m3 = Application.VLookup("m3", Worksheets("Param").Range("SysM"), 2, 0)
If lPay = "" Then
MsgBox m3
Exit Sub
End If


'copy the data to the database
'use protect and unprotect lines,
' with your password
' if worksheet is protected
With ws
' .Unprotect Password:="password"
.Cells(lRow, 2).Value = Me.CboUser.Value
.Cells(lRow, 3).Value = lCustomer
.Cells(lRow, 4).Value = Format(Date, "yyyy.mm.dd")
.Cells(lRow, 5).Value = lTime
.Cells(lRow, 6).Value = lServ2
.Cells(lRow, 7).Value = Me.CboProduct.Value
.Cells(lRow, 8).Value = lQuant
.Cells(lRow, 9).Value = lRev
.Cells(lRow, 10).Value = lPay
' .Protect Password:="password"
End With


'clear the data
Me.CboUser.Value = ""
Me.CboService.Value = ""
Me.CboProduct.Value = ""
Me.CboPymnt.Value = ""
Me.CboUser.SetFocus
End Sub


The system currently gives the infamous error message of: Run-time error '-2147352571 (80020005)": Could not set the Value property. Type mismatch.
Any advice/help/hint would be greatly appreciated :-)





[TABLE="width: 802"]
<tbody>[TR]
[TD]Department[/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C_[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[TD]G[/TD]
[TD]H[/TD]
[TD]I[/TD]
[TD]J[/TD]
[TD]K[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]1[/TD]
[TD]4[/TD]
[TD]8[/TD]
[TD]14[/TD]
[TD]20[/TD]
[TD]29[/TD]
[TD]31[/TD]
[TD]36[/TD]
[TD]49[/TD]
[TD]54[/TD]
[TD]58[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]2[/TD]
[TD]5[/TD]
[TD]9[/TD]
[TD]15[/TD]
[TD]21[/TD]
[TD]30[/TD]
[TD]32[/TD]
[TD]37[/T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][/TD]
[TD][/TD]
[TD]45[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]K[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]46[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]47[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]48[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody><colgroup><col><col span="11"></colgroup>[/TABLE]
Private Sub ComboBox1_Change()
Me.ComboBox2 = ""

Select Case Me.ComboBox1
Case "Department"
Me.ComboBox2.RowSource = "Department"
Case "A"
Me.ComboBox2.RowSource = "A"
Case "B"
Me.ComboBox2.RowSource = "B"
Case "C_"
Me.ComboBox2.RowSource = "C_"
Case "D"
Me.ComboBox2.RowSource = "D"
Case "E"
Me.ComboBox2.RowSource = "E"
Case "F"
Me.ComboBox2.RowSource = "F"
Case "G"
Me.ComboBox2.RowSource = "G"
Case "H"
Me.ComboBox2.RowSource = "H"
Case "I"
Me.ComboBox2.RowSource = "I"
Case "J"
Me.ComboBox2.RowSource = "J"
Case "K"
Me.ComboBox2.RowSource = "K"
End Select



End Sub
Private Sub UserForm_Initialize()

Dim lastrow As Long
Dim lastcolumn As Long
Dim i As Integer

lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
With Worksheets("Sheet1")

For i = 1 To lastcolumn
With .Columns(i)
lastrow = Sheet1.Cells(Rows.Count, i).End(xlUp).Row
With Range(Cells(1, i), Cells(lastrow, i))
Range(Cells(1, i), Cells(lastrow, i)).Select
Selection.CreateNames Top:=True
End With
End With
Next i
End With

Me.ComboBox1.RowSource = "Department"




End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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