+ Reply to Thread
Results 1 to 23 of 23

Reference ListView Selection in MsgBox

  1. #1
    Forum Contributor
    Join Date
    08-17-2009
    Location
    West Midlands
    MS-Off Ver
    Excel 2016
    Posts
    213

    Reference ListView Selection in MsgBox

    Hi folks,

    A simple one? I'm trying to reference a ListView selection in a MsgBox but I can't seem to get it to work. I've tried:

    Please Login or Register  to view this content.
    If I remove the ListView section it works fine.

    Any ideas?

    SA
    Last edited by SAsplin; 01-10-2013 at 11:57 AM.

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Reference ListView Selection in MsgBox

    Try replacing

    Please Login or Register  to view this content.
    with

    Please Login or Register  to view this content.
    If you like my contribution click the star icon!

  3. #3
    Forum Contributor
    Join Date
    08-17-2009
    Location
    West Midlands
    MS-Off Ver
    Excel 2016
    Posts
    213

    Re: Reference ListView Selection in MsgBox

    OllieB - thanks for the reply. I did try that before but it returns a run time error (91):

    Object variable or With block variable not set

    SA

  4. #4
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Reference ListView Selection in MsgBox

    Are you running the statement from a routine within the form? If not you may need to add a reference to the form like in the example below

    Please Login or Register  to view this content.
    if you are running the statement from within a form, the reference is not needed (at most use "Me."). Are you (in that case) sure about the name of the ListView object, i.e. ListView1?

  5. #5
    Forum Contributor
    Join Date
    08-17-2009
    Location
    West Midlands
    MS-Off Ver
    Excel 2016
    Posts
    213

    Re: Reference ListView Selection in MsgBox

    I've have tried changing the code to

    Please Login or Register  to view this content.
    and

    Please Login or Register  to view this content.
    These both return the same run time error (91). The statement is being run from within a form and the ListView object is definitely called ListView1 - the vb editor picks it up as you enter the code.

    I include the full code for the event below:

    Please Login or Register  to view this content.

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Reference ListView Selection in MsgBox

    Please Login or Register  to view this content.
    is what Ollie B meant for you to use I believe
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  7. #7
    Forum Contributor
    Join Date
    08-17-2009
    Location
    West Midlands
    MS-Off Ver
    Excel 2016
    Posts
    213

    Re: Reference ListView Selection in MsgBox

    Thanks JosephP. I did try that variation too and it also returns the run time error.

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Reference ListView Selection in MsgBox

    can you post a file-that code works perfectly here

  9. #9
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Reference ListView Selection in MsgBox

    Maybe there is an action just before the message box is displayed that invalidates the SelectedItem property (like a refresh/reload or something)?

  10. #10
    Forum Contributor
    Join Date
    08-17-2009
    Location
    West Midlands
    MS-Off Ver
    Excel 2016
    Posts
    213

    Re: Reference ListView Selection in MsgBox

    Unfortunately, my company's security measures won't let me upload any files. The code I posted is attached to a standard command button and the ListView object is version 6.0.

  11. #11
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Reference ListView Selection in MsgBox

    Under certain conditions you execute a refresh before displaying the message, hence there will no longer be a selecteditem. Save the value of SelectedItem at the beginning of your routine and use that variable in your message

  12. #12
    Forum Contributor
    Join Date
    08-17-2009
    Location
    West Midlands
    MS-Off Ver
    Excel 2016
    Posts
    213

    Re: Reference ListView Selection in MsgBox

    I posted the full code for the button at post #5.

    I removed the ListView.Refresh line before the MsgBox but still getting the run time error.

  13. #13
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Reference ListView Selection in MsgBox

    Can you check the code below

    Please Login or Register  to view this content.

  14. #14
    Forum Contributor
    Join Date
    08-17-2009
    Location
    West Midlands
    MS-Off Ver
    Excel 2016
    Posts
    213

    Re: Reference ListView Selection in MsgBox

    That returns the run time error (91) on line

    Please Login or Register  to view this content.
    I promise you the object is called ListView1!!

  15. #15
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Reference ListView Selection in MsgBox

    Try removing ".Text", just for arguments sake

  16. #16
    Forum Contributor
    Join Date
    08-17-2009
    Location
    West Midlands
    MS-Off Ver
    Excel 2016
    Posts
    213

    Re: Reference ListView Selection in MsgBox

    Same run time error. I'm beginning to wonder if I really need a MsgBox here at all!

  17. #17
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Reference ListView Selection in MsgBox

    does
    Please Login or Register  to view this content.
    work?

  18. #18
    Forum Contributor
    Join Date
    08-17-2009
    Location
    West Midlands
    MS-Off Ver
    Excel 2016
    Posts
    213

    Re: Reference ListView Selection in MsgBox

    JosephP - yup. If I add that line of code I get the result 0. No run time error though!

    Sorry - empty ListView. I get a value returned in relation to the number of entries in the ListView box.
    Last edited by SAsplin; 01-10-2013 at 11:29 AM.

  19. #19
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Reference ListView Selection in MsgBox

    perhaps a brute force approach
    Please Login or Register  to view this content.

  20. #20
    Forum Contributor
    Join Date
    08-17-2009
    Location
    West Midlands
    MS-Off Ver
    Excel 2016
    Posts
    213

    Re: Reference ListView Selection in MsgBox

    That's only gone and done it! JosephP you're a veritable genius. Many thanks - I was beginning to despair! I'm more than happy to add to your rep for the time and effort you've put in on this one.

    Thanks again!

  21. #21
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Reference ListView Selection in MsgBox

    you're welcome :-)

    thank you for the rep

  22. #22
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Just out of curiosity, does this,
    Please Login or Register  to view this content.
    or this,
    Please Login or Register  to view this content.
    return anything.
    If posting code please use code tags, see here.

  23. #23
    Forum Contributor
    Join Date
    08-17-2009
    Location
    West Midlands
    MS-Off Ver
    Excel 2016
    Posts
    213

    Re: Reference ListView Selection in MsgBox

    Hi Norie,

    .Key returns a blank value and .Index returns 1

    SA

+ 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