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

hey

I have a variable(number) and want it to not die until the
workbook is closed. I need to be able to see the value of
the variable from a worksheet module. How would I do this
and what module would I put the variable in?

Thank you
Todd Huttenstine


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Static Variable

Todd,

Declare the variable as Public in any standard code module, above
and outside of any procedure. E.g.,

Public MyVar As Long


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Todd Huttenstine" wrote in
message ...
hey

I have a variable(number) and want it to not die until the
workbook is closed. I need to be able to see the value of
the variable from a worksheet module. How would I do this
and what module would I put the variable in?

Thank you
Todd Huttenstine




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 260
Default Static Variable

See I did that. Below code is in workbiook module.

Public Sub Workbook_BeforePrint(Cancel As Boolean)
Static PrintCounter
PrintCounter = PrintCounter + 1
MsgBox "print Job: " & PrintCounter
End Sub

Now the value for variable PrintCounter can be 5 but here
is the problem.

I click a print button on worksheets(5) and the code in it
is along the lines of:

If PrintCounter 0 then
else
End If


In this case, the code is seeing the value of PrintCounter
as "EMPTY" but the real value is 5. PrintCounter value is
still 5, but for some reason this code cant see that. How
do I correct this?



-----Original Message-----
Todd,

Declare the variable as Public in any standard code

module, above
and outside of any procedure. E.g.,

Public MyVar As Long


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Todd Huttenstine"

wrote in
message ...
hey

I have a variable(number) and want it to not die until

the
workbook is closed. I need to be able to see the value

of
the variable from a worksheet module. How would I do

this
and what module would I put the variable in?

Thank you
Todd Huttenstine




.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Static Variable

Todd,

Since you have declared the variable PrintCounter within the
BeforePrint procedure, it is visible only within that procedure.
It cannot be accessed by any other procedure. The declaration
'Static' means only that the value of the variable is retained
between calls to that procedure. It does not change the scope
(accessiblity) of the variable. If you want to be able to access
the contents of a variable from more than one procedure, you must
declare it as a Public variable in a standard code module.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Todd Huttenstine" wrote in
message ...
See I did that. Below code is in workbiook module.

Public Sub Workbook_BeforePrint(Cancel As Boolean)
Static PrintCounter
PrintCounter = PrintCounter + 1
MsgBox "print Job: " & PrintCounter
End Sub

Now the value for variable PrintCounter can be 5 but here
is the problem.

I click a print button on worksheets(5) and the code in it
is along the lines of:

If PrintCounter 0 then
else
End If


In this case, the code is seeing the value of PrintCounter
as "EMPTY" but the real value is 5. PrintCounter value is
still 5, but for some reason this code cant see that. How
do I correct this?



-----Original Message-----
Todd,

Declare the variable as Public in any standard code

module, above
and outside of any procedure. E.g.,

Public MyVar As Long


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Todd Huttenstine"

wrote in
message ...
hey

I have a variable(number) and want it to not die until

the
workbook is closed. I need to be able to see the value

of
the variable from a worksheet module. How would I do

this
and what module would I put the variable in?

Thank you
Todd Huttenstine




.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 260
Default Static Variable

Ahhh! Thank you.



-----Original Message-----
Todd,

Since you have declared the variable PrintCounter within

the
BeforePrint procedure, it is visible only within that

procedure.
It cannot be accessed by any other procedure. The

declaration
'Static' means only that the value of the variable is

retained
between calls to that procedure. It does not change the

scope
(accessiblity) of the variable. If you want to be able to

access
the contents of a variable from more than one procedure,

you must
declare it as a Public variable in a standard code module.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Todd Huttenstine"

wrote in
message ...
See I did that. Below code is in workbiook module.

Public Sub Workbook_BeforePrint(Cancel As Boolean)
Static PrintCounter
PrintCounter = PrintCounter + 1
MsgBox "print Job: " & PrintCounter
End Sub

Now the value for variable PrintCounter can be 5 but

here
is the problem.

I click a print button on worksheets(5) and the code in

it
is along the lines of:

If PrintCounter 0 then
else
End If


In this case, the code is seeing the value of

PrintCounter
as "EMPTY" but the real value is 5. PrintCounter value

is
still 5, but for some reason this code cant see that.

How
do I correct this?



-----Original Message-----
Todd,

Declare the variable as Public in any standard code

module, above
and outside of any procedure. E.g.,

Public MyVar As Long


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Todd Huttenstine"


wrote in
message ...
hey

I have a variable(number) and want it to not die

until
the
workbook is closed. I need to be able to see the

value
of
the variable from a worksheet module. How would I do

this
and what module would I put the variable in?

Thank you
Todd Huttenstine




.



.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Static Variable

I may be missing something, but what you have done is not what Chip
suggested. Your variable is not a Public variable and is not in a standard
module.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Todd Huttenstine" wrote in message
...
See I did that. Below code is in workbiook module.

Public Sub Workbook_BeforePrint(Cancel As Boolean)
Static PrintCounter
PrintCounter = PrintCounter + 1
MsgBox "print Job: " & PrintCounter
End Sub

Now the value for variable PrintCounter can be 5 but here
is the problem.

I click a print button on worksheets(5) and the code in it
is along the lines of:

