Passing a variable into a different piece of code

bruty

Active Member
Joined
Jul 25, 2007
Messages
453
I have a spreadsheet that has several buttons on that run code using the cells around the code for parts of the data. eg

ColumnA1 ColumnB1 ColumnC1 Button1
ColumnA2 ColumnB2 ColumnC2 Button2

I want the buttons to run a seperate piece of code to start, but then all buttons run a similar piece of code at the end. I am hoping to be able to pass a variable from the first piece of code each button runs into the section of code that all share (which I have done as a seperate macro)

I am having problems getting the variable to pass - can anyone offer any help?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
set the variable up as public
edit:
I'm not 100% sure that the variable is still available if the code has finished, if that's what I think you are stating.
 
Upvote 0
I only need it while the code is running.

Where do I set this? In the first bit of code?
 
Upvote 0
Set up your second piece of code to take the information as an argument - for example:
Code:
Sub SecondSub(lngInput as Long)
   ' code to do something with lngInput here
End Sub

then you call it from your buttons using:
Code:
Private Sub Button1_Click()
  ' code that does some unique stuff
  ' call second sub and pass the value 1 to it
  SecondSub 1
End Sub

Does that make sense?
 
Upvote 0
Here's the code I have to start with:

Rich (BB code):
Sub Emailer_ChrisBruty()


emailto = "Chris Bruty"

Range("C3").Select
Selection.Copy
Sheets("Fetch").Select
Range("b2").Select
ActiveSheet.Paste
Sheets("Standard Reports").Select
Range("D3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Fetch").Select
Range("b3").Select
ActiveSheet.Paste
Sheets("Standard Reports").Select
Range("E3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Fetch").Select
Range("b4").Select
ActiveSheet.Paste
Sheets("Standard Reports").Select
Range("F3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Fetch").Select
Range("b5").Select
ActiveSheet.Paste
Sheets("Standard Reports").Select
Range("G3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Fetch").Select
Range("b12").Select
ActiveSheet.Paste


emailCode

Windows("Weekly Breakdown.xls").Activate
Sheets("Standard Reports").Select
Range("J3").Value = Now

EndSub:
End Sub

The variable I want to pass over is the emailto and I want to pass it into the emailCode macro that is called at the bottom.
 
Upvote 0
I only need it while the code is running.

Where do I set this? In the first bit of code?


before the sub put
Public VAR as Long
(replace with what your variable is called)
This will store the value from the 1st macro and pass to the 2nd macro, by using the call like Rorya showed
 
Upvote 0
Then make sure emailCode is defined as follows

Sub emailCode(mailto as string)

'Code in here

end sub

That makes the mailto variable available in your emailcode sub. To pass the data to it amend the call in your Emailer_ChrisButy() sub to

call emailacode(emailto)

instead of just

emailacode
 
Upvote 0
before the sub put
Public VAR as Long
(replace with what your variable is called)
This will store the value from the 1st macro and pass to the 2nd macro, by using the call like Rorya showed

You don't need a Public Variable if passing from one sub to another.
 
Upvote 0
as just a FYI

Project-Level, Workbook Level, or Public Module-Level

These variables are dimensioned at the top of any standard public module, like shown below;

Public lRows as Long
All variables dimensioned at this level are available to all Procedures in all Modules. Its value is retained unless the Workbook closes or the End Statement is used.
 
Upvote 0

Forum statistics

Threads
1,214,592
Messages
6,120,433
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top