+ Reply to Thread
Results 1 to 11 of 11

Populate Excel Listview Control

  1. #1
    Registered User
    Join Date
    10-11-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    59

    Populate Excel Listview Control

    Hello all,

    I am struggling a bit with the populating and double click even of a Listview control. I have very little experience with the Listview control.

    I would like to populate the control with the table in sheet1 - I have very little idea of how this is done. Then once the control is envoked, I would like it so that when a user double clicks on a cell in the control, the value of that cell will appear in range("a1") of sheet1 - I am not even certain if the second part is possible. Thank you for any help.

    Please see attached. Below is the code I have so far.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Evagrius; 11-01-2009 at 05:16 PM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Populate Excel Listview Control

    I confess I've never used this control so I suspect the below is wrong (or at best poorly coded)

    Please Login or Register  to view this content.
    That being said the only cell that can be double clicked using the above is the main item (ie the year) - the sub items are not seemingly available.

    EDIT:
    In regard to detecting a sub item being clicked see: http://www.developerfusion.com/code/...ick-detection/
    (given functionality restrictions you might also want to consider a multi column list box given far simpler to populate ?)
    Last edited by DonkeyOte; 11-01-2009 at 06:23 AM.

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Populate Excel Listview Control

    Having nerded around for a while I think there is another alternative and that is to utilise the OWC Spreadsheet Control in your User Form.

    Your profile states 2003 so you should find you have the Microsoft Spreadsheet Control available via Additional Controls... if you were running 2007 and were rolling this out across multiple clients you would have an issue as OWC is not installed as part of 2007 - development has ceased ... and as such you would need to install whichever version of OWC you utilise on each client ... in terms of versions I suspect you will have 9.0/10.0 listed however I've come across some posts by XL-Dennis (XL genius) that state both to be buggy and 11.0 to be more stable -- however if 11.0 is not available on your machine by default you face the same problem as if you were on XL2007 (ie installing on each client).

    Assuming using the Control itself is not an issue per se (ie controls available on clients by default) you could then create a User Form with this Control in place (sized appropriately) and then use the following code:

    Please Login or Register  to view this content.
    The above (10.0) should generate your range in a spreadsheet control ... double clicking any cell within that range should update A1 with the associated value accordingly...
    Downside compared to 11.0 (as annotated) is that for whatever reason the viewonlymode fails in 10.0 (commented out on that basis) - and as such I have added the StartEdit event to cancel (in effect enforcing read-only)
    Last edited by DonkeyOte; 11-01-2009 at 08:49 AM. Reason: added links

  4. #4
    Registered User
    Join Date
    10-11-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    59

    Re: Populate Excel Listview Control

    Hi DonkeyOte,

    Thank you for being so generous with your time - your answer and guidance are far more than I had hoped for.

    I tested your first code and of course it worked flawlessly; the issue then remains with the inability to detect which cell on the control was clicked.

    The truth is the listview control was my second choice; I had tried the spreadsheet control, however, when I sent the workbook to a colleague at work, they were unable to load the form and so I reverted to the Listview.

    Your idea for a multi-column listbox seems to be the most practical method.
    Using a multi-column listbox, will the user be able to click on a single item? If you have the time and it is not an imposition given all the help you have provided so far, would you mind a small demo code since I have never worked with a multi-column list box. Thank you again DonkeyOte.

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Populate Excel Listview Control

    Quote Originally Posted by Evagrius
    Using a multi-column listbox, will the user be able to click on a single item?
    The million dollar question... no, or at least I don't think so...

    It looks, to me at least, that if you need to be able to select single cells in a matrix and you can not use the Spreadsheet control then you will need to pursue the earlier link re: capturing mouse position etc .. either that or think about building your own ... ie adding a grid of labels on the fly to your UserForm, populating each with the range value ... adding all labels to a collection and using a Class Module to handle the click event for all labels ... all of which I say that with zero expertise personally.

    All that being said... as I made mention earlier I rarely use User Forms and even rarer would I use anything more complex than a ComoBox so I suspect I'm probably wrong


    EDIT:
    I should have added - the reason I suggested the ListBox as an alternative to the ListView was simply because given both have the same constraints regards single cell selection (ie neither can), you might as well use the ListBox given it's far simpler to populate.
    (and is a default control)
    Last edited by DonkeyOte; 11-01-2009 at 11:21 AM.

  6. #6
    Registered User
    Join Date
    10-11-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    59

    Re: Populate Excel Listview Control

    I see - thank you for your effort. I have one last small question. I did learn from you a very useful short cut. In the first post of this thread I had this:

    Please Login or Register  to view this content.
    and then I would loop through the elements of that array. You, on the other hand, did it this way - which I had never done before and is far simpler.

    Please Login or Register  to view this content.
    And then you looped through the elements of that list. Thanks for that lesson! My question is, since all the arrays in your code have one dimension, and the default for the dimension declaration is 1, why is it then when I remove the 1 from vHdrs(IngCol,1) I get an out of range error? The line I reference is below.

    Please Login or Register  to view this content.
    Thank you DonkeyOte

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Populate Excel Listview Control

    If you step through the code and have the Locals Window open (View -> Locals) you will find that the Variant Array will be listed as

    Please Login or Register  to view this content.
    although there is only 1 column the Array is still 2-dimensional...

    and though

    Please Login or Register  to view this content.
    will return 5 this is because with UBound function the Dimension parameter is optional (default of 1st dimension).

    However, when referencing a specific point within the 2 dimensional array, eg:

    Please Login or Register  to view this content.
    you will need to specify values for both dimensions

    Please Login or Register  to view this content.
    If you were to transpose the Horizontal source range twice when first populating the variant array, ie:

    Please Login or Register  to view this content.
    You will find vHdrs becomes a single dimension array

    Please Login or Register  to view this content.
    at which point

    Please Login or Register  to view this content.
    would work (and the 2 dimensional syntax would not)

    I would confess Arrays are not a strong point of mine and I've no doubt someone here can explain the above more thoroughly for you... if they're willing ...
    Last edited by DonkeyOte; 11-01-2009 at 01:04 PM.

  8. #8
    Registered User
    Join Date
    10-11-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    59

    Re: Populate Excel Listview Control

    Thanks again DonkeyOte - You've been immensely helpful. Arrays always leave me annoyed

    By the way, I am actually on page 98 of Don Quixote

    I read an abridged version when I was in high school a while back and I had promised myself to read the full version one day. I tried to get my wife to read it but the 1100 pages scared her away

  9. #9
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Populate Excel Listview Control

    Quote Originally Posted by DonkeyOte View Post
    It looks, to me at least, that if you need to be able to select single cells in a matrix and you can not use the Spreadsheet control then you will need to pursue the earlier link re: capturing mouse position etc .. either that or think about building your own ... ie adding a grid of labels on the fly to your UserForm, populating each with the range value ... adding all labels to a collection and using a Class Module to handle the click event for all labels ... all of which I say that with zero expertise personally.
    Below would be my attempt at the "build the control on the fly" approach ...
    to which end there are same pretty basic assumptions...

    a) source table / data is a contiguous range
    b) all you want to do is output a value that is double clicked.

    First I would create a UserForm - for sake of demo (given the no. of variants I have in the file) let's call this UserForm4 ... this User Form will not have any controls added to it manually.

    Next insert a Class Module into your file, name that Module (via Properties [F4]): CTBEvents

    Into that Class Module add the following code:

    Please Login or Register  to view this content.
    Now back to the User Form Object - add the below code

    Please Login or Register  to view this content.
    If you now create a Button to show the above UserForm you should find you're presented with a grid of textboxes (all locked), if you double click on any of the said text boxes (other than headers) A1 will populate accordingly.

    I hope that helps.

  10. #10
    Registered User
    Join Date
    10-11-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    59

    Re: Populate Excel Listview Control

    DonkeyOte - you sir are a LEGEND!

    After I followed your instructions and nervously initialized the userform - forgive my nerdiness - but what appeared was beautiful! I know, I know, its only a grid

    For some reason Excel would not allow me to use CTBEvents so I used CTB only and that worked.

    I will most likely spend the rest of the day learning from what you have done. Class modules are foriegn to me and so are collections. If you know of any links or sources to help learn those I would be very grateful.

    Well done DonkeyOte - I am in your debt!
    Last edited by Evagrius; 11-01-2009 at 05:21 PM.

  11. #11
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Populate Excel Listview Control

    Class modules are foriegn to me and so are collections. If you know of any links or sources to help learn those I would be very grateful.
    I'm no expert either, indeed I adapted my code above from advice/code that had been provided to me previously by another member (romperstomper) elsewhere.

    In short you use both here because you have for ex. using the A3:F13 matrix as source - 66 textboxes to add to your userform dynamically, of which 50 need to have a double click event assigned to them and given all textboxes require the same double click event it makes sense to build "one routine to unite them all"...

    For more info. on Class Modules see:

    http://www.cpearson.com/excel/Classes.aspx
    http://puremis.net/excel/code/086.shtml

    For info. on Collection Object I would say that the VBA Help file is a good starting point.

+ 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