If PrintCounter 0 then
else
End If


In this case, the code is seeing the value of PrintCounter
as "EMPTY" but the real value is 5. PrintCounter value is
still 5, but for some reason this code cant see that. How
do I correct this?



-----Original Message-----
Todd,

Declare the variable as Public in any standard code

module, above
and outside of any procedure. E.g.,

Public MyVar As Long


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Todd Huttenstine"

wrote in
message ...
hey

I have a variable(number) and want it to not die until

the
workbook is closed. I need to be able to see the value

of
the variable from a worksheet module. How would I do

this
and what module would I put the variable in?

Thank you
Todd Huttenstine




.



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 260
Default Static Variable

hey Bob

I created a standard module called PrintCount with a
procedure by the name of PrintCount as well. In the
procedure I put the following code:

Static PrintCounter

Dim RngCounter As Long
If PrintCounter 0 Then
RngCounter = Application.WorksheetFunction.CountA
(Worksheets("Wkly Renewals").Range("D:D"))
With CreateObject("Wscript.Shell")
.Popup "The name of the active printer is " &
Application.ActivePrinter _
, 1, "Print Confirmation", 64
End With
Worksheets("Wkly Renewals").Range("D7:F" & RngCounter +
2).PrintOut
Else

If MsgBox("The name of the active printer is " &
Application.ActivePrinter & vbCrLf & _
"Do you want to use this printer?" & vbCrLf & _
"" & vbCrLf & _
"Click no to select a different printer.", vbYesNo) = vbNo
Then

RngCounter = Application.WorksheetFunction.CountA
(Worksheets("Wkly Renewals").Range("D:D"))
ActiveSheet.PageSetup.PrintArea = "$D$7:$F$" & RngCounter
+ 2
PrintCounter = PrintCounter + 1
Application.Dialogs(xlDialogPrint).Show
ActiveSheet.PageSetup.PrintArea = ""
Else
PrintCounter = PrintCounter + 1
RngCounter = Application.WorksheetFunction.CountA
(Worksheets("Wkly Renewals").Range("D:D"))
Worksheets("Wkly Renewals").Range("D7:F" & RngCounter +
2).PrintOut
End If
End If

PrintCounter = PrintCounter + 1
With CreateObject("Wscript.Shell")
.Popup "Print Job: " & PrintCounter _
, 1, "Print Number", 64
End With


Then in the Print button (commandbutton) code I put:
PrintCount.PrintCount
This way it calls the procedure with the static variable.


This works. My problem was I was just stuck on using the
beforeprint event which I had put in the workbook module.
This throwing me for a loop.

Thanx for everyones help!


Todd



-----Original Message-----
I may be missing something, but what you have done is not

what Chip
suggested. Your variable is not a Public variable and is

not in a standard
module.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Todd Huttenstine"

wrote in message
...
See I did that. Below code is in workbiook module.

Public Sub Workbook_BeforePrint(Cancel As Boolean)
Static PrintCounter
PrintCounter = PrintCounter + 1
MsgBox "print Job: " & PrintCounter
End Sub

Now the value for variable PrintCounter can be 5 but

here
is the problem.

I click a print button on worksheets(5) and the code in

it
is along the lines of:

If PrintCounter 0 then
else
End If


In this case, the code is seeing the value of

PrintCounter
as "EMPTY" but the real value is 5. PrintCounter value

is
still 5, but for some reason this code cant see that.

How
do I correct this?



-----Original Message-----
Todd,

Declare the variable as Public in any standard code

module, above
and outside of any procedure. E.g.,

Public MyVar As Long


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Todd Huttenstine"


wrote in
message ...
hey

I have a variable(number) and want it to not die

until
the
workbook is closed. I need to be able to see the

value
of
the variable from a worksheet module. How would I do

this
and what module would I put the variable in?

Thank you
Todd Huttenstine




.



.

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 459
Default Static Variable

Note that Static is not common usage; I've never seen it in product
code. You will more often see a Property Let/Get pair with a private
module-level variable to persist the property's value e.g.

Private m_strName As String

Public Property Get Name() As String
Name = m_strName
End Property

Public Property Let Name(ByVal NewValue As String)
m_strName = NewValue
End Property

This gives you more control over the underlying value because you can
add code to the Get/Let e.g. to make write-once:

Public Property Let Name(ByVal NewValue As String)
If m_strName < Empty Then
Exit Property
End If
m_strName = NewValue
End Property

--
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
Concatenating two variable fields into a static length text field Young Programmer Lad Excel Worksheet Functions 2 April 17th 09 05:39 PM
STATIC VARIABLE NOT AVAILABLE IN ANOTHER MODULE CAPTGNVR Excel Discussion (Misc queries) 2 February 1st 07 07:30 PM
Sum cells based on a row variable and seperate column variable CheeseHeadTransplant Excel Worksheet Functions 10 September 23rd 05 06:59 PM
why is it saying sheetcnt is "variable not defined" how to do a global variable to share over multiple functions in vba for excel? Daniel Excel Worksheet Functions 1 July 9th 05 03:05 AM
Pivot Table - Object variable or with block variable not set? George Nicholson[_2_] Excel Programming 1 April 16th 04 09:12 PM


All times are GMT +1. The time now is 08:39 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"