You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel VBA标签TextAlign居中无效:仅顶部居中,需文本垂直居中

VBA标签控件实现文本完全居中的方法

你遇到的问题是因为TextAlign = fmTextAlignCenter仅控制水平方向的居中,要让文本在标签控件的正中间(水平+垂直双方向居中),需要补充设置垂直对齐属性。

对于Excel中的Forms Label控件,只需在代码中添加VerticalAlignment属性设置即可:

Sub SetLabelProperties()
    Dim ws As Worksheet
    Dim ctrl As OLEObject

    Set ws = ThisWorkbook.Worksheets("WorkFlow")

    For Each ctrl In ws.OLEObjects
        If TypeName(ctrl.Object) = "Label" Then
            With ctrl.Object
                .Font.Size = 10
                .Font.Name = "Calibri"
                .ForeColor = RGB(70, 70, 70)  
                .BackColor = RGB(204, 255, 0)
                .BorderColor = RGB(169, 169, 169)  
                .BorderStyle = fmBorderStyleSingle
                .TextAlign = fmTextAlignCenter ' 水平居中
                .VerticalAlignment = fmVerticalAlignmentCenter ' 垂直居中,新增这行
            End With
        End If
    Next ctrl
End Sub

添加.VerticalAlignment = fmVerticalAlignmentCenter后,标签内的文本就会同时在水平和垂直方向上处于控件的正中间位置。

内容的提问来源于stack exchange,提问作者vbaguru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 05:24:52