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

I have a macro

Sub TestMacro(X,Y)
....calculations based on X and Y values
End Sub

By putting the "X,Y" in the macro, does this make it a function. Because
when I try to look for the macro using the macro button on the toolbar the
function "TestMacro" does not show up. It shows up when I remove the X,Y.
Do you know what has changed? Also i this a good way to program, I want to
be able to input values for X and Y so I thought this would be good since I
wouldn't have to keep changing the "TestMacro".

Sub macro1()
X = 7
Y=5
Call TestMacro(X,Y)
End sub
  #2   Report Post  
Bob Phillips
 
Posts: n/a
Default VBA passing variables through a function

I is not in the macro list because there is no way that you can pass the
arguments to a macro called from the macro list, Excel just does not provide
it, so it doesn't list.

It isn't a function. A function use Function instead of Sub.

--

HTH

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


"Jeff" wrote in message
...
I have a macro

Sub TestMacro(X,Y)
...calculations based on X and Y values
End Sub

By putting the "X,Y" in the macro, does this make it a function. Because
when I try to look for the macro using the macro button on the toolbar the
function "TestMacro" does not show up. It shows up when I remove the X,Y.
Do you know what has changed? Also i this a good way to program, I want

to
be able to input values for X and Y so I thought this would be good since

I
wouldn't have to keep changing the "TestMacro".

Sub macro1()
X = 7
Y=5
Call TestMacro(X,Y)
End sub



  #3   Report Post  
Dave Peterson
 
Posts: n/a
Default VBA passing variables through a function

I used this subroutine.

Option Explicit
Sub TestMacro(X As Long, Y As Long)
MsgBox X + Y
End Sub

In excel, I hit tools|macro|macros and I typed:

'testmacro 3,5'

And I saw a message box with 8 in it.

But I think you may want to just ask for the values...

Option Explicit
Sub TestMacro()

Dim X As Double
Dim Y As Double

X = Application.InputBox("Please enter 1st number", Type:=1)
Y = Application.InputBox("please enter 2nd nubmer", Type:=1)

MsgBox X + Y

End Sub

You could also use the builtin Inputbox to get the values, too.

And once you decide that you don't want to go through 45 inputboxes, you may
want to make a userform to collect the information.

Debra Dalgleish has some get started instructions at:
http://contextures.com/xlUserForm01.html

Jeff wrote:

I have a macro

Sub TestMacro(X,Y)
...calculations based on X and Y values
End Sub

By putting the "X,Y" in the macro, does this make it a function. Because
when I try to look for the macro using the macro button on the toolbar the
function "TestMacro" does not show up. It shows up when I remove the X,Y.
Do you know what has changed? Also i this a good way to program, I want to
be able to input values for X and Y so I thought this would be good since I
wouldn't have to keep changing the "TestMacro".

Sub macro1()
X = 7
Y=5
Call TestMacro(X,Y)
End sub


--

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
Passing parameters Cel Ref) to Sum function Shamshad Butt Excel Discussion (Misc queries) 4 October 26th 05 10:46 AM
Changing worksheet cells from within a function James4U2enjoy Setting up and Configuration of Excel 1 October 14th 05 02:16 PM
Passing variables between a form and macro David New Users to Excel 1 October 5th 05 04:42 AM
Can I use variables in a Range function? André - Brazil Excel Discussion (Misc queries) 2 May 5th 05 10:39 PM
HOW CAN I GET OFFICE 2003 EXCEL BASIC TO NEST FUNCTIONS LIKE EXCE. Robert AS Excel Worksheet Functions 4 December 2nd 04 10:49 AM


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