VBA Function to return Multiple Values

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,176
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have some code and it works for returning single values but I need to modify it to bring back multiple results
Code:
Public Function States(s As String) As String
    Select Case True
        Case InStr(s, "Alabama"): States = "AL"
        Case InStr(s, "Kansas"): States = "KS"
        Case InStr(s, "Connecticut"): States = "CT"
        Case InStr(s, "Virginia"): States = "VA"
        Case Else
    End Select
End Function
<style type="text/css">
table.tableizer-table {
font-size: 12px;
border: 1px solid #CCC ;
font-family: Arial, Helvetica, sans-serif;
}
.tableizer-table td {
padding: 4px;
margin: 3px;
border: 1px solid #CCC ;
}
.tableizer-table th {
background-color: #104E8B ;
color: #FFF ;
font-weight: bold;
}
</style>
<table class="tableizer-table">
<thead><tr class="tableizer-firstrow"><th>2000 ARLINGTON OAKS DR S; MOBILE Alabama 36695-8739</th><th>AL</th></tr></thead><tbody>
<tr><td>978 VIRGINIA ST SABETHA Kansas 66534-2431</td><td>KS</td></tr>
<tr><td>5200 WILSHIRE RD CHESAPEAKE Virginia 23321-3280</td><td>VA</td></tr>
<tr><td>1952 East Main St Bridgeport Connecticut 06610, 44 Brooks St Woodstock Virginia</td><td>CT, VA</td></tr>
<tr><td>1943 Roosevelt St Wichita Kansas, 441 West Ave. Auburn Alabama</td><td>KA, AL</td></tr>
</tbody></table>
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try:

Code:
Public Function States(s As String) As String
Dim S1 As Variant, S2 As Variant, i As Long

    S1 = Array("AL", "KS", "CT", "VA")
    S2 = Array("Alabama", "Kansas", "Connecticut", "Virginia")

    For i = 0 To UBound(S1)
        If InStr(s, S2(i)) > 0 Then States = States & ", " & S1(i)
    Next i
    States = Mid(States, 3)
End Function
 
Upvote 0
Try:
Code:
Public Function States(ByRef msg As String) As String

    Dim x       As Long
    Dim arr()   As String
    arr = Split("AlabamaAL|KansasKS|ConnecticutCT|VirginiaVA", "|")
    
    For x = LBound(arr) To UBound(arr)
        If InStr(msg, Left$(arr(x), Len(arr(x)) - 2)) Then States = States & Right$(arr(x), 2) & ", "
    Next x

    States = Left$(States, Len(States) - 2)
    Erase arr
    
End Function
 
Last edited:
Upvote 0
AHHHH! Thank you both for the help and knowledge that you supplied me!! I greatly appreciate it! Thanks again!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
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