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

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
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,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

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