Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 将数值设置为数字的单元格格式_Vba_Excel_If Statement - Fatal编程技术网

Vba 将数值设置为数字的单元格格式

Vba 将数值设置为数字的单元格格式,vba,excel,if-statement,Vba,Excel,If Statement,对你们来说,这是一个非常琐碎的问题。我基本上是在尝试识别一个区域中具有数值的每个单元格,然后将其格式化为“数字”(因此忽略了包含字符串的单元格)。我发现了一个excel公式,它使用IF和TRUE/FALSE表达式来计算哪个单元格与条件匹配,但是在VBA中运行代码时,我似乎无法存储IF语句 这可能是非常愚蠢的,因为我是VBA新手,但我会感谢所有的帮助 代码如下: Sub formatnumbers() Dim rng As Range Dim cell As Range Set rng = A

对你们来说,这是一个非常琐碎的问题。我基本上是在尝试识别一个区域中具有数值的每个单元格,然后将其格式化为“数字”(因此忽略了包含字符串的单元格)。我发现了一个excel公式,它使用IF和TRUE/FALSE表达式来计算哪个单元格与条件匹配,但是在VBA中运行代码时,我似乎无法存储IF语句

这可能是非常愚蠢的,因为我是VBA新手,但我会感谢所有的帮助

代码如下:

Sub formatnumbers()

Dim rng As Range
Dim cell As Range

Set rng = ActiveSheet.Range("A1:N10")

For Each cell In rng
 cell.Select
         If cell.Formula = "=COUNT(FIND({0,1,2,3,4,5,6,7,8,9},cell))>0, TRUE, FALSE)" = True Then
            cell.NumberFormat = "0.00"
         End If

Next cell

End Sub
请尝试以下代码:

Sub formatnumbers()
    Dim rng As Range
    Set rng = ActiveSheet.Range("A1:G15")
    rng.NumberFormat = "0.00"
End Sub
或:


如果您的目标是将范围内的每个单元格格式化为数字,那么最简单的方法是将单元格乘以1

'329' * 1 = 329
 329  * 1 = 329

因此,如果您循环范围内的所有单元格并简单地乘以1,您将得到该范围内每个单元格的数值。

如果是数字(cell.value),则可以使用
,然后使用
,或者只格式化整个范围,因为数字格式不会影响文本。格式化之前无需选择
单元格
'329' * 1 = 329
 329  * 1 = 329