[英]Formatting Currencies based on a table with VBA
因此,我想通過在不同工作表上具有的命名范圍(名為“ currencies”)上執行vlookup來基於“ O”列中表示的貨幣將數字格式應用於“ AC”列中的單元格中的值
這是有問題的代碼,它拋出有關“無法獲取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
這是包含命名范圍“ currencies”的表
任何人都對為什么這樣做不起作用有任何建議,並且我可以提出一些方法以使其更靈活地拋出那些實際上將使用此宏的人員不理解的錯誤?
在這里,我將使用評估函數,因為它有助於在vba中編寫更復雜的公式。 現在,由於IFERROR,如果公式不起作用,則僅空白單元格將沒有錯誤。 通過評估函數檢索到值后,請應用格式。
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
根據您之前提出的問題,我認為您的代碼應如下所示:
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.