VBA USERFORM : Search and Find a String and Remove from the Combobox List

sachinns

Board Regular
Joined
Jun 21, 2019
Messages
52
Hi Friends,


I have a combobox in User form where the data is taking from a sheet called "Inconsistency Order" and the combobox name is cmbSelChartAttr . I have so many values in the droplist. The values which ever is having a string "- check" in the end , It should not be added to the combobox List.


Example : We have values like : ABC , DEF , GHI , ABC - check , DEF , GHI - check .


So In our combobox droplist , The droplist will only show ABC , DEF , GHI .


Someone please help to add code for this in my existing Code.

Code:
Function formInitialize(ws As Worksheet)
Set ws = ThisWorkbook.Worksheets("Inconsistency Order")
If ws.Visible = True Then
ws.Activate
Else
ws.Visible = xlSheetVisible
ws.Activate
End If
Dim i, lc
Dim j
Dim cell As Range
Dim tecAttrVal As String
lc = ws.Cells(1, Columns.Count).End(xlToLeft).Column
i = 0


Dim c As Collection
Set c = New Collection


On Error Resume Next




For i = i + 1 To lc
tecAttrVal = Cells(1, i).Value
c.Add tecAttrVal, tecAttrVal
If Err.Number = 0 Then
cmbSelChartAttr.AddItem tecAttrVal
Else
Err.Clear
End If


Next i
Me.Label3.Visible = False
Me.lsbOrderSet4SelAttr.Visible = False


End Function

Thanks in Advance
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
This might do the trick:
Code:
Function formInitialize(ws As Worksheet)
Set ws = ThisWorkbook.Worksheets("Inconsistency Order")
If ws.Visible = True Then
ws.Activate
Else
ws.Visible = xlSheetVisible
ws.Activate
End If
Dim i, lc
Dim j
Dim cell As Range
Dim tecAttrVal As String
lc = ws.Cells(1, Columns.Count).End(xlToLeft).Column
i = 0


Dim c As Collection
Set c = New Collection


On Error Resume Next




For i = i + 1 To lc
tecAttrVal = Cells(1, i).Value
c.Add tecAttrVal, tecAttrVal
If Err.Number = 0 And Right(tecAttrVal, 5) <> "check" Then
cmbSelChartAttr.AddItem tecAttrVal
Else
Err.Clear
End If


Next i
Me.Label3.Visible = False
Me.lsbOrderSet4SelAttr.Visible = False


End Function
...not tested, though...
 
Upvote 0
Where are the values you want in the dropdown located on the sheet 'Inconsistency Order'?

Is it always that sheet you want the values from?

What if there is unique value with ' check' after it?

Would you want to list that in the dropdown?
 
Last edited:
Upvote 0
@sykes

Thanks for the reply. It is working but some issues are there.

Example : My values in the sheets are ABC , DEF , GHI , ABC Mandatory Check , DEF , GHI - Check.

The Values i wanted in my Dropdown is : ABC , DEF , GHI , ABC Mandatory Check .

So as per your code the value "ABC Mandatory Check" is not coming in the drop list.

I hope you understand i point.
 
Last edited:
Upvote 0
@Norie

Where are the values you want in the dropdown located on the sheet 'Inconsistency Order'?

Example : ABC , DEF , GHI , ABC Mandatory Check , DEF , GHI - Check.

Is it always that sheet you want the values from?
Yes

What if there is unique value with ' check' after it?
I dont want any values which has "- Check" .

Would you want to list that in the dropdown?
I dont want to link any values which consists of "- Check"
 
Upvote 0
If I understand correctly, you wish to have any values - except those ending in "- check" ...so the hyphen character is important to omit, too? That's the only difference i can see in your explaation.:
Code:
Function formInitialize(ws As Worksheet)
Set ws = ThisWorkbook.Worksheets("Inconsistency Order")
If ws.Visible = True Then
ws.Activate
Else
ws.Visible = xlSheetVisible
ws.Activate
End If
Dim i, lc
Dim j
Dim cell As Range
Dim tecAttrVal As String
lc = ws.Cells(1, Columns.Count).End(xlToLeft).Column
i = 0


Dim c As Collection
Set c = New Collection


On Error Resume Next




For i = i + 1 To lc
tecAttrVal = Cells(1, i).Value
c.Add tecAttrVal, tecAttrVal
If Err.Number = 0 And Right(tecAttrVal, 7) <> "- check" Then
cmbSelChartAttr.AddItem tecAttrVal
Else
Err.Clear
End If


Next i
Me.Label3.Visible = False
Me.lsbOrderSet4SelAttr.Visible = False


End Function
 
Upvote 0
Pleasure. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,403
Messages
6,119,309
Members
448,886
Latest member
GBCTeacher

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