global variable?

robfo0

Active Member
Joined
Feb 19, 2002
Messages
281
Hi everyone,

Ive tried doing searches and looking in help, but i cant seem to get the job done.

I would like to define a variable:

currbook=activeworkbook.name

so that ALL modules in the book can use this variable.

I have tried using:
pulic currbook as string
dim currbook as string

then defining currbook in the workbook_open. Is this not the correct way to do this? Help!

Thanks :)
 
mark, thanks for the code, im awaiting your next post now. But, sorry if i was too vauge, but I already considered looping through the workbooks to test. The problem is, most of the time there are more than 2 workbooks open when the employees are working. there are MANY worksheets we use, and they are constantly used. I could ask the users to close all before opening a customer file, but expecting them to actually do it would be ridiculous on my part :)

In anycase, I'm sure your code will help, much appreciated.
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
OK, the second example and this is my preferred method.

From the previous example, replace all of the code in the module (Including the Public oWorkbook as Workbook statement) with this code:

<pre>

Public Sub FindWorkbook()
'Declare Variable locally
Dim oWorkbook As Workbook

If Workbooks.Count = 2 Then 'If there are only two workbooks open
For Each oWorkbook In Workbooks 'Cycle through the workbooks collection
If Not oWorkbook.Name = ThisWorkbook.Name Then 'If the name of the workbook does not equal thisworkbook, then we have found the other book
Exit For
End If
Next
Else
MsgBox "There is only one work book open.", vbInformation, "Workbook Information"
End If

DisplayWorkbookName oWorkbook 'Call sub routine in the other module and pass oWorkbook as the workbook the subroutine requires

End Sub</pre>

Now in replace the cod ein step 5 with this:

<pre>
Public Sub DisplayWorkbookName(ByRef oWorkbookDifferentVariableNameButStillSameObject As Workbook)
MsgBox oWorkbookDifferentVariableNameButStillSameObject.Name
End Sub</pre>

Now save (as a different name as the first example), close the book and then open it. The code will work just the same. the difference this time is that there are none of those horrible global variables.

The way the second one works is:

You declare the oWorkbook as a local variable, but the subroutine "DisplayWorkbookName" now requires a valid workbook to be passed to the subroutine as well (in this case oWorkbook). In the subroutine declaration for "DisplayWorkbookName" we basically tell the subroutine that it is to ask for a workbook to be provided for it to work with. i.e. the Byref....bit. Now I put in a long and stupid name just for emphasis "oWorkbookDifferentVariableNameButStillSameObject" but basically, we could have called this "oWorkbook" as well, but it would be a different variable to module 1's oWorkbook because both of the variables have been declared at the subroutine level.

Well, I hope this is what you need and I hop that this helps.

Cheers
 
Upvote 0
Just saw your post in between posts there.

When you say that there are going to be multiple files open, are you going to have to go through each one and extract data from all of them, or are you going to be looking for a particular workbook?
 
Upvote 0
Mark, I will be looking for a particular workbook, BUT, the workbook will have different names (these files i am trying to get data from are saved as customer names). There may be other workbooks open, such as a file search (excel file), or one of many others which i dont want to do anything with
 
Upvote 0
On 2002-04-19 15:36, robfo0 wrote:
ok, another related question. Is there a way that i can use this variable in modules from different workbooks? I tried putting a public in there too, but that didnt work.

I was unclear if your question was answered or not?
Anyway, in similiar situations, via shared drives, I have used "handshaking" text files to communicate with other projects. Not as sexy as DDE, .exe, or .dlls but it has always worked for me...
See a post for Ted in which one single value is used among multiple workbooks on seperate workstations, opened simultaneously...
http://www.mrexcel.com/board/viewtopic.php?topic=5803&forum=2&3
Maybe this will help you?
Tom
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,709
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top