Procedure too large/ Option Explicit

yxz152830

Active Member
Joined
Oct 6, 2021
Messages
393
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?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Keep variables local where possible, and pass them between procedures as arguments when necessary.
 
Upvote 0
Keep variables local where possible, and pass them between procedures as arguments when necessary.
right now ive got another problem...
my whole procedure is a loop, meaning everything is in a loop. How do I break it down into small procedures?
It's like this:
for cell0
for cell1 in range1
next cell1
for cell2 in range2
next cell2
........................
for cell20 in range20
next cell20
next cell0
 
Upvote 0
Whatever you are doing inside those loops can probably be refactored into a separate procedure (or several, if they don't do similar things for each range).
 
Upvote 0
Whatever you are doing inside those loops can probably be refactored into a separate procedure (or several, if they don't do similar things for each range).
one of the conditions for the nested loops to proceed is: if cell(1 to 20).value = cell0.value. I'm not sure how to refactor them into seperate subroutines. If cell0 is only in the main sub, the if statement in other subs like " if cell1.value = cell0.value" will return object not defined type of error.
 
Upvote 0
You would pass the value in question as an argument to the called routine. It's very difficult to give specific advice about such a generalised question, but that is the basic principle.
 
Upvote 0
You would pass the value in question as an argument to the called routine. It's very difficult to give specific advice about such a generalised question, but that is the basic principle.
could you show me how to do it by this example? how do I seperate the nested for loop from the main sub?

VBA Code:
sub main()
for each cell0 in range0
  for each cell1 in range1
    if cell1 = cell0 then
.......
    end if
  next cell1
next cell0
end sub
 
Upvote 0
VBA Code:
sub main()
for each cell0 in range0
   otherSub cell0.Value
next cell0
end sub
Sub otherSub(checkVal as Variant)
  for each cell1 in range1
    if cell1 = checkVal then
       ' do something
    end if
  next cell1
 
Upvote 0
Solution
VBA Code:
sub main()
for each cell0 in range0
   otherSub cell0.Value
next cell0
end sub
Sub otherSub(checkVal as Variant)
  for each cell1 in range1
    if cell1 = checkVal then
       ' do something
    end if
  next cell1
what would happen if i replace checkval by cell0?
 
Upvote 0
Nothing - it doesn't matter what you call the variable.
 
Upvote 0

Forum statistics

Threads
1,215,320
Messages
6,124,238
Members
449,149
Latest member
mwdbActuary

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