How to check if cell is formatted as Currency?

谁说我不能喝 提交于 2019-12-24 17:31:10

问题


I am working on a project where it is necessary to check that a large number of cells meet a number of criteria.

I have been able to use the code below to check whether a cell contains a value in a numeric format. However, I also need a way to check whether a cell contains a value formatted as US Currency.

If Not Application.WorksheetFunction.IsNumber(Range(StringColumn & StringRow).Value) Then  
    MsgBox "Test Failed at " & StringColumn & StringRow  
    Exit Sub  
Else: MsgBox "Valid format for cell " & StringColumn & StringRow  
End If

StringColumn and StringRow are variables that supply the cell reference.

Your help is appreciated. Thanks!


回答1:


Assuming you are using $ as your currency, you can try this:

Dim strFormat as String

strFormat = Range(StringColumn & StringRow).NumberFormat

If InStr(1,strFormat,"$") = 0 Then  'not a currency format

    MsgBox "Test Failed at " & StringColumn & StringRow  
    Exit Sub  

Else: MsgBox "Valid format for cell " & StringColumn & StringRow  

End If

If you have another currency, replace the $ with your currency symbol.




回答2:


You can also use the vba equivalent of the formulae =CELL("format",A1) which starts with C for currency or accounting formats

MsgBox Left$(Evaluate("CELL(""Format"",A1)"), 1) = "C"


来源:https://stackoverflow.com/questions/14347341/how-to-check-if-cell-is-formatted-as-currency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!