VBA 每个范围的数字格式不同
VBA Numberformat differently for each range
我想根据第二个单元格的小数位数更改范围的格式。任何想法?
我正在使用以下函数来计算小数(有效)
Function CountDecimalPlaces(aNumber As Double) As Long
Dim len1 As Long, len2 As Long
len1 = Len(CStr(aNumber))
len2 = Len(CStr(Int(aNumber)))
CountDecimalPlaces = len1 - len2 + CLng(len1 <> len2)
End Function
并想用它来格式化我的具有不同小数位数的范围
For b = 1 To lastCol
Range(cells(3,b),cells(50,b)).NumberFormat = "0." & CountDecimalPlaces (Cells(2, b)) x 0
Next b
当然我知道 "CountDecimalPlaces (Cells(2, b)) x 0" 行不通,但我希望它能让你理解并帮助你
将您的代码替换为:
Range(cells(3,b),cells(50,b)).NumberFormat = "0." & String(CountDecimalPlaces(Cells(2, b)), "0")
String
需要两个强制参数:
Number
: 一个字符必须重复的次数
Character
: 必须重复的字符
这是计算数字中小数位数的另一种方法:
<s>Function CountDecimalPlaces(aNumber As Double) As Long
CountDecimalPlaces = Len(Split(CStr(aNumber), ".")(1))
End Function
</s>
编辑(根据 Excelosaurus 的建议):
Function CountDecimalPlaces(aNumber As Double) As Long
If Int(aNumber) = aNumber Then
CountDecimalPlaces = 0
Else
CountDecimalPlaces = Len(Split(CStr(aNumber), Application.International(xlDecimalSeparator))(1))
End If
End Function
您也可以如下修改您的计数小数。
Function CountDecimalPlaces(aNumber As Double) As String
Dim len1 As Long, len2, lenX As Long
Dim i As Integer
Dim answer As String
answer = "0."
len1 = Len(CStr(aNumber))
len2 = Len(CStr(Int(aNumber)))
lenX = len1 - len2 + CLng(len1 <> len2)
If lenX > 0 Then
For i = 1 To lenX
answer = answer & "0"
Next i
End If
CountDecimalPlaces = answer
End Function
并像这样在您的主要功能中使用:
Range(cells(3,b),cells(50,b)).NumberFormat = CountDecimalPlaces (Cells(2, b))
我想根据第二个单元格的小数位数更改范围的格式。任何想法? 我正在使用以下函数来计算小数(有效)
Function CountDecimalPlaces(aNumber As Double) As Long
Dim len1 As Long, len2 As Long
len1 = Len(CStr(aNumber))
len2 = Len(CStr(Int(aNumber)))
CountDecimalPlaces = len1 - len2 + CLng(len1 <> len2)
End Function
并想用它来格式化我的具有不同小数位数的范围
For b = 1 To lastCol
Range(cells(3,b),cells(50,b)).NumberFormat = "0." & CountDecimalPlaces (Cells(2, b)) x 0
Next b
当然我知道 "CountDecimalPlaces (Cells(2, b)) x 0" 行不通,但我希望它能让你理解并帮助你
将您的代码替换为:
Range(cells(3,b),cells(50,b)).NumberFormat = "0." & String(CountDecimalPlaces(Cells(2, b)), "0")
String
需要两个强制参数:
Number
: 一个字符必须重复的次数Character
: 必须重复的字符
这是计算数字中小数位数的另一种方法:
<s>Function CountDecimalPlaces(aNumber As Double) As Long
CountDecimalPlaces = Len(Split(CStr(aNumber), ".")(1))
End Function
</s>
编辑(根据 Excelosaurus 的建议):
Function CountDecimalPlaces(aNumber As Double) As Long
If Int(aNumber) = aNumber Then
CountDecimalPlaces = 0
Else
CountDecimalPlaces = Len(Split(CStr(aNumber), Application.International(xlDecimalSeparator))(1))
End If
End Function
您也可以如下修改您的计数小数。
Function CountDecimalPlaces(aNumber As Double) As String
Dim len1 As Long, len2, lenX As Long
Dim i As Integer
Dim answer As String
answer = "0."
len1 = Len(CStr(aNumber))
len2 = Len(CStr(Int(aNumber)))
lenX = len1 - len2 + CLng(len1 <> len2)
If lenX > 0 Then
For i = 1 To lenX
answer = answer & "0"
Next i
End If
CountDecimalPlaces = answer
End Function
并像这样在您的主要功能中使用:
Range(cells(3,b),cells(50,b)).NumberFormat = CountDecimalPlaces (Cells(2, b))