Newbie VBA - Combobox check if value exist in named range, error if not

Barsoe

New Member
Joined
Mar 12, 2022
Messages
11
Office Version
  1. 365
Platform
  1. Windows
hi

I am new here and have only recently started looking at VBA.
I copied code from various threads to make the function I am looking for... almost there now, but just need a little check to avoid entering invalid data

I have a userform with a combobox, 3 textboxes.

What I need is the value I enter in the combobox (cboVare) to check against the items listed in the range (Varenr).
If it doesn't exist in the range, it should not be possible to add the data to my worksheet.




Code as follows:



Private Sub cmdAdd_Click()
Dim lRow As Long
Dim lPart As Long

Dim ws As Worksheet
Set ws = Worksheets("Bevægelser")

lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

lPart = Me.cboVare.ListIndex

With ws.Cells(lRow, 4)
.NumberFormat = "dd/mm/yyyy"
.Value = Dato
End With

With ws
.Cells(lRow, 1).Value = Me.cboVare.Value
.Cells(lRow, 5).Value = Me.cboVare.List(lPart, 1)
.Cells(lRow, 2).Value = Me.Ind.Value
.Cells(lRow, 3).Value = Me.Ud.Value
End With

Me.Dato.Value = "dd/mm"
Me.Ind.Value = ""
Me.Ud.Value = ""
cboVare.SetFocus

End Sub


Private Sub UserForm_Initialize()

Dim cVare As Range
Dim ws As Worksheet

Set ws = Worksheets("Varer")

For Each cVare In ws.Range("Varenr")
With Me.cboVare
.AddItem cVare.Value
.List(.ListCount - 1, 1) = cVare.Offset(0, 1).Value
End With
Next cVare


Me.Dato.Value = "dd/mm"
Me.Ind.Value = ""
Me.Ud.Value = ""
Me.cboVare.SetFocus

End Sub



Help appreciated!
thanks
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I think you're saying that you don't want to allow anyone to type in a value. I say that because it appears your list comes from said range, thus the only list values you should have are the ones you have in the range. If so, see
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
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