7 Simple Practices for Writing Super-Readable VBA Code

Поделиться
HTML-код
  • Опубликовано: 15 янв 2025

Комментарии • 102

  • @MrDoomfighter
    @MrDoomfighter 5 лет назад +6

    my coworkers will have to thank you :)

  • @dangelorrrr
    @dangelorrrr 5 лет назад +3

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

  • @grzegorz2852
    @grzegorz2852 3 года назад

    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 5 лет назад

      Yes nice tricks Enum

  • @nelsonrioux5555
    @nelsonrioux5555 5 лет назад +2

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

  • @YKazimir
    @YKazimir 5 лет назад +5

    Sir, one of the best Excel youtube channels.

  • @officetricks6303
    @officetricks6303 3 года назад

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

  • @edwardlee9163
    @edwardlee9163 3 года назад

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

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

  • @houstonvanhoy2198
    @houstonvanhoy2198 3 года назад +1

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

  • @FulvioGaggioli
    @FulvioGaggioli 2 года назад

    Thank you for your work

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

  • @tuyoexcelypowerbi
    @tuyoexcelypowerbi 5 лет назад +2

    Very useful advices, thanks.

  • @kuldar.
    @kuldar. 3 года назад

    Thank you, your tips are really helpful!

  • @rrrraaaacccc80
    @rrrraaaacccc80 11 месяцев назад

    Great 💯👍

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

  • @teguhmarwanto7645
    @teguhmarwanto7645 5 лет назад

    Its very good videos. Simple and clear

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

  • @Mighty1072
    @Mighty1072 5 лет назад +1

    Very informative. Thanks

  • @Trucpq
    @Trucpq 5 лет назад

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

  • @JimHenderson19
    @JimHenderson19 3 года назад

    I really have learned much from watching your videos.

  • @sislmira
    @sislmira 5 лет назад

    Just amazing. Thank you for your videos and page.

  • @bestscenes1469
    @bestscenes1469 5 лет назад

    You're always awesome. Very useful video

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

  • @grahamparker7729
    @grahamparker7729 5 лет назад

    Very useful, many thanks 👌🏻

  • @GersonCarhuapoma
    @GersonCarhuapoma 5 лет назад

    Awesome, Paul, like all the time

  • @grigull
    @grigull 5 лет назад +1

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

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

  • @psychedelarte7257
    @psychedelarte7257 5 лет назад

    Thanks for this

  • @stevennye5075
    @stevennye5075 5 лет назад

    very informative!

  • @rrrprogram8667
    @rrrprogram8667 5 лет назад

    Beautiful... Loving this channel

  • @xn3ko
    @xn3ko 5 лет назад

    Excelente tips, thank you!

  • @mtotowamungu8259
    @mtotowamungu8259 5 лет назад

    Thank You

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

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

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

  • @tojtowny
    @tojtowny 4 года назад

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

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

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

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

  • @urielramirez27
    @urielramirez27 5 лет назад

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

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

  • @ferreira8822
    @ferreira8822 5 лет назад

    Great

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

  • @Laxmanmane007
    @Laxmanmane007 5 лет назад

    Also make video on power bi

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

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

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

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

  • @KhalilYasser
    @KhalilYasser 5 лет назад

    Thanks a lot for the awesome hints

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

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

  • @lolguy-x9n
    @lolguy-x9n 5 лет назад +10

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

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

  • @igalbitan5096
    @igalbitan5096 5 лет назад

    Isn't there any tool to indent code automatically?

  • @dimonovych
    @dimonovych 5 лет назад

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

  • @KevD_
    @KevD_ 5 лет назад +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.

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

  • @igotstoknow2
    @igotstoknow2 5 лет назад

    Indenting under Sub is more annoying than helpful.

  • @rakhisaxena4352
    @rakhisaxena4352 5 лет назад

    Thank you