简体   繁体   中英

Excel checkboxes created using VBA out of proportion

I am working on a company wide file that has checkboxes created using VBA.

The problem: THese checkboxes are displayed based on a value selected from a dropdown, when a selection is made from the dropdown is made checkboxes are displayed. However the width of the checkbox doesn't show the full text that needs to be displayed.

Is there something that can be adjusted to fix this problem?

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

Example of problem:
问题示例

You can set the width via the Width property. I'd also propose creating a sub to apply the caption and value so you can cut down on the bulk of your code.

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

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