Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 46
Default Global definitions

Hi,

I am using a userform that usings the same defined variables, i.e.
worksheets for several different command buttons.

How do I set up these definitions as 'global' (if thats the right term) so
that I don't have to repeat the definitions for each command_button_click sub?

Example Code:-
Sub UserForm1_Load()

Dim CostWk As Worksheet
Set CostWk = Worksheets("JCOST-ALL")

Dim Costs As String
Costs = "JCOST-ALL"

Dim Rev As String
Rev = "JREV-ALL"


End Sub

Sub CommandButton1_Click()

Dim WBName As String
WBName = ActiveWorkbook.Name

Dim CostWk As Worksheet
Set CostWk = Worksheets("JCOST-ALL")

Dim Costs As String
Costs = "JCOST-ALL"


Application.Sheets(Costs).Visible = True


CostWk.Range("A2:J2").End(xlDown).ClearContents
Windows("Worksheet in Basis (1)").Activate
Range("A2:J2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows(WBName).Activate
CostWk.Select
Range("A2").Select
ActiveSheet.Paste

ActiveWindow.WindowState = xlMaximized

Sheets("Front").Select

Application.Sheets(Costs).Visible = False

End Sub

Sub CommandButton2_Click()

Dim WBName As String
WBName = ActiveWorkbook.Name

Dim RevWk As Worksheet
Set RevWk = Worksheets("JREV-ALL")

Dim Rev As String
Rev = "JREV-ALL"


Application.Sheets(Rev).Visible = True


RevWk.Range("A2:J2").End(xlDown).ClearContents
Windows("Worksheet in Basis (1)").Activate
Range("A2:J2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows(WBName).Activate
RevWk.Select
Range("A2").Select
ActiveSheet.Paste

ActiveWindow.WindowState = xlMaximized

Sheets("Front").Select

Application.Sheets(Rev).Visible = False

End Sub

Sub CommandButton3_Click()

Application.Sheets(Costs).Visible = True

End Sub

Sub CommandButton4_Click()

Application.Sheets(Rev).Visible = True

End Sub



Also, any tips on the above would be appreciated.


TIA
Matt
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,117
Default Global definitions

yes, you can definitely do this.
i have taken to setting up a separate module in any large project
called "GlobalMods".
in it i declare all my variables that i want to use throughout the
project.
you don't "dim" them, though, you "public" them..........

instead of
Dim r as Range
you would write
Public r as Range
for all the variables.......... anything that you would normally "dim".
for command buttons or userform things you use:

Public txtName as Control
Public optChoose as Control
etc.

important - when you write the sub in another module, though, you have
to write

Public Sub copytext()

otherwise it doesn't know where to find the variable, because it's
looking for it in that specific module.
hope this helps!
susan

Matthew Balch wrote:
Hi,

I am using a userform that usings the same defined variables, i.e.
worksheets for several different command buttons.

How do I set up these definitions as 'global' (if thats the right term) so
that I don't have to repeat the definitions for each command_button_click sub?

Example Code:-
Sub UserForm1_Load()

Dim CostWk As Worksheet
Set CostWk = Worksheets("JCOST-ALL")

Dim Costs As String
Costs = "JCOST-ALL"

Dim Rev As String
Rev = "JREV-ALL"


End Sub

Sub CommandButton1_Click()

Dim WBName As String
WBName = ActiveWorkbook.Name

Dim CostWk As Worksheet
Set CostWk = Worksheets("JCOST-ALL")

Dim Costs As String
Costs = "JCOST-ALL"


Application.Sheets(Costs).Visible = True


CostWk.Range("A2:J2").End(xlDown).ClearContents
Windows("Worksheet in Basis (1)").Activate
Range("A2:J2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows(WBName).Activate
CostWk.Select
Range("A2").Select
ActiveSheet.Paste

ActiveWindow.WindowState = xlMaximized

Sheets("Front").Select

Application.Sheets(Costs).Visible = False

End Sub

Sub CommandButton2_Click()

Dim WBName As String
WBName = ActiveWorkbook.Name

Dim RevWk As Worksheet
Set RevWk = Worksheets("JREV-ALL")

Dim Rev As String
Rev = "JREV-ALL"


Application.Sheets(Rev).Visible = True


RevWk.Range("A2:J2").End(xlDown).ClearContents
Windows("Worksheet in Basis (1)").Activate
Range("A2:J2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows(WBName).Activate
RevWk.Select
Range("A2").Select
ActiveSheet.Paste

ActiveWindow.WindowState = xlMaximized

Sheets("Front").Select

Application.Sheets(Rev).Visible = False

End Sub

Sub CommandButton3_Click()

Application.Sheets(Costs).Visible = True

End Sub

Sub CommandButton4_Click()

Application.Sheets(Rev).Visible = True

End Sub



Also, any tips on the above would be appreciated.


TIA
Matt


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
xl* object definitions ?? Bill Case Excel Programming 3 August 31st 06 09:36 PM
Trouble with 2 range definitions ... Celt[_39_] Excel Programming 7 April 5th 06 04:45 PM
Definitions Nick Excel Discussion (Misc queries) 1 July 1st 05 09:25 AM
Function Definitions Andrew Edmunds Excel Programming 1 January 8th 04 10:56 AM
Operator definitions Niklas[_2_] Excel Programming 2 July 25th 03 01:09 PM


All times are GMT +1. The time now is 04:17 AM.

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

About Us

"It's about Microsoft Excel"