subroutine

khatri milan

New Member
Joined
Nov 6, 2019
Messages
17
i have initiated value for some variable inside a module and called a subroutine.Inside that subroutine i have formula which uses those variables.will the subroutine recognize those varibles and their respective values even if i dont pass those values as arguemetns into subroutine?
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Not unless they were declared outside a sub eg

Code:
Option Explicit


Public Variable1 As Long


Sub test1()
    Variable1 = 12345
    test2
End Sub


Sub test2()
    MsgBox Variable1
End Sub
 
Upvote 0
when try to declare variable outside sub the lines of code moves upward into the end of previous subroutine.will that be problem? and when i do declare variable like you said and change value of variable inside subroutine, will changed value of variable be available outside subroutine ?
 
Upvote 0
when try to declare variable outside sub the lines of code moves upward into the end of previous subroutine.
No idea what you mean by that... can you give an example?
when i do declare variable like you said
You declare it in a Code module, once it's written it's active.
change value of variable inside subroutine
Whenever you need to, or after a calculation.
will changed value of variable be available outside subroutine ?
The variable will remain until you either change it or the workbook is closed.
 
Upvote 0
Save the image to dropbox/google drive/one drive etc, then post the url. Even better if you can post your workbook, we can see exactly what's going on then.
 
Upvote 0
Subroutine in VBA is often used incorrectly. Code that starts with the word "Sub" is not a subroutine but a Sub Procedure. A true subroutine is code within the Sub or Function procedure that executes when a GoSub statement transfers control to the code block. When the subroutine finishes and the Return statement is executed, execution resumes after the GoSub statement in the Sub or Function procedure.
 
Upvote 0

Forum statistics

Threads
1,224,738
Messages
6,180,673
Members
452,993
Latest member
FDARYABEE

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