Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default variable retaining value between subroutins question

Is there a way for the variable to retain it's value to use it in another
Sub?
See example. I need for Cell_Transfer to keep it's value to use it in
Static_Test or elsewhere

please help



Sub Static_Test()
Call Find_Next_Cell
MsgBox Cell_Transfer
End Sub

Sub Find_Next_Cell()
Dim My_Cell As Range
Static Cell_Transfer

For Each My_Cell In Range("D6:IV6")
If My_Cell.Value = "" Then GoTo when_blank
Next
when_blank:

Cell_Transfer = My_Cell.Address
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default variable retaining value between subroutins question

See "scoping" in the VBA Help for a detailed explanation.

A variable declared inside a procedure will not be seen by another
procedure. If you declare the variable at the module level, using "Private",
it can be seen by all procedures in the given module. If you declare the
variable as "Public" it will be seen by all procedures on all module pages
within the project.

IMPORTANT: Remove any declarations of Cell_Transfer from within a procedure.
Declaring a variable at the project level AND within a procedure can be
quite maddening. They are essentially two different variables that happen to
share the same name. Your code will appear to not do what you want.

You can also remove the line of code: "Static Cell_Transfer"

Troy


----At the VERY TOP of the module page:

Option Explicit
Public Cell_Transfer As String '''<=== This is the largest scope
possible.


Sub Static_Test()
Call Find_Next_Cell
MsgBox Cell_Transfer
End Sub

Sub Find_Next_Cell()
Dim My_Cell As Range
Static Cell_Transfer '''<=== REMOVE THIS LINE OF CODE

For Each My_Cell In Range("D6:IV6")
If My_Cell.Value = "" Then GoTo when_blank
Next
when_blank:

Cell_Transfer = My_Cell.Address
End Sub



"Barmaley" wrote in message
...
Is there a way for the variable to retain it's value to use it in another
Sub?
See example. I need for Cell_Transfer to keep it's value to use it in
Static_Test or elsewhere

please help



Sub Static_Test()
Call Find_Next_Cell
MsgBox Cell_Transfer
End Sub

Sub Find_Next_Cell()
Dim My_Cell As Range
Static Cell_Transfer

For Each My_Cell In Range("D6:IV6")
If My_Cell.Value = "" Then GoTo when_blank
Next
when_blank:

Cell_Transfer = My_Cell.Address
End Sub




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default variable retaining value between subroutins question

Thank you very much, it worked well

"TroyW" wrote in message
...
See "scoping" in the VBA Help for a detailed explanation.

A variable declared inside a procedure will not be seen by another
procedure. If you declare the variable at the module level, using

"Private",
it can be seen by all procedures in the given module. If you declare the
variable as "Public" it will be seen by all procedures on all module pages
within the project.

IMPORTANT: Remove any declarations of Cell_Transfer from within a

procedure.
Declaring a variable at the project level AND within a procedure can be
quite maddening. They are essentially two different variables that happen

to
share the same name. Your code will appear to not do what you want.

You can also remove the line of code: "Static Cell_Transfer"

Troy


----At the VERY TOP of the module page:

Option Explicit
Public Cell_Transfer As String '''<=== This is the largest scope
possible.


Sub Static_Test()
Call Find_Next_Cell
MsgBox Cell_Transfer
End Sub

Sub Find_Next_Cell()
Dim My_Cell As Range
Static Cell_Transfer '''<=== REMOVE THIS LINE OF

CODE

For Each My_Cell In Range("D6:IV6")
If My_Cell.Value = "" Then GoTo when_blank
Next
when_blank:

Cell_Transfer = My_Cell.Address
End Sub



"Barmaley" wrote in message
...
Is there a way for the variable to retain it's value to use it in

another
Sub?
See example. I need for Cell_Transfer to keep it's value to use it in
Static_Test or elsewhere

please help



Sub Static_Test()
Call Find_Next_Cell
MsgBox Cell_Transfer
End Sub

Sub Find_Next_Cell()
Dim My_Cell As Range
Static Cell_Transfer

For Each My_Cell In Range("D6:IV6")
If My_Cell.Value = "" Then GoTo when_blank
Next
when_blank:

Cell_Transfer = My_Cell.Address
End Sub






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
Variable calculation question EG Excel Worksheet Functions 1 October 19th 09 12:00 AM
Variable name question JTWarthogs Excel Discussion (Misc queries) 3 May 7th 09 02:22 PM
Inserting and retaining an input variable ARGT Excel Discussion (Misc queries) 4 June 26th 08 11:42 PM
variable question peyman Excel Discussion (Misc queries) 3 October 16th 07 12:33 AM
I Need VBA Assistance for global variable question Brent E Excel Discussion (Misc queries) 1 March 1st 05 08:46 PM


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