ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Scope of Variables (https://www.excelbanter.com/excel-discussion-misc-queries/204451-scope-variables.html)

leerem

Scope of Variables
 
I pose a question for a better understanding of variables within my
spreadsheet.
I have declared within "ThisWorkbook"

Public Sub Workbook_Open()
Public ver, Report, Week, Period
End Sub

Within Module 1, Sub Report1
I have given the variabes values
Ver = 4
Report = "Report Week 1.xls"
Week = 1
Period = 1

call Reports

Sub Reports

Routine that calls upon the variables mentioned above,
eg. Workbooks.Add
ActiveWorkbook.SaveAs Filename:="D:\Store Returns\Reports\" &
Report, _
FileFormat:=xlNormal, CreateBackup:=False

More code

Windows("Poor Store Returns Ver " & ver & " 2003.xls").Activate


but when run the variables are empty. Can anyone please help me to better
understand where I'm going wrong.

Regards
Lee



Mike H

Scope of Variables
 
Try this,

Paste into a module and you'll see how the values for the variable are
passed to the testvariables sub.

Public ver As Long, Report As String, Week As Long, Period As Long
Sub SetVariables()
ver = 4
Report = "Report Week 1.xls"
Week = 1
Period = 1
testvariables
End Sub

Sub testvariables()
MsgBox ver
MsgBox Report
MsgBox Week
MsgBox Period
End Sub

Mike

"leerem" wrote:

I pose a question for a better understanding of variables within my
spreadsheet.
I have declared within "ThisWorkbook"

Public Sub Workbook_Open()
Public ver, Report, Week, Period
End Sub

Within Module 1, Sub Report1
I have given the variabes values
Ver = 4
Report = "Report Week 1.xls"
Week = 1
Period = 1

call Reports

Sub Reports

Routine that calls upon the variables mentioned above,
eg. Workbooks.Add
ActiveWorkbook.SaveAs Filename:="D:\Store Returns\Reports\" &
Report, _
FileFormat:=xlNormal, CreateBackup:=False

More code

Windows("Poor Store Returns Ver " & ver & " 2003.xls").Activate


but when run the variables are empty. Can anyone please help me to better
understand where I'm going wrong.

Regards
Lee



leerem

Scope of Variables
 
I pasted the code into Module 1 which placed into the Declarations section
Public ver as long ect

I ran the testvariables and all values came back as either blank or zero

Regards


"Mike H" wrote:

Try this,

Paste into a module and you'll see how the values for the variable are
passed to the testvariables sub.

Public ver As Long, Report As String, Week As Long, Period As Long
Sub SetVariables()
ver = 4
Report = "Report Week 1.xls"
Week = 1
Period = 1
testvariables
End Sub

Sub testvariables()
MsgBox ver
MsgBox Report
MsgBox Week
MsgBox Period
End Sub

Mike

"leerem" wrote:

I pose a question for a better understanding of variables within my
spreadsheet.
I have declared within "ThisWorkbook"

Public Sub Workbook_Open()
Public ver, Report, Week, Period
End Sub

Within Module 1, Sub Report1
I have given the variabes values
Ver = 4
Report = "Report Week 1.xls"
Week = 1
Period = 1

call Reports

Sub Reports

Routine that calls upon the variables mentioned above,
eg. Workbooks.Add
ActiveWorkbook.SaveAs Filename:="D:\Store Returns\Reports\" &
Report, _
FileFormat:=xlNormal, CreateBackup:=False

More code

Windows("Poor Store Returns Ver " & ver & " 2003.xls").Activate


but when run the variables are empty. Can anyone please help me to better
understand where I'm going wrong.

Regards
Lee



leerem

Scope of Variables
 
My mistake when I run the Setvariables routine the message boxes work,
however how can I set the variables in one subroutine and access from another
if they are in separte modules

"leerem" wrote:

I pasted the code into Module 1 which placed into the Declarations section
Public ver as long ect

I ran the testvariables and all values came back as either blank or zero

Regards


"Mike H" wrote:

Try this,

Paste into a module and you'll see how the values for the variable are
passed to the testvariables sub.

Public ver As Long, Report As String, Week As Long, Period As Long
Sub SetVariables()
ver = 4
Report = "Report Week 1.xls"
Week = 1
Period = 1
testvariables
End Sub

Sub testvariables()
MsgBox ver
MsgBox Report
MsgBox Week
MsgBox Period
End Sub

Mike

"leerem" wrote:

I pose a question for a better understanding of variables within my
spreadsheet.
I have declared within "ThisWorkbook"

Public Sub Workbook_Open()
Public ver, Report, Week, Period
End Sub

Within Module 1, Sub Report1
I have given the variabes values
Ver = 4
Report = "Report Week 1.xls"
Week = 1
Period = 1

call Reports

Sub Reports

Routine that calls upon the variables mentioned above,
eg. Workbooks.Add
ActiveWorkbook.SaveAs Filename:="D:\Store Returns\Reports\" &
Report, _
FileFormat:=xlNormal, CreateBackup:=False

More code

Windows("Poor Store Returns Ver " & ver & " 2003.xls").Activate


but when run the variables are empty. Can anyone please help me to better
understand where I'm going wrong.

Regards
Lee



Mike H

Scope of Variables
 
leerem,

Once the variables are declared public in this way they can be passed
between different modules. Try it, put the testvariables() sub I gave you in
a new module and you'll find it works.

Mike

"leerem" wrote:

My mistake when I run the Setvariables routine the message boxes work,
however how can I set the variables in one subroutine and access from another
if they are in separte modules

"leerem" wrote:

I pasted the code into Module 1 which placed into the Declarations section
Public ver as long ect

I ran the testvariables and all values came back as either blank or zero

Regards


"Mike H" wrote:

Try this,

Paste into a module and you'll see how the values for the variable are
passed to the testvariables sub.

Public ver As Long, Report As String, Week As Long, Period As Long
Sub SetVariables()
ver = 4
Report = "Report Week 1.xls"
Week = 1
Period = 1
testvariables
End Sub

Sub testvariables()
MsgBox ver
MsgBox Report
MsgBox Week
MsgBox Period
End Sub

Mike

"leerem" wrote:

I pose a question for a better understanding of variables within my
spreadsheet.
I have declared within "ThisWorkbook"

Public Sub Workbook_Open()
Public ver, Report, Week, Period
End Sub

Within Module 1, Sub Report1
I have given the variabes values
Ver = 4
Report = "Report Week 1.xls"
Week = 1
Period = 1

call Reports

Sub Reports

Routine that calls upon the variables mentioned above,
eg. Workbooks.Add
ActiveWorkbook.SaveAs Filename:="D:\Store Returns\Reports\" &
Report, _
FileFormat:=xlNormal, CreateBackup:=False

More code

Windows("Poor Store Returns Ver " & ver & " 2003.xls").Activate


but when run the variables are empty. Can anyone please help me to better
understand where I'm going wrong.

Regards
Lee




All times are GMT +1. The time now is 11:50 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com