Setting up a Case Select structure using a reference table

A Guy Named Robby

New Member
Joined
Oct 19, 2016
Messages
25
I have lots of Case Select scenarios set up. I'm going to have to add a lot more. A greatly condensed sample is below. How can I recreate this scenario using a reference table?

Beginning on row 84 to the end of the data, it's comparing what's in column O (college names) to hundreds/thousands of cases. If it finds a match, it enters a specific digit in column N.

Code:
Sub NumberFillIn()
Dim LastRow As Long
Dim i As Long
LastRow = Range("O" & Rows.Count).End(xlUp).Row
For i = 84 To LastRow

Select Case Range("O" & i)

Case "New York University", "NYU", "New York Univ.", "New York Univ"
Range("N" & i) = "1"

Case "Abraham Lincoln University"
Range("N" & i) = "2"

Case "Mississippi College"
Range("N" & i) = "3"

Case "Piedmont College"
Range("N" & i) = "4"

Case "Minnesota State University"
    Select Case Range("P" & i)
    Case "Bemidji"
    Range("N" & i) = "5"
    Case "Moorhead"
    Range("N" & i) = "6"
    Case "St Cloud", "St. Cloud"
    Range("N" & i) = "7"
    Case "Mankato"
    Range("N" & i) = "8"
    End Select

End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Set up a sheet in your workbook named reference. Then build your table there, like this:

ABC
1ValueMatch1Match2
21New York University*
31NYU*
41New York Univ.*
51New York Univ*
62Abraham Lincoln University*
73Mississippi College*
84Piedmont College*
95Minnesota State UniversityBemidji
106Minnesota State UniversityMoorhead
117Minnesota State UniversitySt Cloud
127Minnesota State UniversitySt. Cloud
138Minnesota State UniversityMankato
149USC*
1510UCLA*

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Reference



The asterisk in column C means anything will match.

Then change your VBA to something like this:

Code:
Sub NumberFillIn()
Dim LastRow As Long, i As Long
Dim MyOptions As Variant, j As Long, Match1 As String, Match2 As String

    MyOptions = Sheets("Reference").Range("A1:C15").Value
    
    LastRow = Range("O" & Rows.Count).End(xlUp).Row
    
    For i = 84 To LastRow
        
        Match1 = Range("O" & i)
        Match2 = Range("P" & i)
        For j = 2 To UBound(MyOptions)
            If Match1 Like MyOptions(j, 2) And Match2 Like MyOptions(j, 3) Then
                Range("N" & i).Value = MyOptions(j, 1)
                Exit For
            End If
        Next j
                
    Next i
    
End Sub

There are ways to tweak this, but this should give you the basic idea. Let us know if this works for you.
 
Upvote 0
Interesting. Thanks for that. It seems simple enough, but I was hoping I'd be able to do something like this, where you could have multiple possible matches in one cell instead of in separate rows. Is there any way to make that work?

ABC
1ValueMatch1Match2
21New York University, NYU, New York Univ., New York Univ
*

<tbody>
</tbody>
 
Upvote 0
Also, I'm going to have this table in a different workbook. What do I need to do to the following line to make it point to a specific file?

Code:
MyOptions = Sheets("Reference").Range("A1:C15").Value
 
Upvote 0
OK, set up your table like this:

ABC
1ValueMatch1Match2
21New York University, NYU, New York Univ., New York Univ*
32Abraham Lincoln University*
43Mississippi College*
54Piedmont College*
65Minnesota State UniversityBemidji
76Minnesota State UniversityMoorhead
87Minnesota State UniversitySt Cloud, St. Cloud
98Minnesota State UniversityMankato
109USC*
1110UCLA*

<tbody>
</tbody>
Reference



Then you can use this code:

