简体   繁体   中英

Reduce Font Size by 1 Step in VBA

Is there a simple way to reduce the Font Size in Word / Excel etc by 1 step in VBA?

So, say if my Font size was 48 could I reduce it to 36, easily, as per the Font drop down in the standard Word 2007 Font group, rather than reducing the font size by 12 - I will not know what the next font size down is...

So rather than setting Font Size explicitly by float:

MyText.Font.Size = 36;

could I do something like:

MyText.Font.Size -= Reduce by 1 step;....  forgive the pseudo code!

For Excel, you can check the Font Size combobox on the Formatting toolbar. That's 2003 stuff and I think it will still work in 2007 and beyond, but I don't have it available to test.

Sub FontShrink(rng As Range)

    Dim i As Long
    Dim ctl As CommandBarComboBox

    Set ctl = Application.CommandBars("Formatting").Controls("Font Size:")

    If rng.Font.Size > CDbl(ctl.List(ctl.ListCount)) Then
        'if it's bigger than the biggest, make it the biggest
        rng.Font.Size = ctl.List(ctl.ListCount)
    Else
        For i = ctl.ListCount To 2 Step -1
            If rng.Font.Size > CDbl(ctl.List(i)) Then
                rng.Font.Size = CDbl(ctl.List(i))
                Exit For
            ElseIf rng.Font.Size = CDbl(ctl.List(i)) Then
                rng.Font.Size = CDbl(ctl.List(i - 1))
                Exit For
            End If
        Next i
    End If
End Sub

Use the Font object's Shrink method:

MyText.Font.Shrink

The opposite is MyText.Font.Grow .

You can Grow and Shrink fonts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM