7 Simple Practices for Writing Super-Readable VBA Code

  Рет қаралды 68,725

Excel Macro Mastery

Excel Macro Mastery

Күн бұрын

Пікірлер: 102
@MrDoomfighter
@MrDoomfighter 5 жыл бұрын
my coworkers will have to thank you :)
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Glad you like it😀
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Hi everyone, Enjoy your watching and please leave your comments below. Don't forget to press SUBSCRIBE my channel for more videos. Thanks for your support.
@RollinShultz
@RollinShultz 5 жыл бұрын
It serves no one for a programmer to take shortcuts in naming and other tasks to save typing. very descriptive names are essential to good coding. It is also essential to use subroutines to compartmentalize the code for simplicity in making changes and separating concerns. It is good to see someone make a video like this for VBA as I get the feeling these things are taken lightly by VBA programmers.
@mikhailk5690
@mikhailk5690 5 жыл бұрын
I didn't know about Enum, this can really make my code much more readable. Thank you so much for sharing these best practices!
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Glad you like it Mikhail
@walerij
@walerij 5 жыл бұрын
Why? What is the profit of Enum?
@mtotowamungu8259
@mtotowamungu8259 5 жыл бұрын
@@walerij docs.microsoft.com/de-de/office/vba/language/reference/user-interface-help/enum-statement
@GGAYTube
@GGAYTube 5 жыл бұрын
Yes nice tricks Enum
@nelsonrioux5555
@nelsonrioux5555 5 жыл бұрын
I was able to learn to code by myself but thanking for teaching me best practices. Deeply appreciate it.
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
You're welcome Nelson.
@rods6405
@rods6405 5 жыл бұрын
Been VBAing for 30years did not know about Ctrl+space autocomplete worked on a blank line thanks heaps! Have watched many of your videos and have implemented many changes to speed my code up thanks! Will watch all your videos now!
@dangelorrrr
@dangelorrrr 5 жыл бұрын
Enum - learn something new every time I watch your videos. Thanks, Paul!
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
You're welcome
@YKazimir
@YKazimir 5 жыл бұрын
Sir, one of the best Excel youtube channels.
@szabolcsjobbagy30
@szabolcsjobbagy30 2 жыл бұрын
Thank you for teaching the developers, these are very important principles.
@houstonvanhoy2198
@houstonvanhoy2198 3 жыл бұрын
6:41. Magic numbers, leading into demonstration of ENUMS.
@edwardlee9163
@edwardlee9163 3 жыл бұрын
Many thanks, it really helps! Appreciate your anonymous effort.
@grzegorz2852
@grzegorz2852 3 жыл бұрын
Paul, thank u very much for ur videos, they are so super useful. i have learned a lot from u.
@waverider80
@waverider80 5 жыл бұрын
Good stuff! Putting the dim statements within the code rather than at the top was new to me.
@officetricks6303
@officetricks6303 3 жыл бұрын
Continuously watching your video... Great topics and useful in vba.
@RichardCookerly
@RichardCookerly 5 жыл бұрын
Just found your channel...loving it! I’ve been a VBA Developer for about 7 years and I learn something from every one of your videos!
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Thanks Richard
@wayneedmondson1065
@wayneedmondson1065 5 жыл бұрын
Hi Paul.. these are 7 super tips.. really helpful and will implement them right away.. especially declaring variables where I use them and giving variables friendly names. When first learning, it seems correct to mimic what we see others doing.. so.. bad habits get passed through the generations. Thanks for this great advice to chart my own course and develop my own style. Thumbs up!!
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Thanks very much Wayne. It's true what you say about bad habits being passed.
@FulvioGaggioli
@FulvioGaggioli 2 жыл бұрын
Thank you for your work
@LPAtkins
@LPAtkins 5 жыл бұрын
I prefer to use i,o,p for nested loops. j looks too much like i and i,o,p are right next to each other. Great video.
@TP-om8of
@TP-om8of 2 жыл бұрын
I don’t like i and j either. I use k and l.
@rrrraaaacccc80
@rrrraaaacccc80 11 ай бұрын
Great 💯👍
@JimHenderson19
@JimHenderson19 4 жыл бұрын
I really have learned much from watching your videos.
@Excelmacromastery
@Excelmacromastery 4 жыл бұрын
That's great to hear Jim.
@tuyoexcelypowerbi
@tuyoexcelypowerbi 5 жыл бұрын
Very useful advices, thanks.
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
You're welcome
@trevorclack7178
@trevorclack7178 5 жыл бұрын
You can get a speed up in code execution by using full referencing as well (e.g. .shData.Range or VBA.Instr). I would also suggest having a unique name for all you VBA projects rather then using the default project name "VBAProject" in order to reduce errors if multiple projects are running at the same time.
@Trucpq
@Trucpq 5 жыл бұрын
It is valuable knowledge. So useful and so practical. Thanks a lot.
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
You're welcome Truc
@Mighty1072
@Mighty1072 5 жыл бұрын
Very informative. Thanks
@KcKc-bh6lu
@KcKc-bh6lu 5 жыл бұрын
Your suggestion is pretty good practice of variable declaration for VBA because of its interpreter nature. But in compiling languages, declaration must be predefined before any use.
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
I'm not sure what you mean. You can define variables as you use them in C# and C++.
@grigull
@grigull 5 жыл бұрын
A different color for parentheses and array values helps a lot :)
@kuldar.
@kuldar. 3 жыл бұрын
Thank you, your tips are really helpful!
@Excelmacromastery
@Excelmacromastery 3 жыл бұрын
You're welcome!
@tojtowny
@tojtowny 4 жыл бұрын
Still exploring what VBA can offer and how to use it, but deffinitly this makes the jurney easier :)
@teguhmarwanto7645
@teguhmarwanto7645 5 жыл бұрын
Its very good videos. Simple and clear
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Thanks Teguh
@dougtvedt890
@dougtvedt890 3 жыл бұрын
At 8:52 when using variables instead of cell references in a For/Next loop, should they be re-set back to 0 before iterating to the next i?
@danielszalok8540
@danielszalok8540 5 жыл бұрын
Thanks, now I understand why enum is better than constants in this special case! 🙂 A question: do you think 'With-End Width' helps readability? I use it very frequently, but it's so easy to overuse it. How about the readability of if, select case and iif? I would love to watch a video about this 🙂
@DarcyWhyte
@DarcyWhyte 4 жыл бұрын
Lover your videos. Quick question, when you click it makes little circles. Is that done in post processing or do you have a program that does that in real time?
@sislmira
@sislmira 5 жыл бұрын
Just amazing. Thank you for your videos and page.
@hotflashfoto
@hotflashfoto 2 жыл бұрын
I agree with your reasoning, but I don't use your execution regarding the declaration of variables. I use the top of the module to declare mine, grouping strings onto one or more lines, as well as other variable types. I also use the LNC, or a modified version of it, to add the type of variable to the front of the name and then supply a descriptive name. For example: Const cstrErrTitle as String = "ERROR MESSAGE TITLE HERE" Private wsMacro as Worksheet, wsSource as Worksheet, wsDest as Worksheet Private strName as String, strAddress1 as String Private lngCount as Long I do this instead of using Dim within each Sub, mostly because I want the value to travel as it's being used, rather than using it only within the Sub and then disposing of it. While I understand the purpose of declaring immediately before use, and it makes it easier to share the code, it also clutters up the code and restricts using the variable's value to that Sub only. I'm sure we won't see eye-to-eye on this, and I don't feel like you're teaching anything that needs to be corrected. It's just that the style of coding that I use is different.
@Anomander1
@Anomander1 5 жыл бұрын
Every vid from you is nice, thank you. I would not delcare variables when you need them, but declare them when you are at the top of your sub or Function. this makes it much easier to transition to another programming language where defining the variables at the beginning is mandatory.
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
In most modern languages it's not mandatory😀.
@grahamparker7729
@grahamparker7729 5 жыл бұрын
Very useful, many thanks 👌🏻
@BenjaminHouot
@BenjaminHouot 5 жыл бұрын
I don't use anymore "i" or "j" as a variable. I prefer to use "For Row = 1 to NbRows", Or "For Col = 1 to ..." It is more clear.
@joecortizo
@joecortizo 5 жыл бұрын
People sometimes underestimate the power of a macro... Thanks Master!
@urielramirez27
@urielramirez27 5 жыл бұрын
Very simple and powerful tips, thank you. Which software you use to record your tutorials?
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Camtasia
@oceanic14123
@oceanic14123 4 жыл бұрын
Great tutorial, thank you. I have a question regarding code behind userforms. I have Textbox, ListBox, ComboBox...with Click events, Functions, BeforeUpdate event, AfterUpdate event and Change event, the code is a mile long and disorganized. Is it possible separate, organize the code by Click event, Function, Update event in regular modules?
@bestscenes1469
@bestscenes1469 5 жыл бұрын
You're always awesome. Very useful video
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Thanks Daniel
@GersonCarhuapoma
@GersonCarhuapoma 5 жыл бұрын
Awesome, Paul, like all the time
@anthonycroft7538
@anthonycroft7538 5 жыл бұрын
Good stuff Paul, although the variable dimming in the body vs top of the procedure would be at the Brexit level of contentiousness within our profession.
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Appears so😂.
@xn3ko
@xn3ko 5 жыл бұрын
Excelente tips, thank you!
@stevennye5075
@stevennye5075 5 жыл бұрын
very informative!
@psychedelarte7257
@psychedelarte7257 5 жыл бұрын
Thanks for this
@Inexpugnablement
@Inexpugnablement 5 жыл бұрын
Thanks for your vids so interesting and useful ! If I dared, I'd suggest you an eighth: comments. It's so obvious but so necessary.
@rrrprogram8667
@rrrprogram8667 5 жыл бұрын
Beautiful... Loving this channel
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Thanks
@mtotowamungu8259
@mtotowamungu8259 5 жыл бұрын
Thank You
@cigmorfil4101
@cigmorfil4101 5 жыл бұрын
The use of I, J etc for loops is a remnant of Fortran where all variables starting with I to N are integers and the rest are floating point (unless explicitly declared). For a loop it would be done an exact number of times so integers would be used for the counter - if a floating point variable was used due to rounding the loop may be executed an extra time.
@gabiold
@gabiold 5 жыл бұрын
I always thought "I" just stands for "index"...
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Thanks for the info😀
@dominiquenoel939
@dominiquenoel939 5 жыл бұрын
In my case, first variable use in for-next loop if always "F" because in ZX81 / ZX spectrum it was same key: first push F to type "For" directly and second push for variable = F is more quickly.
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
That's a good reason. My spectrum was the +2 with a whopping 128k .
@davidr5847
@davidr5847 5 жыл бұрын
I really love your videos. I always considered myself a pro in VBA, but after seeing some videos of you I'm not that sure any more lol. However, sometimes I need to make trainingvideos myself for coworkers, what is the software you use for those red boxes to point out certain parts in the screen? I use Camtasia but I haven't found this feature.. Have a great day
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
They are under Annotations in camtasia.
@davidr5847
@davidr5847 5 жыл бұрын
@@Excelmacromastery Cannot believe I missed that. Thanks!
@rods6405
@rods6405 5 жыл бұрын
I avoid using I and L in code and in excel they look to much like each other and 1 in certain fonts. Test which the lowercase L or I in text I l ? even google font is hard to tell. For for loops I use XA XB XC etc
@Laxmanmane007
@Laxmanmane007 5 жыл бұрын
Also make video on power bi
@maciejdolinski1036
@maciejdolinski1036 5 жыл бұрын
Hi Paul, Thank you for great advises! Anyway, I wonder, why Enum is better than Constants? I used constants so far. Can you please comment?
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
1. You can group related constants using Enum. 2. You can use the enum as a type once you have created it. e.g. set it as a parameter type. 3. Intellisense shows all the available options when you use the enum in your code.
@ferreira8822
@ferreira8822 5 жыл бұрын
Great
@lolguy-x9n
@lolguy-x9n 5 жыл бұрын
I prefer variables at top not as I use. that way I k ow where to get tbem
@KhalilYasser
@KhalilYasser 5 жыл бұрын
Thanks a lot for the awesome hints
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Thank you Yasser.
@igalbitan5096
@igalbitan5096 5 жыл бұрын
Isn't there any tool to indent code automatically?
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Yes, but no part of the Visual Basic Editor.
@dimonovych
@dimonovych 5 жыл бұрын
There is Smart Indenter for VBA, which makes your code Indented by only clicking one button. Really convenient.
@KevD_
@KevD_ 5 жыл бұрын
I did not know that there was a checkbox in the settings to insist that all variables be declared. The first thing I have done for years, before starting a new module is type "OPTION EXPLICIT". I don't even understand why you are allowed to not declare variables! Shakes fist at Microsoft.
@MrWarlls
@MrWarlls 5 жыл бұрын
I prefer to declare the variables on the top of the function/sub. I think it is easier to find them. I am used to start all my variables that have the same type with the same letter (s for the string, a for the arrays, l for long...) For me, it is a good way to identify the king of variable without looking for the statement.
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
The variable naming you are referring to is Hungarian Notation which was used a lot in the 1990s and before.
@TP-om8of
@TP-om8of 2 жыл бұрын
I don’t indent my code, I exdent it.
@serdip
@serdip 5 жыл бұрын
Great video, as always. I disagree, respectfully, with the practice of declaring variables immediately before they're used because in some cases it will be necessary to create more variables than would actually be needed if the declarations were centralized at the top of the method. Below is an admittedly contrived example that illustrates why one cannot always declare variables immediately before they're used. =============================================================== Public Sub TestCode() 'Purpose: If current weekday is Monday, print weekday names 'excluding Monday, in ascending order by weekday number 'If current weekday is NOT Monday, print weekday names 'in DESCENDING order by weekday number 'Cannot always declare variables immediately before 'they're used Dim blnIsMonday As Boolean blnIsMonday = (Weekday(Date) = vbMonday) If blnIsMonday Then Dim i As Integer For i = vbSunday To vbSaturday If i vbMonday Then Debug.Print WeekdayName(i) Next i Else 'Dim i As Integer '
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Thanks for your comment. If you need extra variables then you code needs to be rewritten. In your example, the 2 for loops are not necessary. You can write the code so only one is required by using a variable for the start, end and step values. Then you only need one For loop no matter how many conditions.
@igotstoknow2
@igotstoknow2 5 жыл бұрын
Indenting under Sub is more annoying than helpful.
@rakhisaxena4352
@rakhisaxena4352 5 жыл бұрын
Thank you
5 VBA Hacks Everyone Should Know in 2021
11:20
Excel Macro Mastery
Рет қаралды 92 М.
5 More things I wish I Knew When I Started Using Excel VBA
13:31
Excel Macro Mastery
Рет қаралды 72 М.
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН
Enceinte et en Bazard: Les Chroniques du Nettoyage ! 🚽✨
00:21
Two More French
Рет қаралды 42 МЛН
Can This Code Be Saved?  | VBA Code Audit
10:58
Excel Macro Mastery
Рет қаралды 11 М.
VBA Error Handling Explained in Plain English (with examples)
23:13
Excel Macro Mastery
Рет қаралды 49 М.
How to Use Class Interfaces in Excel VBA
20:16
Excel Macro Mastery
Рет қаралды 83 М.
How to get the Last Row in VBA(The Right Way!)
15:41
Excel Macro Mastery
Рет қаралды 171 М.
Every Single VBA Feature in 10 Minutes
10:35
Excel Macro Mastery
Рет қаралды 7 М.
Python for VBA Developers in 30 Minutes
30:05
Excel Macro Mastery
Рет қаралды 22 М.
How Senior Programmers ACTUALLY Write Code
13:37
Thriving Technologist
Рет қаралды 1,7 МЛН
Object-Oriented Programming is Embarrassing: 4 Short Examples
28:03
Brian Will
Рет қаралды 2,1 МЛН
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН