Results 1 to 3 of 3

Thread: Setting Column Widths of a ListBox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    Montreal, Quebec
    Posts
    400

    Question Setting Column Widths of a ListBox

    I am using the code below to set the width of columns in a ListBox on a UserForm in Excel. However, I get the error "Could not set the ColumnWidths property. Type mismatch". Any thoughts?
    Code:
    Private Sub UserForm_Initialize()
        ThisWorkbook.Sheets("Sheet1").Activate
        colCnt = 2
        Set rng = ActiveSheet.UsedRange 'Range("A3:B25")
        With ListBox1
            .ColumnCount = colCnt
            .RowSource = rng.Address
            cw = ""
            For c = 1 To .ColumnCount
                cw = cw & rng.Columns(c).Width & ":"
            Next c
            .ColumnWidths = cw
            .ListIndex = 0
        End With
    End Sub

  2. #2
    Si_the_geek
    Guest
    "Type mismatch" generally means you are trying to pass text when a number is expected, or vice-versa.

    Try this instead:
    VB Code:
    1. Private Sub UserForm_Initialize()
    2.     ThisWorkbook.Sheets("Sheet1").Activate
    3.     colCnt = 2
    4.     Set rng = ActiveSheet.UsedRange 'Range("A3:B25")
    5.     With ListBox1
    6.         .ColumnCount = colCnt
    7.         .RowSource = rng.Address
    8.  
    9.         For c = 1 To .ColumnCount
    10.           .ColumnWidths (c) = rng.Columns(c).Width
    11.         Next c
    12.  
    13.         .ListIndex = 0
    14.     End With
    15. End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    Montreal, Quebec
    Posts
    400
    Thanks "Si".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width