Listbox search function without duplicates

dss28

Board Regular
Joined
Sep 3, 2020
Messages
165
Office Version
  1. 2007
Platform
  1. Windows
I am enclosing my two sheets - first master data and second sheet showing a tabulated result which I want in a list box.
In the master sheet I have data in column A to R.
The columns F, G, R have same values for a one Patient ID (in column F). rest of the columns have different values for a given Patient ID

I want a listbox to show results of only columns F, G, R but in a single row without duplicates. (summary type)


search listbox data.xlsx
A
2
MastData


search listbox data.xlsx
J
19
ListBoxResult


can any one guide in this please.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I am enclosing my two sheets - first master data and second sheet showing a tabulated result which I want in a list box.
In the master sheet I have data in column A to R.
The columns F, G, R have same values for a one Patient ID (in column F). rest of the columns have different values for a given Patient ID

I want a listbox to show results of only columns F, G, R but in a single row without duplicates. (summary type)


search listbox data.xlsx
A
2
MastData


search listbox data.xlsx
J
19
ListBoxResult


can any one guide in this please.

I am using below code to search and display result in the listbox, need to remove the duplicates now and show only one result per each patient ID
VBA Code:
Private Sub cmdSearch_Click()

Dim RowNum As Long
Dim SearchRow As Long

RowNum = 2
SearchRow = 2

Worksheets("MastData").Activate

Do Until Cells(RowNum, 6).Value = ""

    If InStr(1, Cells(RowNum, 6).Value, txtKeywords.Value, vbTextCompare) > 0 Then     ' results to appear in Search sheet
        Worksheets("Search").Cells(SearchRow, 1).Value = Cells(RowNum, 6).Value    ' column F
        Worksheets("Search").Cells(SearchRow, 2).Value = Cells(RowNum, 7).Value    ' column G
        Worksheets("Search").Cells(SearchRow, 3).Value = Cells(RowNum, 18).Value     ' column R

        SearchRow = SearchRow + 1
    End If
    RowNum = RowNum + 1
Loop

If SearchRow = 2 Then
    MsgBox "No products were found that match your search criteria."
    Exit Sub
End If


Sheet3.Activate
With Sheet3

ListBox1.ColumnCount = 3
ListBox1.RowSource = "A2:C9999"
ListBox1.ColumnHeads = True
ListBox1.TextAlign = fmTextAlignLeft
ListBox1.SpecialEffect = fmSpecialEffectSunken
ListBox1.ColumnWidths = "120,120,120"
ListBox1.ListStyle = fmListStyleOption


'============================
End With


End Sub
 
Upvote 0
@dss28
Could you upload a sample workbook (without sensitive data) to a sharing site like dropbox.com or google drive? And then share the link here.
It will make it easier to test & find a solution.
 
Upvote 0
@dss28
Could you upload a sample workbook (without sensitive data) to a sharing site like dropbox.com or google drive? And then share the link here.
It will make it easier to test & find a solution.
I have attached the xl2bb files of both the sheets in my first initial post. if that is not visible or downloadable, will attach once again if required
 
Upvote 0
search listbox data.xlsx
ABCDEFGHIJKLMNOPQR
1ABCDEFGHIJKLMNOPQR
2Patient ACont 1Val 1
3Patient ACont 1Val 1
4Patient ACont 1Val 1
5Patient ACont 1Val 1
6Patient ACont 1Val 1
7Patient ACont 1Val 1
8Patient BCont 2Val 22
9Patient BCont 2Val 22
10Patient BCont 2Val 22
11Patient BCont 2Val 22
12Patient BCont 2Val 22
13Patient BCont 2Val 22
14Patient BCont 2Val 22
15Patient BCont 2Val 22
16Patient BCont 2Val 22
17Patient Ccont 3Val 33
18Patient Ccont 3Val 33
19Patient Ccont 3Val 33
20Patient Dcont 4Val44
21Patient Dcont 4Val44
22Patient Dcont 4Val44
23Patient Dcont 4Val44
24Patient Dcont 4Val44
25Patient Econt 5Val 55
26Patient Econt 5Val 55
27Patient Econt 5Val 55
28Patient Econt 5Val 55
29Patient Fcont 6Val 666
30Patient Fcont 6Val 666
MastData




search listbox data.xlsx
ABC
1FGR
2Patient ACont 1Val 1
3Patient BCont 2Val 22
4Patient Ccont 3Val 33
5Patient Dcont 4Val44
6Patient Econt 5Val 55
7Patient Fcont 6Val 666
ListBoxResult
 
Upvote 0
search listbox data.xlsx
ABCDEFGHIJKLMNOPQR
1ABCDEFGHIJKLMNOPQR
2Patient ACont 1Val 1
3Patient ACont 1Val 1
4Patient ACont 1Val 1
5Patient ACont 1Val 1
6Patient ACont 1Val 1
7Patient ACont 1Val 1
8Patient BCont 2Val 22
9Patient BCont 2Val 22
10Patient BCont 2Val 22
11Patient BCont 2Val 22
12Patient BCont 2Val 22
13Patient BCont 2Val 22
14Patient BCont 2Val 22
15Patient BCont 2Val 22
16Patient BCont 2Val 22
17Patient Ccont 3Val 33
18Patient Ccont 3Val 33
19Patient Ccont 3Val 33
20Patient Dcont 4Val44
21Patient Dcont 4Val44
22Patient Dcont 4Val44
23Patient Dcont 4Val44
24Patient Dcont 4Val44
25Patient Econt 5Val 55
26Patient Econt 5Val 55
27Patient Econt 5Val 55
28Patient Econt 5Val 55
29Patient Fcont 6Val 666
30Patient Fcont 6Val 666
MastData




