+ Reply to Thread
Results 1 to 9 of 9

[UserForm's Listbox]Why is sheetname empty when using it in another procedure?

  1. #1
    Forum Contributor
    Join Date
    06-20-2021
    Location
    UK
    MS-Off Ver
    2016
    Posts
    126

    [UserForm's Listbox]Why is sheetname empty when using it in another procedure?

    Problem: using listbox.value (for sheet name selection) in another procedure results in nothing?

    thwuserform.png

    Trying to use shtName selection in the list box to use it in another procedure but this is not returning anything after checking debugger. Full code at the end shows that I declared shtName as public and in declarations section (whole code behind userform).

    Please Login or Register  to view this content.
    After drilling down, I find shtName variable to be empty.

    Please Login or Register  to view this content.
    Things that I have tried:

    - Putting shtName var in declarations section: Public shtName as worksheet
    - Setting shtName to worksheet object using Set keyword.


    Is there any other listbox method I should be aware of?

    One other question if you could answer:

    • How to avoid standard message that states to clear clipboard (at the end of copyData procedure)? Programmatically.

    For reference, here is the full code:

    Please Login or Register  to view this content.
    Last edited by immigrated4urjob; 01-18-2022 at 03:59 AM.

  2. #2
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2503
    Posts
    1,485

    Re: [UserForm's Listbox]Why is sheetname empty when using it in another procedure?

    There are parts of your code where you refer to:
    Please Login or Register  to view this content.
    The above will not give a result as shthame has not been defined, could this typo be your issue?

    To clear the clipboard use the below after the paste line:
    Please Login or Register  to view this content.
    The other option for a variable could be 'Global' have you tried to declare as 'Global'?

    Hope this helps
    Last edited by CheeseSandwich; 01-18-2022 at 04:06 AM.

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: [UserForm's Listbox]Why is sheetname empty when using it in another procedure?

    Two words: Option Explicit.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Forum Contributor
    Join Date
    06-20-2021
    Location
    UK
    MS-Off Ver
    2016
    Posts
    126

    Re: [UserForm's Listbox]Why is sheetname empty when using it in another procedure?

    Quote Originally Posted by CheeseSandwich View Post
    There are parts of your code where you refer to:
    Please Login or Register  to view this content.
    The above will not give a result as shthame has not been defined, could this typo be your issue?

    To clear the clipboard use the below after the paste line:
    Please Login or Register  to view this content.
    The other option for a variable could be 'Global' have you tried to declare as 'Global'?

    Hope this helps
    Hi, using option explicit definitely helped and now the debugger problem narrows down to the line in red.
    Please Login or Register  to view this content.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: [UserForm's Listbox]Why is sheetname empty when using it in another procedure?

    Please Login or Register  to view this content.
    should be:

    Please Login or Register  to view this content.

  6. #6
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2503
    Posts
    1,485

    Re: [UserForm's Listbox]Why is sheetname empty when using it in another procedure?

    This one:
    Please Login or Register  to view this content.
    Should be:
    Please Login or Register  to view this content.
    Although I doubt this would have caused an error.

  7. #7
    Forum Contributor
    Join Date
    06-20-2021
    Location
    UK
    MS-Off Ver
    2016
    Posts
    126
    Quote Originally Posted by rorya View Post
    Please Login or Register  to view this content.
    should be:

    Please Login or Register  to view this content.
    I guess the question is that mainWb is implied and I don't need to reference it?

  8. #8
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2503
    Posts
    1,485

    Re: [UserForm's Listbox]Why is sheetname empty when using it in another procedure?

    Usually you would define the workbook first and then the sheet from that workbook, after that you only need to reference it as the worksheet. See below:

    Please Login or Register  to view this content.

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: [UserForm's Listbox]Why is sheetname empty when using it in another procedure?

    Quote Originally Posted by immigrated4urjob View Post
    I guess the question is that mainWb is implied and I don't need to reference it?
    When you assign a sheet to the variable, it is a specific sheet which can only be in one workbook, so there is no need to qualify it further. If you use MainWb.shtname you should get a 438 error since you are effectively trying to access a shtname property of a workbook, and that doesn't exist.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Userform Can't Open When ListBox is Empty
    By Matt W in forum Excel Programming / VBA / Macros
    Replies: 27
    Last Post: 09-14-2021, 12:39 PM
  2. userform listbox add empty cell to my list
    By mazan2010 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-17-2020, 06:12 PM
  3. [SOLVED] Selecting SheetName from Listbox
    By n4rs in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-15-2019, 11:26 PM
  4. Unable to get userform listbox items into next empty cell
    By neevee in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-07-2018, 02:18 PM
  5. [SOLVED] if anything in listbox select first item, if listbox empty do nothing (listbox in userform
    By mcdermott2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-17-2015, 12:49 PM
  6. Listbox to Listbox, no duplicates & submitting same UserForm data for each Listbox entry.
    By jamieswift1977 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-18-2012, 12:18 PM
  7. Use sheetname to open a Userform
    By aph in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-02-2007, 03:19 AM

Tags for this Thread

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