+ Reply to Thread
Results 1 to 12 of 12

Public Variable between Sub and Userform

  1. #1
    Registered User
    Join Date
    06-19-2013
    Location
    Sheffield
    MS-Off Ver
    Excel 2013
    Posts
    86

    Public Variable between Sub and Userform

    Hello,

    I need to pass a variable and its value from the Sub where it is declared to a Userform.
    How can I do it? Here a simple example.

    Sub
    Please Login or Register  to view this content.
    Userform
    Please Login or Register  to view this content.

    Thanks in advance
    Regards

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,507

    Re: Public Variable between Sub and Userform

    Like this:

    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,632

    Re: Public Variable between Sub and Userform

    Please Login or Register  to view this content.
    If you want Red (public) Isplit to hold value of cell in row Rsplit, column C, then you cannot declare local variable (blue) with the samme name.

    Try:
    Please Login or Register  to view this content.
    Best Regards,

    Kaper

  4. #4
    Registered User
    Join Date
    06-19-2013
    Location
    Sheffield
    MS-Off Ver
    Excel 2013
    Posts
    86

    Re: Public Variable between Sub and Userform

    It works, thank you both.
    How about doing it the other way round? I mean passing a variable from the Userform to the Sub.
    Something like:

    Please Login or Register  to view this content.
    The problem is that when I go back to the Sub the Private Sub CommandButton1_Click() is terminated so the a value of "a" in Sub is empty.
    (I didnět declare "a" in the Sub).

    Thanks again

  5. #5
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,632

    Re: Public Variable between Sub and Userform

    declare:
    Please Login or Register  to view this content.
    in standard module (where you declared ISplit) , not in userform code

  6. #6
    Registered User
    Join Date
    06-19-2013
    Location
    Sheffield
    MS-Off Ver
    Excel 2013
    Posts
    86

    Re: Public Variable between Sub and Userform

    Works fine with: [seems there is no need of Public declarations for bringing a variable from userform to Sub, right?]:
    Sub (No Public declarations)

    Please Login or Register  to view this content.
    Userform
    Please Login or Register  to view this content.
    Works fine with:
    Sub
    Please Login or Register  to view this content.
    Userform
    Please Login or Register  to view this content.

    Not working with (equal to previous exchanging a with FSplit): [Error - Found non univocal name: FSplit]
    Sub
    Please Login or Register  to view this content.
    Userform
    Please Login or Register  to view this content.

    [Could you tell me the differences between the three methods?]

    Thanks!
    Last edited by BrianAll; 05-22-2018 at 04:56 AM.

  7. #7
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,632

    Re: Public Variable between Sub and Userform

    Brian,

    Please DO observe Rule 3 of https://www.excelforum.com/forum-rul...rum-rules.html

    Moreover, It's usually easier to respond if sample file is attached, not code alone.

  8. #8
    Registered User
    Join Date
    06-19-2013
    Location
    Sheffield
    MS-Off Ver
    Excel 2013
    Posts
    86

    Re: Public Variable between Sub and Userform

    Sorry, Kaper. I did it but I probably published an older version of the reply (made something messy with the preview).
    I've modified the reply with the code tags.
    Thanks again

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Public Variable between Sub and Userform

    Your first code will not work without a public declaration. The fact that your third code produces the error it does suggests that you do have FSplit declared as a Public variable somewhere else already.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  10. #10
    Registered User
    Join Date
    06-19-2013
    Location
    Sheffield
    MS-Off Ver
    Excel 2013
    Posts
    86

    Re: Public Variable between Sub and Userform

    Hi xlniwit, it really helped.
    Regards

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,507

    Re: Public Variable between Sub and Userform

    You're welcome.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  12. #12
    Registered User
    Join Date
    06-19-2013
    Location
    Sheffield
    MS-Off Ver
    Excel 2013
    Posts
    86

    Re: Public Variable between Sub and Userform

    Finally a quicker method to mark it as solved...
    I was already trying to figure out how the old one works...
    Thanks

+ 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] Public Variable and Static Variable Difference
    By zanshin777 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-18-2016, 01:58 AM
  2. [SOLVED] Public Variable
    By MarBoTJ in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-23-2015, 03:02 AM
  3. Userform not passing through 1 particular public variable
    By newbvba in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-20-2014, 09:59 PM
  4. Public Variable value
    By nfpaccounting in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-11-2011, 02:34 PM
  5. Losing public variable when submitting userform
    By JP Romano in forum Excel General
    Replies: 2
    Last Post: 01-29-2009, 11:39 AM
  6. Public Variable
    By Digitborn.com in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-09-2007, 03:54 PM
  7. [SOLVED] Pass public variable from one userform to a second...
    By Mike Dunworth in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-03-2005, 08:05 PM

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