停止VBA简化分数并保持格式。

huangapple go评论60阅读模式
英文:

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.

停止VBA简化分数并保持格式。

Sub for_live_code()
    
    StartRow = 2
            
    Do Until Range(&quot;AT&quot; &amp; StartRow).Value = &quot;&quot;
        If Range(&quot;AR&quot; &amp; StartRow).Value &lt; 100 Then
            Range(&quot;AQ&quot; &amp; StartRow).NumberFormat = &quot;0#/0#&quot;
        Else
            Range(&quot;AQ&quot; &amp; StartRow).NumberFormat = &quot;0##/0#&quot;
        End If
        Range(&quot;AQ&quot; &amp; StartRow).Value = Range(&quot;AR&quot; &amp; StartRow).Value &amp; &quot;/&quot; &amp; Range(&quot;AS&quot; &amp; 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(&quot;B&quot; &amp; StartRow).Value = &quot;&quot;
        &#39;        If Range(&quot;C&quot; &amp; StartRow).Value &lt; 100 Then
        &#39;            Range(&quot;A&quot; &amp; StartRow).NumberFormat = &quot;0#/0#&quot;
        &#39;        Else
        &#39;            Range(&quot;A&quot; &amp; StartRow).NumberFormat = &quot;0##/0#&quot;
        &#39;        End If
        Range(&quot;A&quot; &amp; StartRow).NumberFormat = &quot;@&quot;  &#39; 文本格式
        Range(&quot;A&quot; &amp; StartRow).Value = Format(Range(&quot;C&quot; &amp; StartRow).Value, &quot;00&quot;) &amp; &quot;/&quot; &amp; Format(Range(&quot;B&quot; &amp; StartRow), &quot;00&quot;)
        
                
        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(&quot;B&quot; &amp; StartRow).Value = &quot;&quot;
        &#39;        If Range(&quot;C&quot; &amp; StartRow).Value &lt; 100 Then
        &#39;            Range(&quot;A&quot; &amp; StartRow).NumberFormat = &quot;0#/0#&quot;
        &#39;        Else
        &#39;            Range(&quot;A&quot; &amp; StartRow).NumberFormat = &quot;0##/0#&quot;
        &#39;        End If
        Range(&quot;A&quot; &amp; StartRow).NumberFormat = &quot;@&quot;  &#39; Text Format
        Range(&quot;A&quot; &amp; StartRow).Value = Format(Range(&quot;C&quot; &amp; StartRow).Value, &quot;00&quot;) &amp; &quot;/&quot; &amp; Format(Range(&quot;B&quot; &amp; StartRow), &quot;00&quot;)
        
                
        StartRow = StartRow + 1
    Loop
End Sub

huangapple
  • 本文由 发表于 2023年5月14日 15:15:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定