Best Vba practice

julhs

Active Member
Joined
Dec 3, 2018
Messages
409
Office Version
  1. 2010
Platform
  1. Windows
I have numerous modules that have the SAME section of code in them to carry out the same task.

Before I take what I’m doing any further, just want to clarify “Best Vba” practice.

Is it best/better to keep those sections of code in EACH individual module or have them in a separate module and “Call” them as and when required?
Having them in a separate module, then calling them when required seems to me to be somewhat similar to using,
“GoTo line?:” (Spaghetti code).
What is the advice?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I would do it something like this:
Excel Formula:
Sub Hello()
MsgBox "Hello Julhs"
End Sub
Sub Goodbye()
MsgBox "Goodbye Julhs "
End Sub
Sub Both()
Cells(1, 1).Value = "1"
Call Hello
Cells(2, 1).Value = "2"
Call Goodbye
End Sub
 
Upvote 0
I have numerous modules that have the SAME section of code in them to carry out the same task.

Something considered good coding practice is do not repeat yourself or DRY for short.

If you have code in each module that is doing the same thing then create either a common code or a Function if need it to return a value & call it as required.

general explanation here but there are plenty of examples you can search


Dave
 
Upvote 0
Thank you both for your advice.

Dave, found the link you sent particularly helpful.
 
Upvote 0
Thank you both for your advice.

Dave, found the link you sent particularly helpful.

most welcome glad we could help & appreciate your feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,215,446
Messages
6,124,896
Members
449,194
Latest member
JayEggleton

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