+ Reply to Thread
Results 1 to 12 of 12

Passing variables and values between sheet code and module code

  1. #1
    Registered User
    Join Date
    05-13-2009
    Location
    Mexico City
    MS-Off Ver
    Excel 2003
    Posts
    8

    Thumbs up Passing variables and values between sheet code and module code

    hello:

    I really need your help.

    I have some buttons in different sheets in an excel file, each button has its own code, that is the reason I can not move the code related to each object to another location (sheet or module).

    And I have one piece of code in Module1 (Auto_load) in order to execute automatically this routine every time file is opened. Inside "auto_load" routine I initialize some values of some check buttons,options buttons and positions of some objects in diferent sheets, but I can not pass the value of variables between Module and Sheet's code even when I declare as public variables and/or function.

    I have the following structure:

    VBA Project
    ------Microsoft Excel Object
    ----------Sheet1 <-I have code here
    ----------Sheet2
    ----------Sheet3 <-I have code here
    ------Modules
    ----------Module1 <-I have code here
    ----------Module2
    ----------Module3

    How I can pass the value of some variables between Module and sheets??

    Thanks in advance.
    Last edited by imux; 05-14-2009 at 12:26 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Passing variables and values between sheet code and module code

    Declare the CONSTANT values outside of the individual macros.
    Please Login or Register  to view this content.
    This creates the reference and gives it a beginning value. You can update the constant's value in any other macro at any time, but its current value is available at all times. That may be oversimplified, but enough to get you started, maybe?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    05-13-2009
    Location
    Mexico City
    MS-Off Ver
    Excel 2003
    Posts
    8

    Question Re: Passing variables and values between sheet code and module code

    Hi again:

    Thanks for the help.
    I got an error, maybe I'm not using in a correct way your suggestion.

    I have two check buttons, and two option buttons in sheet1, named:

    cb1, cb2, op1 and op2, I would like initialize every time the file is opened to:

    cb1=Activated (1)
    cb2=Activated (1)
    ob1=Activated (true)
    ob2=Deactivated (false)

    and in the other hand I have the next piece of code in the module1 (in order to do this initialization):

    Sub auto_load()

    ob1 = True
    ob2 = False
    cb1 = 1
    cb2 = 1

    End Sub

    But, when I test the file, the check boxes and option buttons do not initialize in according with the preset.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Passing variables and values between sheet code and module code

    Click on GO ADVANCED and use the paperclip icon to upload your workbook. Point out the items in question in the workbook, I'll take a look.

  5. #5
    Registered User
    Join Date
    05-13-2009
    Location
    Mexico City
    MS-Off Ver
    Excel 2003
    Posts
    8

    Wink Re: Passing variables and values between sheet code and module code

    I'm attaching the file with a brief explanation on it.

    Thanks for your patient.
    Attached Files Attached Files

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Passing variables and values between sheet code and module code

    Public variables need to be declared in a regular old code module, not in an object module (Sheetx, ThisWorkbook, ...). Then they are visible throughout the project.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Passing variables and values between sheet code and module code

    There is something off about the entire Sheet "Forms" ...it doesn't behave in a way I can wrangle. See, the same idea that you're attempting I put on the attached sheet. This very simple "auto_open" module macro takes care of setting the buttons and checkboxes for me when the sheet is opened.

    I simply linked the objects to a range of cells, then set the value of the entire range to TRUE in the macro:
    Please Login or Register  to view this content.
    Try it, open this book, turn everything the wrong way, save it, then reopen it.

    this same techniques "sort of works" on your sheet, but when the sheet opens, it not only sets the items on that you want, it also leaves the alternate toggle buttons selected. Very odd...
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    05-13-2009
    Location
    Mexico City
    MS-Off Ver
    Excel 2003
    Posts
    8

    Thumbs up Re: Passing variables and values between sheet code and module code

    Thanks JBeaucaire, I implemented some kind of solution as you suggested, I had unmerged the "A" column in Sheet(1) and then hide it, I added the TRUE, TRUE, 1, 1 in the A1:A4 cells and then I linked to the objects by implementing your solution.

    It solved my problem for this case in particular, I'm using it now, but maybe in the future and in the process of development of my code maybe I'll find some issues more, and:

    Just a question: (sorry for to be obstinate)

    Exist one way to pass a value of variable between Modules (code) and Excel Object (code)? Or between an excel object and other one of the same type? I mean: pass values between a code in sheet1 and code in sheet2?

    merci

    Thanks to shg for the support provided.

  9. #9
    Registered User
    Join Date
    05-13-2009
    Location
    Mexico City
    MS-Off Ver
    Excel 2003
    Posts
    8

    Thumbs up Re: Passing variables and values between sheet code and module code

    Problem solved!!!

    Sorry I was confused, but with help of JBeaucaire and shg i solved my problem (pass values between sheets codes) even when this solution does not apply directly to the initialization of objects (I used the suggestion of JBeaucaire for this case in particular).

    I declare "Public Variables" in the module1 and the I used as I want in any part of the code independently in sheet1, sheet2...

    Thanks, for your help.

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Passing variables and values between sheet code and module code

    to force the control setting on open you can just use

    Thisworkbook code module

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Passing variables and values between sheet code and module code

    If that takes care of your need, be sure to EDIT your original post (Go Advanced) and mark the PREFIX box [SOLVED].






    (Also, use the "scales" icon in our posts to leave Feedback, it is appreciated)

  12. #12
    Registered User
    Join Date
    05-13-2009
    Location
    Mexico City
    MS-Off Ver
    Excel 2003
    Posts
    8

    Arrow Re: Passing variables and values between sheet code and module code

    Thanks for the comment of Andy Pope, that was a little more elegant solution, Wookbook_open() method, implemented.

    Anyway I have now a more clear idea about how I can handle the objects values by considering the lot of ideas everyone you provided to me, thanks to you, thank you so much.

    JBeaucaire
    shg
    Andy Pope

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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