vba - 查找文本框/标签标题是否适合控件

标签 vba excel

该场景试图调整字体大小以获得良好的图形排列,或试图决定在哪里打破标题/副标题。
a) 在 XL VBA 中,有没有办法找出文本框上的文本或标签上的标题是否仍然适合控件?
b) 有没有办法知道多行控件上的文本/标题在哪里被破坏?

最佳答案

我休息了一下,给了它足够的回想时间(这比“尽快打嗝,以获得学分”产生更好的结果),并且......

Function TextWidth(aText As String, Optional aFont As NewFont) As Single
    Dim theFont As New NewFont
    Dim notSeenTBox As Control

    On Error Resume Next 'trap for aFont=Nothing
    theFont = aFont 'try assign

    If Err.Number Then 'can't use aFont because it's not instantiated/set
        theFont.Name = "Tahoma"
        theFont.Size = 8
        theFont.Bold = False
        theFont.Italic = False
    End If
    On Error GoTo ErrHandler

    'make a TextBox, fiddle with autosize et al, retrive control width
    Set notSeenTBox = UserForms(0).Controls.Add("Forms.TextBox.1", "notSeen1", False)
    notSeenTBox.MultiLine = False
    notSeenTBox.AutoSize = True 'the trick
    notSeenTBox.Font.Name = theFont.Name
    notSeenTBox.SpecialEffect = 0
    notSeenTBox.Width = 0 ' otherwise we get an offset (a ""feature"" from MS)
    notSeenTBox.Text = aText
    TextWidth = notSeenTBox.Width
    'done with it, to scrap I say
    UserForms(0).Controls.Remove ("notSeen1")
    Exit Function

ErrHandler:
    TextWidth = -1
    MsgBox "TextWidth failed: " + Err.Description
End Function

我觉得我正在/接近答案 b),但我会让它再休息一下……因为它比一闪而过地说“不可能”更好。

关于vba - 查找文本框/标签标题是否适合控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/256823/

相关文章:

vba - Excel 按首字母过滤列中超过 2 个值

file - 打开输入 VB 时指定编码

excel - VBA将Excel电子表格逐行导入Access

python - curve_fit、python和excel之间的幂律回归问题

arrays - 如何使 FLATTEN 对非连续范围起作用?

excel - 使用 OleDb 解析 Excel 时跳过几行

excel - 如何从索引匹配中添加一些结果

VBA 引用图表名称

sql-server - 更新 Excel 后,我无法再使用 SQL 查询获取数据

vba - 使用 VBA 代码复制和粘贴数据