Convert strings

hanasamo

Board Regular
Joined
May 31, 2005
Messages
83
A B
PEK BEIJING
SHA SHANGHAI
CAN GUANGZHOU

In sheet1 I have a database like the above, the column A is city code, and the column B contains theie corresponding city name.

In sheet2 there are citypairs as follows:
PEK-SHA
PEK-SHA-PEK
PEK-SHA-CAN-SHA
PEK-SHA-CAN-SHA-PEK

What I want to do is convert the citypairs into their actual city names, for example, PEK-SHA-CAN-SHA, I want it to be BEIJING-SHANGHAI-GUANGZHOU-SHANGHAI. I have tried a several time by using functions, mid, left,right,index,match,if, but I failed because it's too long to be kept correct. Can you experts tell me how to do it, VBA and functions are both fine. Thanks in advance.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi,

A VBA UDF solution, assuming you have Excel 2002 or higher:
Code:
Function CityCodes(ByVal Codes As String, ByVal CodeTable As Range)
Dim iPtr As Integer
Dim rTable As Range, rBase As Range
Dim vElements As Variant, vResult As Variant
Dim wsTable As Worksheet

Set wsTable = Sheets(CodeTable.Parent.Name)
Set rBase = wsTable.Range(Cells(CodeTable.Row, CodeTable.Column).Address)

Set rTable = wsTable.Range(rBase.Address, _
                           wsTable.Cells(CodeTable.Row + CodeTable.Rows.Count - 1, _
                                         CodeTable.Column).Address)
vElements = Split(Codes, "-")
For iPtr = 0 To UBound(vElements)
    vResult = "*"
    On Error Resume Next
    vResult = WorksheetFunction.Match(vElements(iPtr), rTable, 0)
    On Error GoTo 0
    If IsNumeric(vResult) = True Then
        vElements(iPtr) = rBase.Offset(vResult - 1, 1).Text
    End If
Next iPtr
CityCodes = Join(vElements, "-")
End Function
Book1
ABCD
1CodeCity
2PEKBEIJING
3SHASHANGHAI
4CANGUANGZHOU
Sheet1


Formula in B2 & copied down is =citycodes(A2,Sheet1!A:B)
 
Upvote 0
Hi
using Replace Method
Code:
Sub test()
Dim a, i As Long
a = Sheets("sheet1").Range("a1").CurrentRegion.Resize(, 2).Value
For i = 1 To UBound(a, 1)
    With Sheets("sheet2")
        .Columns("a").Replace what:=a(i, 1), replacement:=a(i, 2)
    End With
Next
Erase a
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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