Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Carrying Variables into Called Macros

Hello:

I'm looking for a way to carry a variable with an
assigned value into a called macro.

I have 10 CheckBoxes on a worksheet. When box 1 is
checked, I want to assign a variable (call it X) the
value equal to the checkbox number. Example: when box 1
is checked, I want X = 1, when box 2 is checked, I want X
= 2, etc.

I have a macro which I want to "Call" and execute for
each box. The same macro is called for each box; the
only difference is the value of the variable X, which the
macro uses to do its thing.

When I set it up like this:
Private Sub CheckBox 1_Click()
X = 1
Call {macro}
End Sub

I get an error which tells me that X is not recognized by
the macro. When I hardcode X=1 in the macro, it works.

Is there a way to carry X into the macro? I'm trying to
avoid having 10 nearly identical macros in my spreadsheet.

I could settle for modifying the macro by adding 10
lines:
If [CheckBox 1 is clicked] then X = 1
If [CheckBox 2 is clicked] then X = 2, etc.

But I don't know the correct syntax for the code between
the []'s.

First option (carrying the value) is preferred, but 2nd
option (modifying the macro) will be OK with me.

Help on either is appreciated.

Thanks,
MARTY
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Carrying Variables into Called Macros

Pass X as a macro argument

Call myMacro(X)

and in the macro

Sub myMacro(XVal)

MsgBox XVal
'etc.

End SUb

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Marty" wrote in message
...
Hello:

I'm looking for a way to carry a variable with an
assigned value into a called macro.

I have 10 CheckBoxes on a worksheet. When box 1 is
checked, I want to assign a variable (call it X) the
value equal to the checkbox number. Example: when box 1
is checked, I want X = 1, when box 2 is checked, I want X
= 2, etc.

I have a macro which I want to "Call" and execute for
each box. The same macro is called for each box; the
only difference is the value of the variable X, which the
macro uses to do its thing.

When I set it up like this:
Private Sub CheckBox 1_Click()
X = 1
Call {macro}
End Sub

I get an error which tells me that X is not recognized by
the macro. When I hardcode X=1 in the macro, it works.

Is there a way to carry X into the macro? I'm trying to
avoid having 10 nearly identical macros in my spreadsheet.

I could settle for modifying the macro by adding 10
lines:
If [CheckBox 1 is clicked] then X = 1
If [CheckBox 2 is clicked] then X = 2, etc.

But I don't know the correct syntax for the code between
the []'s.

First option (carrying the value) is preferred, but 2nd
option (modifying the macro) will be OK with me.

Help on either is appreciated.

Thanks,
MARTY



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Carrying Variables into Called Macros

Perfect Bob! Thank you vey much.
-----Original Message-----
Pass X as a macro argument

Call myMacro(X)

and in the macro

Sub myMacro(XVal)

MsgBox XVal
'etc.

End SUb

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Marty" wrote in

message
...
Hello:

I'm looking for a way to carry a variable with an
assigned value into a called macro.

I have 10 CheckBoxes on a worksheet. When box 1 is
checked, I want to assign a variable (call it X) the
value equal to the checkbox number. Example: when

box 1
is checked, I want X = 1, when box 2 is checked, I

want X
= 2, etc.

I have a macro which I want to "Call" and execute for
each box. The same macro is called for each box; the
only difference is the value of the variable X, which

the
macro uses to do its thing.

When I set it up like this:
Private Sub CheckBox 1_Click()
X = 1
Call {macro}
End Sub

I get an error which tells me that X is not recognized

by
the macro. When I hardcode X=1 in the macro, it works.

Is there a way to carry X into the macro? I'm trying

to
avoid having 10 nearly identical macros in my

spreadsheet.

I could settle for modifying the macro by adding 10
lines:
If [CheckBox 1 is clicked] then X = 1
If [CheckBox 2 is clicked] then X = 2, etc.

But I don't know the correct syntax for the code

between
the []'s.

First option (carrying the value) is preferred, but 2nd
option (modifying the macro) will be OK with me.

Help on either is appreciated.

Thanks,
MARTY



.

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
Carrying last $$$ amount to another field Challenged Excel Worksheet Functions 4 April 13th 09 07:01 AM
Carrying data over in worksheets roxiemayfield Excel Worksheet Functions 4 December 5th 07 10:00 PM
Carrying a balance forward Melanie Savasta Excel Worksheet Functions 2 April 18th 06 04:07 PM
Carrying formatting automatically to new W/S Jonah Excel Worksheet Functions 4 September 6th 05 12:12 AM
Input variables on Sheet1 being called by a VB program on Sheet 2 RU42 Excel Programming 1 August 23rd 03 09:50 AM


All times are GMT +1. The time now is 08:55 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"