Search code examples
vbaif-statementms-wordgoto

Jumping to the end of IFs-statements when condition met at first IF


Following macro get input (Ticker) and translate it into Countries Name. After getting input it go through all If conditions. how could I stop macro from going through all Ifs when condition met at first If?

For example: Macro get input "BU" after first If variable UT becomes "BULGARIA" NOW I need to stop here and jump at the end of last If. how it is done? Is there any other way to tackle this kind of situation?

ST:
T = InputBox("Country Ticker?", "Finding country name using a Ticker", "Write Country Name to Transfer Data")

ut = UCase(T)
'Dim C As Integer
C = Len(T)

If T = "Write Country Name to Transfer Data" Then
MsgBox "No Country Name Input"
GoTo ST

ElseIf ut = "" Then
Exit Sub

ElseIf C < 2 Then
MsgBox "Wrong Country Name :) Please Try Again!"
GoTo ST
End If

If ut = "BU" Then ut = "BULGARIA"
If ut = "AR" Then ut = "ARGENTINA"
If ut = "CI" Then ut = "CHILE"
If ut = "CB" Then ut = "COLOMBIA"
If ut = "CZ" Then ut = "CROATIA"
If ut = "CP" Then ut = "CZECH REPUBLIC"
If ut = "ET" Then ut = "ESTONIA"
If ut = "HB" Then ut = "HUNGARY"
If ut = "IQ" Then ut = "IRAQ"
If ut = "KN" Then ut = "KENYA"
If ut = "LR" Then ut = "LATVIA"
If ut = "LH" Then ut = "LITHUANIA"
If ut = "MM" Then ut = "MEXICO"
If ut = "NL" Then ut = "NIGERIA"
If ut = "PW" Then ut = "POLAND"
If ut = "RO" Then ut = "ROMANIA"
If ut = "RM" Then ut = "RUSSIA"
If ut = "RU" Then ut = "RUSSIA"
If ut = "RX" Then ut = "RUSSIA"
If ut = "SJ" Then ut = "SOUTH AFRICA"
If ut = "TI" Then ut = "TURKEY"
If ut = "UG" Then ut = "UGANDA"
If ut = "UZ" Then ut = "UKRAINE"
If ut = "ZH" Then ut = "ZIMBABWE"
If ut = "AB" Then ut = "SAUDI ARABIA"
If ut = "EY" Then ut = "EGYPT"
If ut = "OM" Then ut = "OMAN"
If ut = "QD" Then ut = "QATAR"
If ut = "UH" Then ut = "UNITED ARAB EMIRATES"
If ut = "DU" Then ut = "UNITED ARAB EMIRATES"
If ut = "KK" Then ut = "KUWAIT"
If ut = "BI" Then ut = "BAHRAIN"
If ut = "JR" Then ut = "JORDAN"
If ut = "MC" Then ut = "MOROCCO"
If ut = "TZ" Then ut = "TANZANIA"
If ut = "RW" Then ut = "RWANDA"
If ut = "CD" Then ut = "COTE D IVOIRE"
If ut = "ZL" Then ut = "Zambia"
If ut = "SG" Then ut = "Serbia"
If ut = "NW" Then ut = "NAMIBIA"
If ut = "GN" Then ut = "GHANA"
If ut = "TP" Then ut = "TRINIDAD & TOBAGO"
If ut = "GA" Then ut = "GREECE"
If ut = "PS" Then ut = "PALESTINE"
If ut = "NO" Then ut = "NOTABLE RESEARCH"
C = Len(ut)
If C < 3 Then Exit Sub

Solution

  • Use Select Case

    Select Case ut
        Case "DU","UH"
            ut = "UNITED ARAB EMIRATES"
        Case "BU"
            ut = "BULGARIA"
        Case "AR"
            ut = "ARGENTINA"
            '...
    End Select
    

    You might also consider using a dictionary where the abbr e.g. BU is the key and the replacement is the value.

    Example with dictionary:

    Dim dict As Object, ut As String
    Set dict = CreateObject("Scripting.Dictionary")
    dict.Add "BU", "Bulagaria"
    dict.Add "AR", "Argentina"
    'etc
    
    'Example
    ut = "AR"
    If dict.Exists(ut) Then ut = dict(ut)