Excel VBA : 格式化为不带百分号的百分比
Excel VBA : format to percentage without percentage sign
为了让问题尽可能简单:
在 excel 单元格 A1 中有一个数值:0.11
我想将单元格格式化为百分比值但没有 % 符号。
我想要:11
;不是 11%
我不是在问如何定期执行此操作 excel;必须是 VBA..
我想 Range("A1").NumberFormat = ....
是要走的路;但是使用这种方法时,总是会出现%符号。
有没有办法在没有百分号的情况下格式化它?
请注意,该值无法更改,必须保持为 0.11。
试试这个,
Range("A5").NumberFormat = "0.00" & Chr(10) & "%"
Range("A5").WrapText = True
这将按要求工作:
Private Sub remove_percent(where As Range)
Dim cell As Range
For Each cell In where
If (cell.NumberFormat = "0.00") Then
cell = Round(cell * 100, 0)
cell.NumberFormat = "0"
' if we exceed 100%, let's round it to 100
If (cell > 100) Then
cell = 100
End If
End If
Next cell
End Sub
然后您可以通过以下方式调用它(作为示例):
Call remove_percent(Range("A1:A5"))
这有点变通,但试试这个:
Sub Test()
Dim i As Long
Dim STRNG As String
For i = 2 To Range("A1").Height / 11.25
STRNG = STRNG & Chr(10)
Next i
Debug.Print STRNG
Range("A1").NumberFormat = STRNG & "0" & Chr(10) & "%"
End Sub
它会查看单元格高度并计算隐藏“%”符号需要多少换行符。
您可能想添加它,比如在 sheet 更改事件时调用它,而不是只查看单元格 A1 然后从 A1 循环到 A & Lastrow.....
或者将它分配给一个按钮offcourse
另一种解决方法是将单元格的内容显示 x 100。
Sub Macro1()
Dim cell As Range
For Each cell In Range("A:A") ' Change with your Range
If cell.Value > 0 Then
cell.NumberFormat = Str(cell.Value * 100)
End If
Next cell
End Sub
这可行,但每次更改都必须重新运行。例如,在您的工作表中使用以下代码:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A:A")) Is Nothing Then
Call Macro1
End If
End Sub
Sub Macro1()
Dim cell As Range
For Each cell In Range("A:A") ' Change with your Range
If cell.Value > 0 Then
cell.NumberFormat = Str(cell.Value * 100)
End If
Next cell
End Sub
为了让问题尽可能简单:
在 excel 单元格 A1 中有一个数值:0.11
我想将单元格格式化为百分比值但没有 % 符号。
我想要:11
;不是 11%
我不是在问如何定期执行此操作 excel;必须是 VBA..
我想 Range("A1").NumberFormat = ....
是要走的路;但是使用这种方法时,总是会出现%符号。
有没有办法在没有百分号的情况下格式化它? 请注意,该值无法更改,必须保持为 0.11。
试试这个,
Range("A5").NumberFormat = "0.00" & Chr(10) & "%"
Range("A5").WrapText = True
这将按要求工作:
Private Sub remove_percent(where As Range)
Dim cell As Range
For Each cell In where
If (cell.NumberFormat = "0.00") Then
cell = Round(cell * 100, 0)
cell.NumberFormat = "0"
' if we exceed 100%, let's round it to 100
If (cell > 100) Then
cell = 100
End If
End If
Next cell
End Sub
然后您可以通过以下方式调用它(作为示例):
Call remove_percent(Range("A1:A5"))
这有点变通,但试试这个:
Sub Test()
Dim i As Long
Dim STRNG As String
For i = 2 To Range("A1").Height / 11.25
STRNG = STRNG & Chr(10)
Next i
Debug.Print STRNG
Range("A1").NumberFormat = STRNG & "0" & Chr(10) & "%"
End Sub
它会查看单元格高度并计算隐藏“%”符号需要多少换行符。
您可能想添加它,比如在 sheet 更改事件时调用它,而不是只查看单元格 A1 然后从 A1 循环到 A & Lastrow.....
或者将它分配给一个按钮offcourse
另一种解决方法是将单元格的内容显示 x 100。
Sub Macro1()
Dim cell As Range
For Each cell In Range("A:A") ' Change with your Range
If cell.Value > 0 Then
cell.NumberFormat = Str(cell.Value * 100)
End If
Next cell
End Sub
这可行,但每次更改都必须重新运行。例如,在您的工作表中使用以下代码:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A:A")) Is Nothing Then
Call Macro1
End If
End Sub
Sub Macro1()
Dim cell As Range
For Each cell In Range("A:A") ' Change with your Range
If cell.Value > 0 Then
cell.NumberFormat = Str(cell.Value * 100)
End If
Next cell
End Sub