7 Simple Practices for Writing Super-Readable VBA Code

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ต.ค. 2024

ความคิดเห็น • 102

  • @MrDoomfighter
    @MrDoomfighter 5 ปีที่แล้ว +6

    my coworkers will have to thank you :)

  • @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.

  • @grzegorz2852
    @grzegorz2852 2 ปีที่แล้ว

    Paul, thank u very much for ur videos, they are so super useful. i have learned a lot from u.

  • @mikhailk5690
    @mikhailk5690 5 ปีที่แล้ว +14

    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 4 ปีที่แล้ว

      Yes nice tricks Enum

  • @szabolcsjobbagy30
    @szabolcsjobbagy30 2 ปีที่แล้ว

    Thank you for teaching the developers, these are very important principles.

  • @RollinShultz
    @RollinShultz 5 ปีที่แล้ว +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.

  • @YKazimir
    @YKazimir 5 ปีที่แล้ว +5

    Sir, one of the best Excel youtube channels.

  • @officetricks6303
    @officetricks6303 2 ปีที่แล้ว

    Continuously watching your video... Great topics and useful in vba.

  • @edwardlee9163
    @edwardlee9163 2 ปีที่แล้ว

    Many thanks, it really helps! Appreciate your anonymous effort.

  • @dangelorrrr
    @dangelorrrr 5 ปีที่แล้ว +3

    Enum - learn something new every time I watch your videos. Thanks, Paul!

  • @nelsonrioux5555
    @nelsonrioux5555 5 ปีที่แล้ว +2

    I was able to learn to code by myself but thanking for teaching me best practices. Deeply appreciate it.

  • @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!

  • @waverider80
    @waverider80 5 ปีที่แล้ว +2

    Good stuff! Putting the dim statements within the code rather than at the top was new to me.

  • @FulvioGaggioli
    @FulvioGaggioli 2 ปีที่แล้ว

    Thank you for your work

  • @wayneedmondson1065
    @wayneedmondson1065 5 ปีที่แล้ว +1

    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.

  • @kuldar.
    @kuldar. 3 ปีที่แล้ว

    Thank you, your tips are really helpful!

  • @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!

  • @tuyoexcelypowerbi
    @tuyoexcelypowerbi 5 ปีที่แล้ว +2

    Very useful advices, thanks.

  • @Akens888
    @Akens888 5 ปีที่แล้ว +4

    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 ปีที่แล้ว

      I don’t like i and j either. I use k and l.

  • @JimHenderson19
    @JimHenderson19 3 ปีที่แล้ว

    I really have learned much from watching your videos.

  • @Mighty1072
    @Mighty1072 5 ปีที่แล้ว +1

    Very informative. Thanks

  • @teguhmarwanto7645
    @teguhmarwanto7645 4 ปีที่แล้ว

    Its very good videos. Simple and clear

  • @grigull
    @grigull 5 ปีที่แล้ว +1

    A different color for parentheses and array values helps a lot :)

  • @Trucpq
    @Trucpq 5 ปีที่แล้ว

    It is valuable knowledge. So useful and so practical. Thanks a lot.

  • @rrrraaaacccc80
    @rrrraaaacccc80 8 หลายเดือนก่อน

    Great 💯👍

  • @sislmira
    @sislmira 5 ปีที่แล้ว

    Just amazing. Thank you for your videos and page.

  • @tojtowny
    @tojtowny 4 ปีที่แล้ว

    Still exploring what VBA can offer and how to use it, but deffinitly this makes the jurney easier :)

  • @DarcyWhyte
    @DarcyWhyte 4 ปีที่แล้ว +1

    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?

  • @Anomander1
    @Anomander1 5 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      In most modern languages it's not mandatory😀.

  • @BenjaminHouot
    @BenjaminHouot 5 ปีที่แล้ว +13

    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.

  • @grahamparker7729
    @grahamparker7729 5 ปีที่แล้ว

    Very useful, many thanks 👌🏻

  • @hotflashfoto
    @hotflashfoto ปีที่แล้ว

    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.

  • @trevorclack7178
    @trevorclack7178 5 ปีที่แล้ว +2

    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.

  • @bestscenes1469
    @bestscenes1469 5 ปีที่แล้ว

    You're always awesome. Very useful video

  • @GersonCarhuapoma
    @GersonCarhuapoma 5 ปีที่แล้ว

    Awesome, Paul, like all the time

  • @houstonvanhoy2198
    @houstonvanhoy2198 3 ปีที่แล้ว +1

    6:41. Magic numbers, leading into demonstration of ENUMS.

  • @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++.

  • @joecortizo
    @joecortizo 5 ปีที่แล้ว

    People sometimes underestimate the power of a macro...
    Thanks Master!

  • @Inexpugnablement
    @Inexpugnablement 5 ปีที่แล้ว +3

    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.

  • @psychedelarte7257
    @psychedelarte7257 5 ปีที่แล้ว

    Thanks for this

  • @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 🙂

  • @xn3ko
    @xn3ko 5 ปีที่แล้ว

    Excelente tips, thank you!

  • @stevennye5075
    @stevennye5075 5 ปีที่แล้ว

    very informative!

  • @rakhisaxena4352
    @rakhisaxena4352 5 ปีที่แล้ว

    Thank you

  • @rrrprogram8667
    @rrrprogram8667 5 ปีที่แล้ว

    Beautiful... Loving this channel

  • @urielramirez27
    @urielramirez27 5 ปีที่แล้ว

    Very simple and powerful tips, thank you. Which software you use to record your tutorials?

  • @anthonycroft7538
    @anthonycroft7538 5 ปีที่แล้ว +1

    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.

  • @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?

  • @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 ปีที่แล้ว +1

      They are under Annotations in camtasia.

    • @davidr5847
      @davidr5847 5 ปีที่แล้ว

      @@Excelmacromastery Cannot believe I missed that. Thanks!

  • @cigmorfil4101
    @cigmorfil4101 5 ปีที่แล้ว +2

    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 ปีที่แล้ว +1

      That's a good reason. My spectrum was the +2 with a whopping 128k .

  • @Laxmanmane007
    @Laxmanmane007 5 ปีที่แล้ว

    Also make video on power bi

  • @ferreira8822
    @ferreira8822 5 ปีที่แล้ว

    Great

  • @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.

  • @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?

  • @rods6405
    @rods6405 5 ปีที่แล้ว +2

    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

  • @KhalilYasser
    @KhalilYasser 5 ปีที่แล้ว

    Thanks a lot for the awesome hints

  • @lolguy-x9n
    @lolguy-x9n 5 ปีที่แล้ว +10

    I prefer variables at top not as I use. that way I k ow where to get tbem

  • @dimonovych
    @dimonovych 5 ปีที่แล้ว

    There is Smart Indenter for VBA, which makes your code Indented by only clicking one button.
    Really convenient.

  • @KevD_
    @KevD_ 4 ปีที่แล้ว +1

    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.

  • @igalbitan5096
    @igalbitan5096 5 ปีที่แล้ว

    Isn't there any tool to indent code automatically?

    • @Excelmacromastery
      @Excelmacromastery  5 ปีที่แล้ว

      Yes, but no part of the Visual Basic Editor.

  • @TP-om8of
    @TP-om8of ปีที่แล้ว

    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.

  • @igotstaknow
    @igotstaknow 5 ปีที่แล้ว

    Indenting under Sub is more annoying than helpful.

  • @mtotowamungu8259
    @mtotowamungu8259 5 ปีที่แล้ว

    Thank You