Combining VBA Codes

Klea

New Member
Joined
Jun 29, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello! I'm extremely new to all of this and I am trying to create a drop down menu that will allow me to make multiple selections plus show any results that contain the selected option in any way.

Klea (4).xlsm
D
1
2Order
3Urine culture
4Urine culture
5Urine culture
6Throat Culture
7Throat Culture
8HIV screen, RPR, Hep B, HepBSAg w/ Reflex confirm, GC/Chlamydia, Urine
9HIV screen, RPR, Hep B, HepBSAg w/ Reflex confirm
10Liver function panel
11Celiac Disease Panel
12SureSwab Vaginosis/Vaginitus Plus, Herpes culture
13GC/Chlamydia, Urine, Pap Smear
14Urine culture
15GC/Chlamydia, Urine
16Anemia panel, C-reactive protein, Sed Rate
17Urine culture
18HepBSAg w/ Reflex confirm, Hepatitis C Ab, RPR
19Urine culture
Sheet1
Cells with Data Validation
CellAllowCriteria
D1:D19ListUrine culture,Throat Culture,HIV screen,RPR,Hep B,HepBSAg w/ Reflex confirm,GC/Chlamydia,Urine,Liver function panel,Celiac Disease Panel,SureSwab Vaginosis/Vaginitus Plus,Herpes culture,Pap Smear,Anemia panel,C-reactive protein,Sed Rate,Hepatitis C Ab


So the drop down menu is in D1. I want to be able to select "Urine" and it show me all the results that contain "Urine" whether it's by itself or with a list of other tests ran as well. I also want to be able to select "Urine", "HIV", and "Pap Smear" and have it return all valid responses to me. I also want it to be able to recognize any version of (for example) "Pap Smear", whether it was spelled with capitalization or not, etc.

Here are all the codes I have so far. However, because two of the codes are named the same thing, it does not work. How do I combine these in such a way that they still function the way I need them to. Thank you for your help!!

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("D1")) Is Nothing Then Exit Sub
    With Range("A2")
        .CurrentRegion.AutoFilter 4, "*" & Target & "*"
    End With
End Sub

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
' To allow multiple selections in a Drop Down List in Excel (without repetition)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$D$1" Then
  If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
    GoTo Exitsub
  Else: If Target.Value = "" Then GoTo Exitsub Else
    Application.EnableEvents = False
    Newvalue = Target.Value
    Application.Undo
    Oldvalue = Target.Value
      If Oldvalue = "" Then
        Target.Value = Newvalue
      Else
        If InStr(1, Oldvalue, Newvalue) = 0 Then
            Target.Value = Oldvalue & ", " & Newvalue
      Else:
        Target.Value = Oldvalue
      End If
    End If
  End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub



VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Intersect(Target, Range("D1")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, dic As Object, splt As Variant, rng As Variant
    v = Range("D3", Range("D" & Rows.Count).End(xlUp)).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(v)
        If InStr(v(i, 1), ",") = 0 Then
            If Not dic.Exists(v(i, 1)) Then
                dic.Add v(i, 1), Nothing
            End If
        Else
            splt = Split(v(i, 1), ", ")
            For Each rng In splt
                If Not dic.Exists(rng) Then
                    dic.Add rng, Nothing
                End If
            Next rng
        End If
    Next i
    With Target.Validation
         .Delete
         .Add Type:=xlValidateList, Formula1:=Join(dic.keys, ",")
     End With
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Not really understand precisely try this
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
' To allow multiple selections in a Drop Down List in Excel (without repetition)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$D$1" Then
  If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
    GoTo Exitsub
  Else: If Target.Value = "" Then GoTo Exitsub Else
    With Range("A2")
        .CurrentRegion.AutoFilter 4, "*" & Target & "*"
    End With
    Application.EnableEvents = False
    Newvalue = Target.Value
    Application.Undo
    Oldvalue = Target.Value
      If Oldvalue = "" Then
        Target.Value = Newvalue
      Else
        If InStr(1, Oldvalue, Newvalue) = 0 Then
            Target.Value = Oldvalue & ", " & Newvalue
      Else:
        Target.Value = Oldvalue
      End If
    End If
  End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

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