Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Scope of variable includes all Form _and_ Code modules??

Is it possible to declare a public variable so that it is within scope for
all Form and Code modules?

Thanks.

John Wirt


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Scope of variable includes all Form _and_ Code modules??

Yes, it's possible. Simply declare them variables in a module using Public
keyword, Public p_i As Long, Public p_Const As String = "This is a string."


"John Wirt" wrote in message
...
Is it possible to declare a public variable so that it is within scope for
all Form and Code modules?

Thanks.

John Wirt




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 620
Default Scope of variable includes all Form _and_ Code modules??

A publicly declared variable in a class module is also available to the
other modules as long as you precede it by the class name.

For instance, declare a public variable cWorksheets in ThisWorkbook module,
and form anywhere else you can reference it with this code

Thisworkbook.cWorksheets=17

same with Forms, Worksheets.

--

HTH

Bob Phillips

"kiat" wrote in message
...
Yes, it's possible. Simply declare them variables in a module using Public
keyword, Public p_i As Long, Public p_Const As String = "This is a

string."


"John Wirt" wrote in message
...
Is it possible to declare a public variable so that it is within scope

for
all Form and Code modules?

Thanks.

John Wirt






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Scope of variable includes all Form _and_ Code modules??

I do not think this works.

I declared the variable numPrintOrder in a module 15 (one where the variable
is used in a procedure) thusly,

Option Explicit
Public numPrintOrder as integer

----------------------------------------
The value of numPrintOrder is set from the values entered in .textboxes on
frmUserForm8.

Private Sub butPrintOK_Click()
Dim numCopiestoPrint As Integer
Dim numPagestoPrint As Integer
Dim numPrintOrder As Integer

With frmUserForm10
If txtNumCopies.Value = "0" Then
numCopiestoPrint = 1
ElseIf txtNumCopies.Value < "" Then
numCopiestoPrint = txtNumCopies.Value
Else
numCopiestoPrint = 1
End If
If txtNumPages.Value = "0" Then
numPagestoPrint = 0
ElseIf txtNumPages.Value < "" Then
numPagestoPrint = txtNumPages.Value
Else
numPagestoPrint = 0
End If
End With

numPrintOrder = 100 * numPagestoPrint + numCopiestoPrint

------------------

I tried calling frmUserForm10 from code module 15, like this:

Option Explicit
Public numPrintOrder as Integer

Public Sub PrintCurrentPage() <-----Here???
Dim strWshName As String
Dim numCopiestoPrint As Integer
Dim numPagestoPrint As Integer

frmUserForm10.Show

strWshName = ActiveSheet.name

numCopiestoPrint = numPrintOrder Mod 100
numPagestoPrint = (numPrintOrder - numCopiestoPrint) / 100
If numPagestoPrint = 0 Then numPagestoPrint = 1

ActiveSheet.PrintOut _
From:=1, To:=numPagestoPrint, _
Copies:=numCopiestoPrint

End Sub
--------------------------------------------

This did not work. After fmUserForm 10 closed, the value of numPrintOrder is
0.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 90
Default Scope of variable includes all Form _and_ Code modules??

Do you realize that when you declare the same variable name twice, it's
two distinct variables?


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 620
Default Scope of variable includes all Form _and_ Code modules??

John,

You have declared the variable numPrintOrder as a global variable in a code
module. Okay so far.

Unfortunately, you then declare it again as a local variable in the form
procedure butPrintOK_Click. Therefore when you reference numPrintOrder
within this form procedure, you use the local variable, but which is being
loaded, but when you reference numPrintOrder in other procs, you reference
the global variable, which is empty.

Get rid of the local variab le!

--

HTH

Bob Phillips

"John Wirt" wrote in message
...
I do not think this works.

I declared the variable numPrintOrder in a module 15 (one where the

variable
is used in a procedure) thusly,

Option Explicit
Public numPrintOrder as integer

----------------------------------------
The value of numPrintOrder is set from the values entered in .textboxes on
frmUserForm8.

Private Sub butPrintOK_Click()
Dim numCopiestoPrint As Integer
Dim numPagestoPrint As Integer
Dim numPrintOrder As Integer

With frmUserForm10
If txtNumCopies.Value = "0" Then
numCopiestoPrint = 1
ElseIf txtNumCopies.Value < "" Then
numCopiestoPrint = txtNumCopies.Value
Else
numCopiestoPrint = 1
End If
If txtNumPages.Value = "0" Then
numPagestoPrint = 0
ElseIf txtNumPages.Value < "" Then
numPagestoPrint = txtNumPages.Value
Else
numPagestoPrint = 0
End If
End With

numPrintOrder = 100 * numPagestoPrint + numCopiestoPrint

------------------

I tried calling frmUserForm10 from code module 15, like this:

Option Explicit
Public numPrintOrder as Integer

Public Sub PrintCurrentPage() <-----Here???
Dim strWshName As String
Dim numCopiestoPrint As Integer
Dim numPagestoPrint As Integer

frmUserForm10.Show

strWshName = ActiveSheet.name

numCopiestoPrint = numPrintOrder Mod 100
numPagestoPrint = (numPrintOrder - numCopiestoPrint) / 100
If numPagestoPrint = 0 Then numPagestoPrint = 1

ActiveSheet.PrintOut _
From:=1, To:=numPagestoPrint, _
Copies:=numCopiestoPrint

End Sub
--------------------------------------------

This did not work. After fmUserForm 10 closed, the value of numPrintOrder

is
0.

To get the value of numPrintOrder into the PrintCurrentPage procedure, I
split it in two after the frmUserForm10.show command and added a

statement,

call PrintCurrentPage2(numPrintOrder)

This works. the printer control information entered in form 10 is entered

in
the parameters of the PrintOut method.

---------------------------------
What is the problem here? How does one define a global variable in a code
module, set the value of the variable in a procedure within a form, and

then
have the value of the variable be within scope inside a different

procedure
in a code module?

As indicated above, I was only able to transfer the value of the variable

as
set in the form procedure to the procedure in the code module, by calling

it
with the variable as an input parameter. This hardly qualifies as being a
'global' variable.

Thanks in advance.

John Wirt















Call PrintCurrentPage2(numPrintOrder)


"kiat" wrote in message
...
Yes, it's possible. Simply declare them variables in a module using

Public
keyword, Public p_i As Long, Public p_Const As String = "This is a

string."


"John Wirt" wrote in message
...
Is it possible to declare a public variable so that it is within scope

for
all Form and Code modules?

Thanks.

John Wirt








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
split post code (zip code) out of cell that includes full address Concord Excel Discussion (Misc queries) 4 October 15th 09 06:59 PM
Where 2 place the code? (Worksheet Codes Vs. Modules) FARAZ QURESHI Excel Discussion (Misc queries) 3 February 23rd 09 02:01 AM
can i link a variable cost code with a variable sum I need help!! Excel Discussion (Misc queries) 0 August 1st 08 11:40 AM
Delete Code Modules Programatically blatham Excel Discussion (Misc queries) 1 February 3rd 06 05:38 PM
Using the same variable in separate userform modules DarrenW Excel Programming 2 July 11th 03 03:11 AM


All times are GMT +1. The time now is 06:58 PM.

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"