VBA - String with comma-separated values, how to Vlookup against each value?

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
810
Office Version
  1. 365
Platform
  1. Windows
Hello everyone, I have a string like this:

Excel Formula:
Swindon, Chippenham, Malmesbury, Devizes, Cricklade

I've converted it to:

Excel Formula:
 Swindon, Chippenham, Malesbury, Devizes, Cricklade,

Note the space at the start and comma at the end.

This is because I have a list of pickups like so:

PickupRegion
Swindon1
Chippenham4
Malmesbury5
Devizes5
Cricklade10
Oxford20

What I'd like to do is end up with a string like so:

Excel Formula:
1, 4, 5, 10

Note how Malmesbury & Devizes share the same Region number, so they aren't duplicated?


I feel like the best way to do this would be to use the Split function:

VBA Code:
If Right(Range("K3").Value, 1) <> "," Then
' Prep pickups for matching
Range("AD2").Value = "Regions"

   Dim Ary As Variant ', Ary2 As Variant
   Dim r As Long
   
   Ary = Range("K2:K" & LastrowPPL).Value2
   'Ary2 = Range("AD2:AD" & LastrowPPL).Value2
   
   For r = 1 To UBound(Ary)
      Ary(r, 1) = " " & Ary(r, 1) & "," ' & Ary2(r, 1)
      
   Next r
   Range("K2:K" & LastrowPPL).Value = Ary
End If

The pickups are in range K2:K & LastrowPPL

What that's doing is essentially taking a string and then adding the space before and comma after, then:

VBA Code:
Dim Sp As Variant
Dim RegString As String
    Sp = Split(Ary, ", ")
    For r = 1 To UBound(Sp)
        RegLU = Application.WorksheetFunction.VLookup(Sp, Regions.Range("B2:C1424"), 2, 0)
        RegString = RegLU & Sp(r) & ", "
    Next r

My plan here is to take that same ary, splitting each cell on the comma, and using worksheetfunction to vlookup " Chippenham," against " Chippenham," in Regions tab, returning 4 and writing that in to the string.

However, this doesn't work. I get type mismatch on Sp = Split line.

What am I doing wrong here? Thanks.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Ah, FormR solved it nicely here:

VLOOKUP multiple results (Custom Function)

User defined function:

VBA Code:
Function MLookUp(lkup, tbl As Range, col As Long)
Dim a, b
With CreateObject("Scripting.Dictionary")
  .CompareMode = vbTextCompare
  For Each a In Split(lkup, ",")
    b = Application.VLookup(Trim(a), tbl, col, 0)
    If Not IsError(b) Then
      If Not .exists(b) Then .Add b, 1
    End If
  Next a
  MLookUp = Join(.keys, ", ")
End With
End Function
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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