繁体   English   中英

使用 VBA 创建的 Excel 复选框不成比例

[英]Excel checkboxes created using VBA out of proportion

我正在处理一个公司范围的文件,该文件具有使用 VBA 创建的复选框。

问题:这些复选框是根据从下拉列表中选择的值显示的,当从下拉列表中进行选择时,会显示复选框。 但是复选框的宽度不显示需要显示的全文。

有什么可以调整来解决这个问题的吗?

Dim cb As Object
    For Each cb In Sheet1.OLEObjects
        If InStr(1, cb.Name, "CheckBox") > 0 Then
            cb.Object.Value = False
            cb.Visible = False
        End IF
If Left(Range("Req_Type").Value, 3) = "LCL" Then
    Sheet1.CheckBox1.Visible = True
    Sheet1.CheckBox1.Caption = "L001"
    Sheet1.CheckBox1.Value = True
ElseIf Left(Range("Req_Type").Value, 3) = "JFS" Then
    Sheet1.CheckBox1.Visible = True
    Sheet1.CheckBox1.Caption = "L002"
    Sheet1.CheckBox1.Value = False
    Sheet1.CheckBox2.Visible = True
    Sheet1.CheckBox2.Caption = "L042"
    Sheet1.CheckBox2.Value = False
    Sheet1.CheckBox3.Visible = True
    Sheet1.CheckBox3.Caption = "L043"
    Sheet1.CheckBox3.Value = False
ElseIf Left(Range("Req_Type").Value, 3) = "PCB" Then
    Sheet1.CheckBox1.Visible = True
    Sheet1.CheckBox1.Caption = "L300"
    Sheet1.CheckBox1.Value = False
    Sheet1.CheckBox2.Visible = True
    Sheet1.CheckBox2.Caption = "L301"
    Sheet1.CheckBox2.Value = False
    Sheet1.CheckBox3.Visible = True
    Sheet1.CheckBox3.Caption = "L302"
    Sheet1.CheckBox3.Value = False
    Sheet1.CheckBox4.Visible = True
    Sheet1.CheckBox4.Caption = "L303"
    Sheet1.CheckBox4.Value = False
ElseIf Left(Range("Req_Type").Value, 4) = "REIT" Then
    Sheet1.CheckBox1.Visible = True
    Sheet1.CheckBox1.Caption = "P001"
    Sheet1.CheckBox1.Value = False
    Sheet1.CheckBox2.Visible = True
    Sheet1.CheckBox2.Caption = "P200"
    Sheet1.CheckBox2.Value = False
    Sheet1.CheckBox3.Visible = True
    Sheet1.CheckBox3.Caption = "P201"
    Sheet1.CheckBox3.Value = False
    Sheet1.CheckBox4.Visible = True
    Sheet1.CheckBox4.Caption = "P202"
    Sheet1.CheckBox4.Value = False
    Sheet1.CheckBox5.Visible = True
    Sheet1.CheckBox5.Caption = "P003"
    Sheet1.CheckBox5.Value = False
    Sheet1.CheckBox6.Visible = True
    Sheet1.CheckBox6.Caption = "P204"
    Sheet1.CheckBox6.Value = False
    Sheet1.CheckBox7.Visible = True
    Sheet1.CheckBox7.Caption = "P205"
    Sheet1.CheckBox7.Value = False
    Sheet1.CheckBox8.Visible = True
    Sheet1.CheckBox8.Caption = "R003"
    Sheet1.CheckBox8.Value = False
ElseIf Left(Range("Req_Type").Value, 3) = "SDM" Then
    Sheet1.CheckBox1.Visible = True
    Sheet1.CheckBox1.Caption = "L001"
    Sheet1.CheckBox1.Value = False
    Sheet1.CheckBox2.Visible = True
    Sheet1.CheckBox2.Caption = "L600"
    Sheet1.CheckBox2.Value = False

问题示例:
问题示例

您可以通过Width属性设置宽度。 我还建议创建一个 sub 来应用标题和值,这样您就可以减少大部分代码。

Sub Tester()

    Dim cb As Object, v

    For Each cb In Sheet1.OLEObjects
        'safer to use this to detect checkboxes
        ' in case names are changed...
        If TypeName(cb.Object) = "CheckBox" Then
            cb.Object.Value = False
            cb.Visible = True
            cb.Width = 50  '<<< set size here
        End If
    Next cb

    'need to move this outside of your For Each loop
    v = Range("Req_Type").Value
    Select Case True
        Case v Like "LCL*"
            SetUp Sheet1.CheckBox1, "L001", True
        Case v Like "JFS*"
            SetUp Sheet1.CheckBox1, "L002", False
            SetUp Sheet1.CheckBox2, "L042", False
            SetUp Sheet1.CheckBox3, "L043", False
        Case v Like "PCB*"
        '...etc
        '...etc
    End Select

End Sub

'Configure a checkbox with caption and value
'  could optionally set size here, maybe as a function of caption length
Sub SetUp(cb As Object, cbCaption As String, cbValue As Boolean)
    With cb
        .Visible = True 'since you always do this...
        .Caption = cbCaption
        .Value = cbValue
        '.Width = Len(cbCaption) * 5 'for example
    End With
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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