Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Global Variables using User Forms

Is there a way to set global variables so I can use them
in while I am coding in a user form. Right now I am
setting the variable up under the workbook and it works in
any worksheet, however when I try to access that data in a
user form opened from the worksheet, it does not work.
Any Ideas?

Thanks,
Steve
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Global Variables using User Forms

This code belongs in the Workbook module:

Public vTest As String
Sub The_Test()
vTest = "Can you see me?"
UserForm1.Show
End Sub

This code belongs in the UserForm1 module:

Private Sub UserForm_Initialize()
UserForm1.Caption = ThisWorkbook.vTest
End Sub

You need to publicly declare it at the module level, and
in the form - qualify the module with the variable. So to
access functions and variables from a UserForm, explicitly
qualify with "ThisWorkbook" or whatever module it is.

HTH.

-Brad
-----Original Message-----
Is there a way to set global variables so I can use them
in while I am coding in a user form. Right now I am
setting the variable up under the workbook and it works

in
any worksheet, however when I try to access that data in

a
user form opened from the worksheet, it does not work.
Any Ideas?

Thanks,
Steve
.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Global Variables using User Forms

If just using it within you project,
better in my opinion would be to declare it in a general module

General Module: (Insert = Module in the VBE)
Public vTest as String

ThisWorkbook module
Sub The_Test()
vTest = "Can you see me?"
UserForm1.Show
End Sub

Userform Module
Private Sub UserForm_Initialize()
UserForm1.Caption = vTest
End Sub


Then you don't have to worry about qualifying it and all elements of the
Project can see it (without qualifying it)

--
Regards,
Tom Ogilvy

"Brad" wrote in message
...
This code belongs in the Workbook module:

Public vTest As String
Sub The_Test()
vTest = "Can you see me?"
UserForm1.Show
End Sub

This code belongs in the UserForm1 module:

Private Sub UserForm_Initialize()
UserForm1.Caption = ThisWorkbook.vTest
End Sub

You need to publicly declare it at the module level, and
in the form - qualify the module with the variable. So to
access functions and variables from a UserForm, explicitly
qualify with "ThisWorkbook" or whatever module it is.

HTH.

-Brad
-----Original Message-----
Is there a way to set global variables so I can use them
in while I am coding in a user form. Right now I am
setting the variable up under the workbook and it works

in
any worksheet, however when I try to access that data in

a
user form opened from the worksheet, it does not work.
Any Ideas?

Thanks,
Steve
.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Global Variables using User Forms

Neat. I thought all modules had the same scope.

-Brad
-----Original Message-----
If just using it within you project,
better in my opinion would be to declare it in a general

module

General Module: (Insert = Module in the VBE)
Public vTest as String

ThisWorkbook module
Sub The_Test()
vTest = "Can you see me?"
UserForm1.Show
End Sub

Userform Module
Private Sub UserForm_Initialize()
UserForm1.Caption = vTest
End Sub


Then you don't have to worry about qualifying it and all

elements of the
Project can see it (without qualifying it)

--
Regards,
Tom Ogilvy

"Brad" wrote in

message
...
This code belongs in the Workbook module:

Public vTest As String
Sub The_Test()
vTest = "Can you see me?"
UserForm1.Show
End Sub

This code belongs in the UserForm1 module:

Private Sub UserForm_Initialize()
UserForm1.Caption = ThisWorkbook.vTest
End Sub

You need to publicly declare it at the module level, and
in the form - qualify the module with the variable. So

to
access functions and variables from a UserForm,

explicitly
qualify with "ThisWorkbook" or whatever module it is.

HTH.

-Brad
-----Original Message-----
Is there a way to set global variables so I can use

them
in while I am coding in a user form. Right now I am
setting the variable up under the workbook and it works

in
any worksheet, however when I try to access that data

in
a
user form opened from the worksheet, it does not work.
Any Ideas?

Thanks,
Steve
.



.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Global Variables using User Forms

Class modules (worksheet modules, userform module, thisworkbook module and
class modules) are private by default.

--
Regards,
Tom Ogilvy

Brad wrote in message
...
Neat. I thought all modules had the same scope.

-Brad
-----Original Message-----
If just using it within you project,
better in my opinion would be to declare it in a general

module

General Module: (Insert = Module in the VBE)
Public vTest as String

ThisWorkbook module
Sub The_Test()
vTest = "Can you see me?"
UserForm1.Show
End Sub

Userform Module
Private Sub UserForm_Initialize()
UserForm1.Caption = vTest
End Sub


Then you don't have to worry about qualifying it and all

elements of the
Project can see it (without qualifying it)

--
Regards,
Tom Ogilvy

"Brad" wrote in

message
...
This code belongs in the Workbook module:

Public vTest As String
Sub The_Test()
vTest = "Can you see me?"
UserForm1.Show
End Sub

This code belongs in the UserForm1 module:

Private Sub UserForm_Initialize()
UserForm1.Caption = ThisWorkbook.vTest
End Sub

You need to publicly declare it at the module level, and
in the form - qualify the module with the variable. So

to
access functions and variables from a UserForm,

explicitly
qualify with "ThisWorkbook" or whatever module it is.

HTH.

-Brad
-----Original Message-----
Is there a way to set global variables so I can use

them
in while I am coding in a user form. Right now I am
setting the variable up under the workbook and it works
in
any worksheet, however when I try to access that data

in
a
user form opened from the worksheet, it does not work.
Any Ideas?

Thanks,
Steve
.



.





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
New to User Forms Stacy New Users to Excel 2 July 30th 08 04:26 PM
User Forms Joanne New Users to Excel 18 July 5th 07 12:42 AM
User Defined Functions - local or global? Simon Excel Worksheet Functions 5 September 27th 06 09:15 AM
Global Variables Not Accessible John Baker Excel Programming 5 November 28th 03 01:23 AM
Declaring Global Variables skmr3 Excel Programming 1 July 14th 03 05:54 AM


All times are GMT +1. The time now is 07:19 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"