Procedure too large/ Option Explicit

yxz152830

Active Member
Joined
Oct 6, 2021
Messages
390
Office Version
  1. 365
Platform
  1. Windows
Gurus,
If my module is option explicit, how do I break the procedure into smaller procedures in the most efficient way?
my guess is that I can claim the variables in global or I can claim them again in each procedure?
 
Nothing - it doesn't matter what you call the variable.
Hello just one more question; How do I pass a variable down to the other sub and then send the variable out from the other sub back to main sub?
VBA Code:
sub main()
dim x as double
x= 100
abc x
range("a1") = x
end sub
VBA Code:
sub abc(x as double)
(some calculations on x)
end sub
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Exactly like that. Variables are passed ByRef by default, so any changes you make to the variable passed as an argument are reflected in the original variable too.
 
Upvote 0
Exactly like that. Variables are passed ByRef by default, so any changes you make to the variable passed as an argument are reflected in the original variable too.
on more question pls:
if the nested sub has a variable that needs to be passed down to the main sub, how do I do it?
like below, a is calculated in the nested sub.

VBA Code:
sub main()
dim x as double
x= 100
abc x
range("a1") = a
end sub

VBA Code:
sub abc(x as double)
(some calculations on x)
a = x^2
end sub
 
Upvote 0
Simplest way is to change the called sub to a function that returns the value you want:

VBA Code:
sub main()
dim x as double
dim a as double
x= 100
a = abc(x)
range("a1") = a
end sub

VBA Code:
function abc(x as double) as double
(some calculations on x)
abc = x^2
end function
 
Upvote 0
Simplest way is to change the called sub to a function that returns the value you want:

VBA Code:
sub main()
dim x as double
dim a as double
x= 100
a = abc(x)
range("a1") = a
end sub

VBA Code:
function abc(x as double) as double
(some calculations on x)
abc = x^2
end function
is there another way? I need to keep the called sub for other procedures. Is it better that I just declare a in the main sub so I can pass it to the called sub so that it can automatically return to the main sub?
 
Upvote 0
You should still make it a function. Other routines can still call it the same way they already do. It would tend to suggest that the called sub may be doing too many things if it should sometimes return a value and sometimes not.
 
Upvote 0

Forum statistics

Threads
1,214,553
Messages
6,120,179
Members
448,948
Latest member
spamiki

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