search listbox data.xlsx
ABC
1FGR
2Patient ACont 1Val 1
3Patient BCont 2Val 22
4Patient Ccont 3Val 33
5Patient Dcont 4Val44
6Patient Econt 5Val 55
7Patient Fcont 6Val 666
ListBoxResult

after running the macro i get the filtered data in another sheet called "Search" like this

search listbox data.xlsm
ABC
1FGR
2Patient ACont 1Val 1
3Patient ACont 1Val 1
4Patient ACont 1Val 1
5Patient ACont 1Val 1
6Patient ACont 1Val 1
7Patient ACont 1Val 1
8Patient BCont 2Val 22
9Patient BCont 2Val 22
10Patient BCont 2Val 22
11Patient BCont 2Val 22
12Patient BCont 2Val 22
13Patient BCont 2Val 22
14Patient BCont 2Val 22
15Patient BCont 2Val 22
16Patient BCont 2Val 22
17Patient Ccont 3Val 33
18Patient Ccont 3Val 33
19Patient Ccont 3Val 33
20Patient Dcont 4Val44
21Patient Dcont 4Val44
22Patient Dcont 4Val44
23Patient Dcont 4Val44
24Patient Dcont 4Val44
25Patient Econt 5Val 55
26Patient Econt 5Val 55
27Patient Econt 5Val 55
28Patient Econt 5Val 55
29Patient Fcont 6Val 666
30Patient Fcont 6Val 666
Search


the end result in the listbox of the userform should be similar to the data presented in sheet "ListBoxResult"
 
Upvote 0
the end result in the listbox of the userform should be similar to the data presented in sheet "ListBoxResult"
1. So the listbox is in a userform not in the sheet?
2. You want the result in sheet "ListBoxResult" to be transferred to the Listbox.

See if this work.
1. In the userform module, use this code:

VBA Code:
Private Sub UserForm_Initialize()
With ListBox1
    .ColumnCount = 3
    .RowSource = ""
    .ColumnHeads = True
    .TextAlign = fmTextAlignLeft
    .SpecialEffect = fmSpecialEffectSunken
    .ColumnWidths = "120,120,120"
    .ListStyle = fmListStyleOption
End With

With Sheets("ListBoxResult")
    ListBox1.List = .Range("A2", .Cells(.Rows.Count, "C").End(xlUp)).Value
End With

End Sub

2. Remove the code for sheet3

VBA Code:
With Sheet3

ListBox1.ColumnCount = 3
ListBox1.RowSource = "A2:C9999"
ListBox1.ColumnHeads = True
ListBox1.TextAlign = fmTextAlignLeft
ListBox1.SpecialEffect = fmSpecialEffectSunken
ListBox1.ColumnWidths = "120,120,120"
ListBox1.ListStyle = fmListStyleOption


'============================
End With

If that doesn't work then I need a sample workbook.
 
Upvote 0
1. So the listbox is in a userform not in the sheet?
2. You want the result in sheet "ListBoxResult" to be transferred to the Listbox.

See if this work.
1. In the userform module, use this code:

VBA Code:
Private Sub UserForm_Initialize()
With ListBox1
    .ColumnCount = 3
    .RowSource = ""
    .ColumnHeads = True
    .TextAlign = fmTextAlignLeft
    .SpecialEffect = fmSpecialEffectSunken
    .ColumnWidths = "120,120,120"
    .ListStyle = fmListStyleOption
End With

With Sheets("ListBoxResult")
    ListBox1.List = .Range("A2", .Cells(.Rows.Count, "C").End(xlUp)).Value
End With

End Sub

2. Remove the code for sheet3

VBA Code:
With Sheet3

ListBox1.ColumnCount = 3
ListBox1.RowSource = "A2:C9999"
ListBox1.ColumnHeads = True
ListBox1.TextAlign = fmTextAlignLeft
ListBox1.SpecialEffect = fmSpecialEffectSunken
ListBox1.ColumnWidths = "120,120,120"
ListBox1.ListStyle = fmListStyleOption


'============================
End With

If that doesn't work then I need a sample workbook.
thanks for your valuable time
to be clear in what I want:
1. My workbook has sheets - "MastData" where all data is stored, sheet "Search" where filtered data after running macro is stored temporarily.
2. I have a userform with a textbox to search a particular text in the sheet "MastData" from column F and the results are displayed in the sheet "Search" and the data is captured in the listbox in the userform.
3. I have included this sheet "Search" just to show the data in the listbox. The data in the listbox has many duplicate rows as captured from the "MastData".
3. however I do not want this data as such in the listbox. I need the duplicate rows to be deleted and the final data in the listbox should look like the data presented in sheet "ListBoxSearch" . this sheet "ListBoxSearch" does not exist in my workbook but the tabulated data is just shown here to add clarity how the data in the list box should look like.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,021
Members
452,374
Latest member
keccles

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