Rich (BB code):
Sub NumberFillIn()
Dim LastRow As Long, i As Long
Dim MyOptions As Variant, j As Long, Match1 As String, Match2 As String
    
    With Workbooks("Reference.xlsx")
        LastRow = .Sheets("Reference").Cells(Rows.Count, "A").End(xlUp).Row
        MyOptions = .Sheets("Reference").Range("A1:C" & LastRow).Value
    End With
    
    LastRow = Range("O" & Rows.Count).End(xlUp).Row
    
    For i = 84 To LastRow
        
        Match1 = Range("O" & i)
        Match2 = Range("P" & i)
        For j = 2 To UBound(MyOptions)
            If InStr(MyOptions(j, 2), Match1) > 0 And InStr(MyOptions(j, 3), Match2) > 0 Then
                Range("N" & i).Value = MyOptions(j, 1)
                Exit For
            End If
        Next j
                
    Next i
    
End Sub

You can see the line in red that allows you to pull the reference table from another workbook. With the macro coded this way, that workbook must be open when you run this macro. There are more complicated ways to read data from a closed workbook, but it's probably easier to just open it and minimize it.

I used the InStr function instead of the Like function so that you could put the comma delimited variations in a single cell.

Hope this helps!
 
Upvote 0
I got curious and did a bit more research. Here's a way to read from a closed file:

Rich (BB code):
Sub NumberFillIn()
Dim LastRow As Long, i As Long
Dim MyOptions As Variant, j As Long, Match1 As String, Match2 As String
    
    Application.ScreenUpdating = False
    With Worksheets.Add
        .Range("$A$1:$C$1000").FormulaArray = _
        "='C:\Users\username\Documents\[Reference.xlsx]Reference'!$A$1:$C$1000"
        LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
        MyOptions = .Range("A1:C" & LastRow).Value
        Application.DisplayAlerts = False
        .Delete
        Application.DisplayAlerts = True
    End With
    
    LastRow = Range("O" & Rows.Count).End(xlUp).Row
    
    For i = 84 To LastRow
        
        Match1 = Range("O" & i)
        Match2 = Range("P" & i)
        For j = 2 To UBound(MyOptions)
            If InStr(MyOptions(j, 2), Match1) > 0 And InStr(MyOptions(j, 3), Match2) > 0 Then
                Range("N" & i).Value = MyOptions(j, 1)
                Exit For
            End If
        Next j
                
    Next i

    Application.ScreenUpdating = True
    
End Sub

It's not too terribly complicated. It requires the full path name of the file. It creates a temporary sheet, pulls the data onto the temporary sheet, then saves the pertinent data to an array, then deletes the temporary sheet. The 1000 in the range would be an upper limit, change it to something meaningful for your data.

Hope this helps!
 
Last edited:
Upvote 0
Actually, wait. I tried using your code for reading from a closed file. I'm getting this error: Unable to set the FormulaArray property of the Range class

Code:
Sub NumberFillIn3()
Dim LastRow As Long, i As Long
Dim MyOptions As Variant, j As Long, Match1 As String, Match2 As String
    
    Application.ScreenUpdating = False
    With Worksheets.Add
        .Range("$A$1:$D$1000").FormulaArray = _
        "='H:\Folder\Folder\hoodlookuptesting.xlsx]Reference'!$A$1:$D$1000"
        LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
        MyOptions = .Range("A1:C" & LastRow).Value
        Application.DisplayAlerts = False
        .Delete
        Application.DisplayAlerts = True
    End With
    
    LastRow = Range("O" & Rows.Count).End(xlUp).Row
    
    For i = 84 To LastRow
        
        Match1 = Range("O" & i)
        Match2 = Range("P" & i)
        For j = 2 To UBound(MyOptions)
            If InStr(MyOptions(j, 2), Match1) > 0 And InStr(MyOptions(j, 3), Match2) > 0 Then
                Range("N" & i).Value = MyOptions(j, 1)
                Exit For
            End If
        Next j
                
    Next i
End Sub
 
Upvote 0
You missed a bracket:

Rich (BB code):
.Range("$A$1:$D$1000").FormulaArray = _
        "='H:\Folder\Folder\[hoodlookuptesting.xlsx]Reference'!$A$1:$D$1000"
 
Upvote 0

Forum statistics

Threads
1,216,446
Messages
6,130,690
Members
449,585
Latest member
Nattarinee

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