2

我有以下公式,如果我在 Excel 中输入,它可以完美运行,但是当打开宏时它会给我一个错误,它会突出显示FRANCE

预期编译错误:语句结束

我真的很困惑,我不知道我做错了什么。任何人都可以看看这个。

ActiveCell.FormulaR1C1 = "=IF(LEFT(R[-1]C[1],6)=""AT_COM"",""AUSTRIA"",IF(LEFT(R[-1]C[1],6)=""BE_COM"",""BELUX"",IF(LEFT(R[-1]C[1],6)=""CP_COM"",""CZECH"",IF(LEFT(R[-1]C[1],6)=""CZ_COM"",""CZECH"",IF(LEFT(R[-1]C[1],6)=""DK_COM"",""DENMARK"",IF(LEFT(R[-1]C[1],6)=""FI_COM"",""FINLAND"",IF(LEFT(R[-1]C[1],6)=""FR_COM""," & _ ""**FRANCE**"",IF(LEFT(R[-1]C[1],6)=""DE_COM"",""GERMANY"",IF(LEFT(R[-1]C[1],6)=""GR_COM"",""GREECE"",IF(LEFT(R[-1]C[1],6)=""IL_COM"",""ISRAEL"",IF(LEFT(R[-1]C[1],6)=""IT_COM"",""ITALY"",IF(LEFT(R[-1]C[1],6)=""ME_COM"",""MIDDLE EAST"",IF(LEFT(R[-1]C[1],6)=""NL_COM"",""NETHERLANDS"",IF(LEFT(R[-1]C[1],6)=""NO_COM"",""NORWAY"",IF(LEFT(R[-1]C[1],6)=""PL_COM"",""POLAND"",IF(LEFT(R[-1]C[1],6)=""PT_COM"",""PORTUGAL"",IF(LEFT(R[-1]C[1],6)=""RU_RUSSIA"",""RUSSIA"",IF(LEFT(R[-1]C[1],6)=""RU_ENT"",""RUSSIA"",IF(LEFT(R[-1]C[1],6)=""SEE_CO"",""SEE"",IF(LEFT(R[-1]C[1],6)=""ES_COM"",""SPAIN"",IF(LEFT(R[-1]C[1],6)=""SA_COM"",""SOUTH AFRICA"",IF(LEFT(R[-1]C[1],6)=""SE_COM"",""SWEDEN"",IF(LEFT(R[-1]C[1],6)=""CH_COM"",""SWITZERLAND"",IF(LEFT(R[-1]C[1],6)=""TR_COM"",""TURKEY"",IF(LEFT(R[-1]C[1],6)=""UK_COM"",""UK"",IF(LEFT(R[-1]C[1],6)=""UK_ENT"",""UK PS"",""UNKNOWN""))))))))))))))))))))))))))"

4

4 回答 4

3

如果您正在做 VBA,而不是反对 UDF,那么我会提出一个这样的自定义函数,您可以从工作表中调用它:

工作表中 UDF 的屏幕截图

Function LookupCountryCode(countrycode As String) As String
Dim myVal As String
Dim cntryShort As String

cntryShort = Left(countrycode, 6)

    Select Case Trim(UCase(cntryShort))
        Case "AT_COM"
            myVal = "AUSTRIA"
        Case "BE_COM"
            myVal = "BELUX"
        Case "CP_COM", "CZ_COM"
            myVal = "CZECH"
        Case "DK_COM"
            myVal = "DENMARK"
        Case "FI_COM"
            myVal = "FINLAND"
        Case "FR_COM"
            myVal = "FRANCE"
        Case "DE_COM"
            myVal = "GERMANY"
        Case "GR_COM"
            myVal = "GREECE"
        Case "IL_COM"
            myVal = "ISRAEL"
        Case "IT_COM"
            myVal = "ITALY"
        Case "ME_COM"
            myVal = "MIDDLE EAST"
        Case "NL_COM"
            myVal = "NETHERLANDS"
        Case "NO_COM"
            myVal = "NORWAY"
        Case "PL_COM"
            myVal = "POLAND"
        Case "PT_COM"
            myVal = "PORTUGAL"
        Case "RU_COM", "RU_ENT"
            myVal = "RUSSIA"
        Case "SEE_CO"
            myVal = "SEE"
        Case "ES_COM"
            myVal = "SPAIN"
        Case "SA_COM"
            myVal = "SOUTH AFRICA"
        Case "SE_COM"
            myVal = "SWEDEN"
        Case "CH_COM"
            myVal = "SWITZERLAND"
        Case "TR_COM"
            myVal = "TURKEY"
        Case "UK_COM"
            myVal = "UK"
        Case "UK_ENT"
            myVal = "UK PS"
        Case Else
            myVal = "UNKNOWN"
    End Select

    LookupCountryCode = myVal

End Function

在 VBA 中,您可以这样称呼它。我的R1C1符号有点生疏,所以可能需要稍微调整一下:

With ActiveCell
    .FormulaR1C1 = "=LookupCountryCode(" & _
        .Offset(-1, 1).Address(, , xlR1C1) & _
        ")"
End With
于 2013-05-07T17:22:25.377 回答
2

我看到的唯一明显的是“法国”前面的下划线。这是不合适的吗?

