RockandGrohl
Well-known Member
- Joined
- Aug 1, 2018
- Messages
- 801
- Office Version
- 365
- Platform
- Windows
Hello everyone, I have a string like this:
I've converted it to:
Note the space at the start and comma at the end.
This is because I have a list of pickups like so:
What I'd like to do is end up with a string like so:
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:
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:
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 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:
Pickup | Region |
Swindon | 1 |
Chippenham | 4 |
Malmesbury | 5 |
Devizes | 5 |
Cricklade | 10 |
Oxford | 20 |
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.