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

I am trying to modularize my code, but I'm having trouble with one aspect.
How do I make a variable such as a counter available within a macro that I
just called?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Passing Variables to another macro

Option Explicit
Sub Main()
Dim icntr As Long
Dim adjNum As Long
For icntr = 1 To 10
MySub icntr
adjNum = MyFunc(icntr)
MsgBox "Adjusted Counter is " & adjNum
Next
End Sub

Sub MySub(num As Long)
If num < 5 Then
MsgBox "num: " & num & ", is less than 5"
Else
MsgBox "num: " & num & ", is greater" & _
" than or equal to 5"
End If
End Sub

Function MyFunc(myNum As Long)
Dim MyAdjustedNum As Long
MyAdjustedNum = myNum * myNum
MyFunc = MyAdjustedNum
End Function


--
Regards,
Tom Ogilvy

"Jeff" wrote in message
...
I am trying to modularize my code, but I'm having trouble with one aspect.
How do I make a variable such as a counter available within a macro that I
just called?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default Passing Variables to another macro

Here is an example:

Sub FirstProcedure()
Dim lngCounter As Long

lngCounter = 0

SecondProcedure lngCounter

MsgBox lngCounter

SecondProcedure lngCounter
SecondProcedure lngCounter
SecondProcedure lngCounter

MsgBox lngCounter
End Sub

Sub SecondProcedure(Counter As Long)
Counter = Counter + 1
End Sub


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Jeff" wrote in message
...
I am trying to modularize my code, but I'm having trouble with one aspect.
How do I make a variable such as a counter available within a macro that I
just called?



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Passing Variables to another macro


To retrieve the value of a variable from another routine you'll need to do
two simple things:

1) Ensure the routine which has to retrun the variable value is a Function
not a Sub. The primary difference between the two is that Functions are
capable of returning a value to a calling routine while Subs are not.

2) At the end of the function assign the value of the variable to the name
of the function.

Here's an example:


Sub Test()
MsgBox LoopAndCount()
End Sub



Function LoopAndCount() As Long
Dim i As Long
Dim Total As Long

For i = 1 To 100
Total = Total + i
Next i

LoopAndCount = Total

End Function


Note a couple of things. The function has a return type of Long which is the
same data type as the variable I want to get the value of. The moral here is:
whatever data type the variable is that you want to get the value of, make
the function return the same data type.

Let me know if you need a more specific example.

Good Coding!

OfficeHacker

"Jeff" wrote:

I am trying to modularize my code, but I'm having trouble with one aspect.
How do I make a variable such as a counter available within a macro that I
just called?

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 Variables Jeff Excel Discussion (Misc queries) 1 November 4th 05 06:46 PM
Passing variables between a form and macro David New Users to Excel 1 October 5th 05 04:42 AM
Passing variables between a form and macro David New Users to Excel 4 September 23rd 05 11:57 AM
Passing variables from Outlook Macro to Excel Macro [email protected] Excel Programming 2 January 7th 05 10:07 PM
passing variables from an excel macro to a powerpoint macro jake Excel Programming 1 December 11th 03 02:36 AM


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