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


How can you code a module to pass a value from one Private sub to a
Public sub or the other way around? More to the point is once the
variable has the value how do you retrieve the value?


--
Information Hog
------------------------------------------------------------------------
Information Hog's Profile: http://www.excelforum.com/member.php...o&userid=21508
View this thread: http://www.excelforum.com/showthread...hreadid=398019

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Passing Values


the simplest way is to define the variable as a public variable
Then its value can be read and modified by any sub


--
instruite

I am B.E. Instrumentation and Control and write now working as Hardware
design engineer in automotive electrconics industry
------------------------------------------------------------------------
instruite's Profile: http://www.excelforum.com/member.php...o&userid=26539
View this thread: http://www.excelforum.com/showthread...hreadid=398019

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Passing Values

An example:

Private Sub start()
Dim myVar As Integer
myVar = 5
Call NextBit(myVar)
End Sub

Sub NextBit(Var1 As Integer)
MsgBox "Variable value is " & Var1
'or
'activesheet.range("A1").value = var1
'etc
End Sub

The sub NextBit takes a variable of integer type called Var1. When the
Private sub runs it passes a value to NextBit which is set to the value of
Var1. You can then retrieve this value by referencing the variable.

Hope this helps
Rowan

"Information Hog" wrote:


How can you code a module to pass a value from one Private sub to a
Public sub or the other way around? More to the point is once the
variable has the value how do you retrieve the value?


--
Information Hog
------------------------------------------------------------------------
Information Hog's Profile: http://www.excelforum.com/member.php...o&userid=21508
View this thread: http://www.excelforum.com/showthread...hreadid=398019


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Passing Values

There are a number of ways.

The easiest and worst IMO is to use a public variable throughout.

Second way is to pass a variable ByRef (the default)

Public Sub Macro1()
Dim myVar As Long

myVar = 5
MsgBox myVar
Macro2 myVar
MsgBox myVar
End Sub

Public Sub Macro2(ByRef arg As Long)
arg=22
End Sub

Another way, and my preferred option, is to use functions

Public Sub Macro1()
Dim myVar As Long

myVar = 5
MsgBox myVar
myVar = Func1(myVar)
MsgBox myVar
End Sub

Function Func1(arg As Long) As Long
Func1 = arg * 5
End Function


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Information Hog"
<Information.Hog.1u7b6a_1124773519.243@excelforu m-nospam.com wrote in
message news:Information.Hog.1u7b6a_1124773519.243@excelfo rum-nospam.com...

How can you code a module to pass a value from one Private sub to a
Public sub or the other way around? More to the point is once the
variable has the value how do you retrieve the value?


--
Information Hog
------------------------------------------------------------------------
Information Hog's Profile:

http://www.excelforum.com/member.php...o&userid=21508
View this thread: http://www.excelforum.com/showthread...hreadid=398019



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default Passing Values

Not to be trite or anything but is sounds like you don't have a grasp
of scope (lifetime of a variable). Any VB programming book should
address this subject which is too lengthy for a post.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Passing Values

Who is this addressed to. If to Bob as it appears, please explain how you
would improve on his succinct yet complete response. Also explain in what
way you consider Bob does not have a grasp of the scope of variables.

Regards,
Peter T

PS Friendly advice - read Bob's response carefully before replying !

"mike" wrote in message
oups.com...
Not to be trite or anything but is sounds like you don't have a grasp
of scope (lifetime of a variable). Any VB programming book should
address this subject which is too lengthy for a post.



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default Passing Values

Your correct, of course! I knew who the comment was address to, not Bob
but the original poster. I should have replied to "Hog", not Bob.
Sorry for the confusion, Bob.
Since "Hog" seemed to indicate a lack of knowledge on simply getting a
value from a variable, I felt the best advice was to recommend becoming
more familiar with the basics. While Bob's response is good, "Hog" also
needs to have an understanding of where variables should reside and
why.

mike

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
passing values to other sheets [email protected] New Users to Excel 1 February 23rd 08 02:03 AM
Passing values in Sheet to VBA Jonathan Charts and Charting in Excel 1 June 30th 06 06:00 AM
Passing values to new app BrianG[_4_] Excel Programming 3 May 27th 04 01:58 PM
Hyperklinks - passing values Nigel[_7_] Excel Programming 0 February 18th 04 10:23 AM
Passing values between 2 subs ? [email protected] Excel Programming 1 November 21st 03 05:56 PM


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