英文:
Stop VBA simplifying a fraction and keep the format
问题
I'm trying to use VBA to put together two numbers with a "/" between them and a leading zero but it keeps simplifying the fraction rather than keeping the format I have set.
Sub for_live_code()
StartRow = 2
Do Until Range("AT" & StartRow).Value = ""
If Range("AR" & StartRow).Value < 100 Then
Range("AQ" & StartRow).NumberFormat = "0#/0#"
Else
Range("AQ" & StartRow).NumberFormat = "0##/0#"
End If
Range("AQ" & StartRow).Value = Range("AR" & StartRow).Value & "/" & Range("AS" & StartRow)
StartRow = StartRow + 1
Loop
End Sub
How do I stop the fraction being simplified and keep the formatting? Also, it is giving a decimal number in the formula bar and if it is not a whole number it is not keeping the first zero.
Thanks for the help!
英文:
I'm trying to use VBA to put together two numbers with a "/" between them and a leading zero but it keeps simplifying the fraction rather than keeping the format I have set.
Sub for_live_code()
StartRow = 2
Do Until Range("AT" & StartRow).Value = ""
If Range("AR" & StartRow).Value < 100 Then
Range("AQ" & StartRow).NumberFormat = "0#/0#"
Else
Range("AQ" & StartRow).NumberFormat = "0##/0#"
End If
Range("AQ" & StartRow).Value = Range("AR" & StartRow).Value & "/" & Range("AS" & StartRow)
StartRow = StartRow + 1
Loop
End Sub
How do I stop the fraction being simplified and keep the formatting? Also, it is giving a decimal number in the formula bar and if it is not a whole number it is not keeping the first zero.
Thanks for the help!
答案1
得分: 2
在文档中说:
> 如果您不需要对分数进行计算,可以在键入分数之前将单元格格式设置为文本,方法是点击“类别”列表中的“文本”。这样,您键入的分数将不会被减少或转换为小数。但是,您不能对显示为文本的分数进行数学计算。
所以,我猜,以下代码可以实现你想要的功能
Sub for_live_code()
Dim StartRow
StartRow = 2
Do Until Range("B" & StartRow).Value = ""
' If Range("C" & StartRow).Value < 100 Then
' Range("A" & StartRow).NumberFormat = "0#/0#"
' Else
' Range("A" & StartRow).NumberFormat = "0##/0#"
' End If
Range("A" & StartRow).NumberFormat = "@" ' 文本格式
Range("A" & StartRow).Value = Format(Range("C" & StartRow).Value, "00") & "/" & Format(Range("B" & StartRow), "00")
StartRow = StartRow + 1
Loop
End Sub
英文:
In the documentation it says
> If you don't need to perform calculations on fractions, you can format
> a cell as text before you type a fraction into it by clicking Text in
> the Category list. This way, the fractions that you type will not be
> reduced or converted to decimals. However, you cannot perform
> mathematical calculations on fractions that are displayed as text.
So, I guess, the following code does what you are after
Sub for_live_code()
Dim StartRow
StartRow = 2
Do Until Range("B" & StartRow).Value = ""
' If Range("C" & StartRow).Value < 100 Then
' Range("A" & StartRow).NumberFormat = "0#/0#"
' Else
' Range("A" & StartRow).NumberFormat = "0##/0#"
' End If
Range("A" & StartRow).NumberFormat = "@" ' Text Format
Range("A" & StartRow).Value = Format(Range("C" & StartRow).Value, "00") & "/" & Format(Range("B" & StartRow), "00")
StartRow = StartRow + 1
Loop
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论