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.
Declare the CONSTANT values outside of the individual macros.
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?Public Const LastRow As Long = 1
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon 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!)
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.
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.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon 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!)
I'm attaching the file with a brief explanation on it.
Thanks for your patient.
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.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
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:
Try it, open this book, turn everything the wrong way, save it, then reopen it.Option Explicit Private Sub auto_open() Sheets("Form").Range("AA1:AA4").Value = True End Sub
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...
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon 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!)
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.
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.
to force the control setting on open you can just use
Thisworkbook code module
Private Sub Workbook_Open() With Sheet1 .obes_sa1.Value = True .obsa1.Value = True .cbme1.Value = True .cbme2.Value = True End With End Sub
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)
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon 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!)
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks