Results 1 to 3 of 3

Thread: [RESOLVED] How to set column width of a listview report/table?

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2015
    Posts
    51

    Resolved [RESOLVED] How to set column width of a listview report/table?

    I am just learning the listview control in Excel 2007 vba.

    I managed to create a listview and load the headers and populate the table/report.

    This is the line code:

    Code:
    hdr = "FieldName"
    ListView1.ColumnHeaders.Add , , hdr, 55
    Question 1. Is this the only place to set column width?
    Question 2. Can I set the width in ListView1.ListItems.Add and/or ListView1.ListSubItems.Add ?
    Question 3. What does the 55 in the code represent? Pixel? Dots? mm?
    Question 4. How can I set dynamic variable width (let the text content determine the width).
    Question5. How do I set alignment of the column (left, right or center)?


    I browsed a lot of examples in the web but none in particular in VBA. There is no ListView1.Columns property in Excel 2007.

    Thanks.
    Last edited by ssal; Mar 3rd, 2015 at 09:34 PM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to set column width of a listview report/table?

    1. you can set the width of columns at design time, in the property pages (custom > columnheaders), or at runtime when adding columns, as per your code snippet, or at any time, using ListView1.ColumnHeaders(key).width = 70

    2. you can test the length of each item added, or when editing, then change the column width to suit

    3. see below, depends on the scalemode of the form or other object containing the listview

    5. columnheader alignment, either by code or when adding

    Add Method (ColumnHeaders Collection)


    Adds a ColumnHeader object to a ColumnHeaders collection in a ListView control.

    Syntax

    object.Add(index, key, text, width, alignment, icon)

    The Add method syntax has these parts:

    Part Description
    object Required. Anobject expression that evaluates to a ColumnHeaders collection.
    index Optional. An integer that uniquely identifies a member of an object collection.
    key Optional. A uniquestring expression that can be used to access a member of the collection.
    text Optional. A string that appears in the ColumnHeader object.
    width Optional. Anumeric expression specifying the width of the object using the scale units of the control's container.
    alignment Optional. An integer that determines the alignment of text in the ColumnHeader object. For settings, choose the Alignment property from the See Also list.
    icon Optional. The key or index of an image in the Smallicons ImageList.


    Remarks

    The Add method returns a reference to the newly inserted ColumnHeader object.

    Use the index argument to insert a column header in a specific position in the ColumnHeaders collection.

    When the members of a ColumnHeaders collection can change dynamically, you may want to reference them using the Key property, because the Index property for any ColumnHeader object may be changing.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2015
    Posts
    51

    Re: How to set column width of a listview report/table?

    Thanks.
    Last edited by ssal; Mar 4th, 2015 at 04:12 PM.

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