Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Jeff
 
Posts: n/a
Default VBA defining variables

Hi

I have the code below. I set JJ=33, and I need Macro5 and Macro6 to use the
variable JJ. But when it goes into those macros JJ is empty. Do you know
how to fix this?

Sub TestMacro2()
Dim JJ As Double
JJ = 33
Call Macro5
Call Macro6
End Sub

Thanks for your help
  #2   Report Post  
bpeltzer
 
Posts: n/a
Default VBA defining variables

Because the variable is declared inside the sub TestMacro2, its 'scope' is
limited to that sub. to make it more widely available you can either declare
it outside the sub (making it 'global', which is easy but not appreciated by
the programming gurus) or make it an argument to Macro5 and Macro6 (ex:
declare Sub Macro6(InputVar as double) ... End Sub, and call that as
Macro6(JJ33).
--Bruce
(BTW, to find such errors earlier, you should set VBA to require that
variables be declared; 'Option Explicit' should appear at the beginning of
each module).


"Jeff" wrote:

Hi

I have the code below. I set JJ=33, and I need Macro5 and Macro6 to use the
variable JJ. But when it goes into those macros JJ is empty. Do you know
how to fix this?

Sub TestMacro2()
Dim JJ As Double
JJ = 33
Call Macro5
Call Macro6
End Sub

Thanks for your help

  #3   Report Post  
Dave Peterson
 
Posts: n/a
Default VBA defining variables

One way is to pass the value:

Sub TestMacro2()
Dim JJ As Double
JJ = 33
Call Macro5(jj)
Call Macro6(jj)
End Sub

sub macro5(myVal as double)
msgbox myVal
end sub

sub macro6(myothervalue as double)
msgbox myothervalue
end sub



Jeff wrote:

Hi

I have the code below. I set JJ=33, and I need Macro5 and Macro6 to use the
variable JJ. But when it goes into those macros JJ is empty. Do you know
how to fix this?

Sub TestMacro2()
Dim JJ As Double
JJ = 33
Call Macro5
Call Macro6
End Sub

Thanks for your help


--

Dave Peterson
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
How do I use variables in Excel? XUT67 Excel Discussion (Misc queries) 1 October 3rd 05 07:11 PM
How do I use variables in Excel? XUT67 Excel Discussion (Misc queries) 4 July 22nd 05 09:04 PM
solver and defining all variables different than one another excel_excel_excel Excel Discussion (Misc queries) 0 July 19th 05 07:38 AM
Plot 2 variables on Y axis nemme Charts and Charting in Excel 1 March 18th 05 08:17 PM
Index Match With 3 Variables Scooterdog Excel Worksheet Functions 5 January 2nd 05 07:05 PM


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