问题
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