简体   繁体   English

使用VBA根据表格格式化货币

[英]Formatting Currencies based on a table with VBA

So I want to apply number formatting to the value in the cells in column "AC" based on the currency denoted in column "O" by doing a vlookup on a named range I have on a different sheet (named "currencies") 因此,我想通过在不同工作表上具有的命名范围(名为“ currencies”)上执行vlookup来基于“ O”列中表示的货币将数字格式应用于“ AC”列中的单元格中的值

Here is the code in question that is throwing up errors about being "unable to get the VLookup Property of the WorksheetFunction class" 这是有问题的代码,它抛出有关“无法获取WorksheetFunction类的VLookup属性”的错误。

If Not (Range("O" & i)) = vbNullString Then
    If Not IsEmpty(Range("O" & i)) Then
        Range("AC" & i).NumberFormat = 
        Application.WorksheetFunction.VLookup(Range("O" & i), currencies, 2, False)
    End If
End If

and here is the table that comprises the named range "currencies" 这是包含命名范围“ currencies”的表

在此处输入图片说明

Anyone have any suggestions for why this isn't working and ways I can make it more resilient to throwing up errors that the people who will actually be using this macro wont understand? 任何人都对为什么这样做不起作用有任何建议,并且我可以提出一些方法以使其更灵活地抛出那些实际上将使用此宏的人员不理解的错误?

Here I would use the evaluate function, because it facilitates writing more complex formulas in vba. 在这里,我将使用评估函数,因为它有助于在vba中编写更复杂的公式。 Now because of the IFERROR there will be no error just a blank cell if the formula does not work. 现在,由于IFERROR,如果公式不起作用,则仅空白单元格将没有错误。 Once a value is retrieved by the evaluate function, then apply your formatting. 通过评估函数检索到值后,请应用格式。

If Not (Range("O" & i)) = vbNullString Then
    If Not IsEmpty(Range("O" & i)) Then
        If Evaluate("IFERROR(VLOOKUP(O" & i & ",currencies, 2, False),"""")") <> "" then
          'APPLY NUMBER FORMATTING HERE
           Range("AC" & i).Numberformat = ""
        End if
    End If
End If

Based on previous questions you have asked, I imagine your code should look like this: 根据您之前提出的问题,我认为您的代码应如下所示:

If Not (Range("O" & i)) = vbNullString Then
    If Not IsEmpty(Range("O" & i)) Then
        If Evaluate("IFERROR(VLOOKUP(O" & i & ",currencies, 2, False),"""")") <> "" then
          'APPLY NUMBER FORMATTING HERE
           Select Case ("IFERROR(VLOOKUP(O" & i & ",currencies, 2, False),"""")")
           Case "USD"
           Range("AC" & i).NumberFormat = "$#,##0.00_);($#,##0.00)"
           Case "RMB"
           Range("AC" & i).NumberFormat = "[$¥-zh-CN]#,##0.00;[$¥-zh-CN]-#,##0.00"
           Case "EUR"
           Range("AC" & i).NumberFormat = "[$€-x-euro2] #,##0.00_);([$€-x-euro2] #,##0.00)"
           Case "GBP"
           Range("AC" & i).NumberFormat = "[$£-en-GB]#,##0.00;-[$£-en-GB]#,##0.00"
           Case "HKD"
           Range("AC" & i).NumberFormat = "[$HK$-zh-HK]#,##0.00_);([$HK$-zh-HK]#,##0.00)"
           Case "JPY"
           Range("AC" & i).NumberFormat = "[$¥-ja-JP]#,##0.00;-[$¥-ja-JP]#,##0.00"
           End Select
        End if
    End If
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM