vba 如何检查单元格是否格式化为货币?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14347341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:11:26  来源:igfitidea点击:

How to check if cell is formatted as Currency?

excelvbaexcel-2010

提问by JAGAnalyst

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.

StringColumn 和 StringRow 是提供单元格引用的变量。

Your help is appreciated. Thanks!

感谢您的帮助。谢谢!

回答by Scott Holtzman

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.

如果您有其他货币,请将 替换为$您的货币符号。

回答by brettdj

You can also use the vbaequivalent of the formulae =CELL("format",A1)which starts with Cfor currency or accounting formats

您还可以使用以货币或会计格式 开头的公式的vba等效项 =CELL("format",A1)C

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