你有没有考虑过把这张桌子放在某个地方——也许在一张新的纸上——并使用 VLOOKUP?

于 2013-05-07T16:37:51.287 回答
1

我可以先说哇,我认为这是我见过的最大的!

您需要 3 " 在 France 1 前面开始字符串,并在公式中创建一个 2。

ActiveCell.FormulaR1C1 = "=IF(LEFT(R[-1]C[1],6)=""AT_COM"",""AUSTRIA"",IF(LEFT(R[-1]C[1],6)=""BE_COM"",""BELUX"",IF(LEFT(R[-1]C[1],6)=""CP_COM"",""CZECH"",IF(LEFT(R[-1]C[1],6)=""CZ_COM"",""CZECH"",IF(LEFT(R[-1]C[1],6)=""DK_COM"",""DENMARK"",IF(LEFT(R[-1]C[1],6)=""FI_COM"",""FINLAND"",IF(LEFT(R[-1]C[1],6)=""FR_COM""," & _ """**FRANCE**"",IF(LEFT(R[-1]C[1],6)=""DE_COM"",""GERMANY"",IF(LEFT(R[-1]C[1],6)=""GR_COM"",""GREECE"",IF(LEFT(R[-1]C[1],6)=""IL_COM"",""ISRAEL"",IF(LEFT(R[-1]C[1],6)=""IT_COM"",""ITALY"",IF(LEFT(R[-1]C[1],6)=""ME_COM"",""MIDDLE EAST"",IF(LEFT(R[-1]C[1],6)=""NL_COM"",""NETHERLANDS"",IF(LEFT(R[-1]C[1],6)=""NO_COM"",""NORWAY"",IF(LEFT(R[-1]C[1],6)=""PL_COM"",""POLAND"",IF(LEFT(R[-1]C[1],6)=""PT_COM"",""PORTUGAL"",IF(LEFT(R[-1]C[1],6)=""RU_RUSSIA"",""RUSSIA"",IF(LEFT(R[-1]C[1],6)=""RU_ENT"",""RUSSIA"",IF(LEFT(R[-1]C[1],6)=""SEE_CO"",""SEE"",IF(LEFT(R[-1]C[1],6)=""ES_COM"",""SPAIN"",IF(LEFT(R[-1]C[1],6)=""SA_COM"",""SOUTH AFRICA"",IF(LEFT(R[-1]C[1],6)=""SE_COM"",""SWEDEN"",IF(LEFT(R[-1]C[1],6)=""CH_COM"",""SWITZERLAND"",IF(LEFT(R[-1]C[1],6)=""TR_COM"",""TURKEY"",IF(LEFT(R[-1]C[1],6)=""UK_COM"",""UK"",IF(LEFT(R[-1]C[1],6)=""UK_ENT"",""UK PS"",""UNKNOWN""))))))))))))))))))))))))))"

顺便说一句,我会这样写:

ActiveCell.FormulaR1C1 = "=IF(LEFT(R[-1]C[1],6)=""AT_COM"",""AUSTRIA""," & _
"IF(LEFT(R[-1]C[1],6)=""BE_COM"",""BELUX""," & _
"IF(LEFT(R[-1]C[1],6)=""CP_COM"",""CZECH""," & _
"IF(LEFT(R[-1]C[1],6)=""CZ_COM"",""CZECH""," & _
"IF(LEFT(R[-1]C[1],6)=""DK_COM"",""DENMARK""," & _
"IF(LEFT(R[-1]C[1],6)=""FI_COM"",""FINLAND""," & _
"IF(LEFT(R[-1]C[1],6)=""FR_COM"",""**FRANCE**""," & _
....
于 2013-05-09T11:28:25.310 回答
0

我知道我已经发布了另一个答案,但我认为这是值得的。这是一个经典问题,我喜欢使用以下VLOOKUP/ 数组 -{}解决方案。CHOOSE调用公式的廉价函数:

ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(LEFT(R[-1]C[1],6)," & _    
    "{""AT_COM"",""AUSTRIA"";""BE_COM"",""BELUX"";""CP_COM"",""CZECH"";""CZ_COM"",""CZECH"";" & _
    """DK_COM"",""DENMARK"";""FI_COM"",""FINLAND"";""FR_COM""," & _ """**FRANCE**"";""DE_COM"",""GERMANY"";" & _
    """GR_COM"",""GREECE"";""IL_COM"",""ISRAEL"";""IT_COM"",""ITALY"";""ME_COM"",""MIDDLE EAST"";" & _
    """NL_COM"",""NETHERLANDS"";""NO_COM"",""NORWAY"";""PL_COM"",""POLAND"";""PT_COM"",""PORTUGAL"";" & _
    """RU_RUSSIA"",""RUSSIA"";""RU_ENT"",""RUSSIA"";""SEE_CO"",""SEE"";""ES_COM"",""SPAIN"";" & _
    """SA_COM"",""SOUTH AFRICA"";""SE_COM"",""SWEDEN"";""CH_COM"",""SWITZERLAND"";""TR_COM"",""TURKEY"";" & _
    """UK_COM"",""UK"";""UK_ENT"",""UK PS""},2,FALSE)," & _
    """UNKNOWN"")"
于 2013-05-09T11:59:11.860 回答