+ Reply to Thread
Results 1 to 8 of 8

Listbox Column Widths and Lengths -Autofit?

  1. #1
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Listbox Column Widths and Lengths -Autofit?

    Please forgive me if this post shows up twice; my first attempt disappeared on me (the system logged me out when I tried to submit it).

    I've got a Listbox in a User form that looks at data on another spreadsheet purely for informational purposes, not to be selected. I have two problems with what it's doing:

    1) There are 23 columns, of varying widths. However the listbox uses a standard width, so some items are truncated and some have too much room.

    2) The data changes, and the number of rows may change from 2 to 15000. I've set the RowSource to cover the ranged from A1:W15000, but if only 2-300 lines are selected the user can't grab the scrollbar button to scroll through, but must instead use the arrow, which is rather slow.

    Is there any way to set the column widths to match the data, and the Range to only go as far as the last populated row?

    Thanks in advance for your help on this.
    Last edited by jomili; 12-14-2009 at 12:10 PM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Listbox Column Widths and Lengths -Autofit?

    here's one way, possibly not 100% accurate.
    Please Login or Register  to view this content.
    Last edited by royUK; 12-09-2009 at 05:56 PM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Listbox Column Widths and Lengths -Autofit?

    Roy,

    I tried your solution, and got a Run-Time Error 9: Subscript out of range.

    One possible error I see is putting this routine in the Initialize step. Once the user selects the region, the data is copied from from the SourceData/Sheet1 workbook/sheet to the Final/Data workbook/sheet, so if this runs in the Initialize subform routine that's prior to populating the data.

    However, I tried it with data present, and that's when I got the Subscript out of range.

    Here's the code as I tried to implement it. I'd appreciate your eyeballing it for errors:
    Please Login or Register  to view this content.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Listbox Column Widths and Lengths -Autofit?

    Move it to the Activate event of the Form, but it might be better to post an eample workbook so that I can see the full picture

  5. #5
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Listbox Column Widths and Lengths -Autofit?

    I haven't tried moving to activate yet, just commented it out for now so I could get you a working copy of what I've got so far (see the attachments).
    Attached Files Attached Files

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Listbox Column Widths and Lengths -Autofit?

    There's a terrific amount of tidying up needed on your code. You have Option Explicit at radom places, this shol ppar once at the top of a Code module. You haven't declared all your variables.

    I suggest yo Debug your code and orrect all the errors that show up first

  7. #7
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Listbox Column Widths and Lengths -Autofit?

    Roy,

    I've tidied up what I could. I don't know what variables haven't been declared. I also don't know how to use "Option Explicit"; I've seen it in a few routines, but don't know what it does, where it goes, how to use, or why to use it. If I try to put it at the top of a routine it jumps up to the bottom of the previous one.

    I've attached updated spreadsheets for your review.

    Going to the "Activate Event of the Form", the listbox form gets populated when the data is copied from SourceData to Final. No click happens, on the form, so the code won't be applied. How do we get it to apply automatically, or should I be using something different than a listbox to show the data?
    Attached Files Attached Files

  8. #8
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Listbox Column Widths and Lengths -Autofit?

    Marking as solved, as I'm redoing the userform and may not even use the field. I appreciate Roy's help. Thanks, and Merry Christmas

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1