5 VBA Hacks Everyone Should Know in 2021

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

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

  • @Excelmacromastery
    @Excelmacromastery  3 ปีที่แล้ว +13

    Let me know in the comments if you plan to use these hacks in your own code. Make sure to download the source code from the link in the description.

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

      The tips about referencing tables in the get range section is hugely helpful. I've got a project where I need to pull data from a certain column of a vendor-supplied spreadsheet, but some months one column gets removed, a column might get added, or the columns have been reordered for some reason so I never know which column I should be looking at (I can't just say "look at column G" because it won't always be that same column) and I have some unnecessarily complex code in my macro to find the right column - with this I can just convert it to a table and reference the header name, which is so much simpler and more flexible. I can use the array tip for reading data too to speed it up since it's often a lot of lines (sometimes 50K+). I actually think I have a few macros that I can take advantage of the array tip to read data faster for - definitely a good Monday morning project. :)

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

      I've always been using the Split function because its easy to handle with, now I'm using arrays and class modules that I learned from the previous videos of yours. Thanks for the hints Paul.

    • @marcogerblinger3717
      @marcogerblinger3717 3 ปีที่แล้ว +2

      Hi, nice tips for intermediate users.
      For the pros:
      As performance is always an issue with VBA, working in memory with arrays instead of Excel objects which are visible (e.g. cells) is way faster. Instead of writing each single array value in a cell just dump the array in a defined range the size of the array. This is x times faster as it is one single line. When manipulating large tables for future use, try creating an "Entry" class with the fields as attributes and put the instances in an array. This allows you to address everything easily and is extremely fast as VBA handels objects quite well. Best alternative, however, put it in a database and connect with ADODB to the DB using SQL. When considering Access as the DB alternative, connecting to Access via a Excel file with VBA, this is even faster than executing the code in Access.

  • @MudandoARotina
    @MudandoARotina 3 ปีที่แล้ว +48

    Hey paul, i got a promotion by studiyng with your videos!! Thank you!!

  • @ricos1497
    @ricos1497 3 ปีที่แล้ว +16

    I love the split function in VBA, I use it all the time, especially for parsing range addresses. A tip for others is that you can add a second set of brackets to return the split number, e.g. Split (s, ":")(1) to return the second item.

    • @AS-ym2bp
      @AS-ym2bp 3 ปีที่แล้ว +2

      I use it along with FullName or Path to navigate between folders

  • @seamushand8439
    @seamushand8439 3 ปีที่แล้ว +6

    I like how you use the watch window. I never thought of adding properties to it like the address query you added. Thanks

  • @mateuszbajko4342
    @mateuszbajko4342 3 ปีที่แล้ว +2

    Great work as always! I must admit I didn't know about checking properites in the watch window, so big plus to you! I have been using these tips for some time yet, but I saw them for the first time on your channel long ago :) I agree they were gamechangers.

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

    ty.
    these are always super helpfull when comming to a new syntax etc ^^

  • @MrSweck71
    @MrSweck71 3 ปีที่แล้ว +2

    A lot of your videos have changed my coding to the better... both to the eye and to the runtime stability. I've been doing this for almost 20, years but somewhere the learning curve phased out and I started to "do with what I had", which meant long detours instead of direct functions, ex nested loops and Mid to find strings instead of simple Instr (and really dangerous Resume Nexts just to get to End)... not to mention 'time'. Your quote "What people often do when they start VBA because they don't know any better", from your 1000x faster video, made me laugh... because I knew you were talking about me. I haven't "just started", but there's certainly a lot I should know better after 15+ years. Your videos have given me much and I'm sure that your coming ones will.

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

      Glad you have found the videos useful.

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

      @@Excelmacromastery I'm not the only one benefitting from your videos. So far at least 3 of the tools I've done for people at work have been updated since I stumbled on your collection of uploads. :)

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

    Thank you for another valuable video.
    I remember reading (rightly or wrongly) that Excel and VBA are in different memory processes, which creates a performance hit each time you go between them, so I always minimise traffic between VBA and the worksheet, reading all the sheet data that I need into arrays at the start, doing all the work in memory, then writing results back in as few statements as possible, ideally one figure or table.
    The other performance hack I think everyone should know is dictionaries.

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

      That's a very efficient way to do it Dermot. Make sure to check out my Arrays Class that adds a ton of functionality to arrays(th-cam.com/video/ZYxa-Q-nSX0/w-d-xo.html).

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

    I didn't know about the watch and the error-break hacks. These are really helpful in debugging, which often takes longer than the coding itself. I hope you have a video or article that focuses on debugging tricks (not the textbook methods) to quickly nail down problems.

  • @Tattysnuc
    @Tattysnuc 3 ปีที่แล้ว +6

    faster than a speeding bullet! Thank goodness for the pause & rewind buttons...

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

      My thoughts exactly!

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

      Yes. Try this: click on the gear (settings button) beneath the video, and change the playback speed to .75. It helped me a lot.

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

      @@houstonvanhoy2198 hmmm, 25% better!

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

      Paul, I like the pace of your tutorials … even though I occasionally adjust the playback speed as suggested by others.

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

      When you can watch without pause and rewind you are a true Jedi Master😉

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

    Thanks Paul. Love the watch window edit. I had no idea you could add .address to something while in the watch window.

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

    Wow, awesome video!!! Thank you so much for sharing these cool tips!!!

  • @JT-ky6ov
    @JT-ky6ov 3 ปีที่แล้ว +3

    What's that clsTimer you are using? Is it that one using the QueryPerformanceCounter and double integer values?

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

    تمثل اللغة تحدي جدي بالنسبة لي لكي اتابع فيديوهاتك ، فانا ابذل جهدا مضاعفا لكي احصل على الفائدة القصوى ، و مع ذلك استمر في متابعة منشوراتك لاني اعلم انك الافضل .
    شكرا بول .

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

    Hi Paul.. than you very much for all your lessons about vba excel. With all of those i can make my pos application much much more faster.

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

    Very insightful video 📹 Thank u for your hard work

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

    Whow! Once again: I learned a lot! The only thing I already knew about was the timing advantage of arrays over ranges. And I knew about list objects. But all other topics: well worth the watch! :-)

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

    That error handling hack would have saved me some grief in the past for sure.

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

    I liked the turn off error handling idea, but would it not be 'safer' to before the , the code will stop at i just befofre the issue row.

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

    I do a lot of work with HMI displays that utilize DHTML and scripting, the split function is invaluable for building strings that can be passed to the execute command or eval function.

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

    Hi Excel Mastery :) I`m not very advance in VBA. Could you tell me how I can use your examples from Immediate window in normal worksheet ? thx
    😁

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

    Hi Paul. Your teaching is nice. I learn some new things on watching each of your videos.

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

    Nice video. I regularly follow your video. Your teaching is so excellent.
    Please make a video- Excel data send through what's app. Data may be jpg, pdf, text accordingly.

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

    Thank you very much for your videos and instructions and tips. Not sure why, but the shortcuts to select regions, Ctrl * gives me a "Cannot show outline symbols" message. and the Ctrl Shift * gives me a "Printer Installer Client Console".

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

    As always, amazingly well explained!
    Edit:
    I'll use your Code but I need to understand the Magie, accordingly I'll write it by myself! Thanks alt sir!😉🤟😎

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

      You're welcome John.

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

      @@Excelmacromastery please excuse the auto-cerrecture- mistakes! Thanks for all your content! I enjoy every single minute!👍👍👍🤟😎 BR from Germany! Cheers!🍻

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

    Great Video
    A remark:
    In the Use Range macro, you should save the last row of the range in a variable, and then put: For i = 2 to LastRow. I think it would be faster.
    Greetings from Bolivia.

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

      Hi Jose, I haven't tested it but I don't know if it would really make much difference to speed. VBA might optimise the For loop for this.

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

    Thank you for this great video. Well explained and useful

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

      You're very welcome Jan! Thanks for your ongoing support.

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

    Great video, as always! Thanks very much for posting.
    When using LBound(arr) and UBound(arr) in the For loop header, does VBA reevaluate those functions at each pass through the loop or does it optimize performance by evaluating them only once? Generally, when I am looping through an array and I know the lower and upper bounds, I store those in variables such as inLBound and intUBound and use those variables in the construction of the For loop. Thank youl

  • @Powner187
    @Powner187 5 หลายเดือนก่อน

    Thanks for everything.

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

    Hi Paul can you please make a video on to update pivot table when user changes particular cells. Example I want to create a p& l iin excel with pivot table. I want to update it based on the financial period i.e from and to date. Dont want to do that with slicers but want to do it with excel vba. Thank you

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

    Hai Paul. Can you please explain how to fill a form in a website form using vba excel? Thank you

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

    Ive learned so much with your vids.... Thanks!

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

    Harika yöntemler. Teşekkür ederim. İyi Çalışmalar..

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

    Hi. In the last section you show error handling and debugging. How do you get the error handling to give the location of bad data?

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

    Hi Paul. As always, great tips! Thanks for sharing :)) Thumbs up!!

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

    Thank you Paul. Once again great video!!! :)

  • @planxlsm
    @planxlsm 2 ปีที่แล้ว +4

    00:00 - Introduction
    00:19 - Manipulating Text Hack
    02:32 - Debug Properties Hack
    04:30 - Get Range Hack
    07:58 - Read Data Faster Hack
    09:43 - Find Error Fast Hack

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

    Nice Video. Thank you.

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

    Hi Paul I have got a data in a serious of columns and row which change every time. What I want to achieve is I want to add each columns and if the result is different from zero then.
    In short if you could help me with the code that can loop across the column and sum it. In an if and then
    Many thanks

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

    Thank you for another fantastic video, I didn't know you could do that with the watch window! With the code Sheet1.range"A1:C1").value = split(s,":") (11:19) does this need the array to work or is the array element of this redundant?

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

      Thanks. Split will convert the string to an array and that get's written directly to the worksheet. You can store it in an array if you need to perform so extra steps.

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

    So if I have used the current region, an array can I compare each element from sheet 1 to mapping column name ( Order of columns can be different) from sheet 2 to highlight mismatches both ways. I am using power query using multiple join methods as separate queries.

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

    For the split I use
    For each item in split(string, "~")
    .......
    Next Item

  • @clandeszipp4564
    @clandeszipp4564 3 หลายเดือนก่อน

    I already knew these.

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

    Great tips, super useful. I will use this for sure !

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

    Thanks, this is was very useful

  • @Monochromatic_Spider
    @Monochromatic_Spider 3 ปีที่แล้ว +2

    The Split() approach is good. Extremely useful for doing what the name suggests, not just for debugging but also for merging and separating multiple data in a return value string, when you cannot be bothered doing something nicer.
    The Parent thing is also very nice to keep in mind. It could easily happen that all you have is a worksheet refeference, but actually you need to save the workbook. You could overhaul stuff and make that reference available or just go with Worksheet.Parent.Save. Also very nifty hack inside the hack to adjust the expression in the watch window.
    CurrentRegion is dangerous unless you know that your data will not contain empty rows or columns. Do use it if you can be certain of that but otherwise my feeling is that it has the potential to be one of those PITA sources of errors that is just not particularly obvious unless you're actively looking for it. Finding end points the traditional way seems safer to me.
    Using Excel tables is nice, if that is how the data is formatted, but keep in mind that Excel table columns cannot have the same name. And I have seen many, many data ranges in Excel that would then suddenly get renamed if you just force it to be an Excel table, so I prefer simply writing a bit more code over using Excel table specific code.
    The next hack, the array thing, is in my view not optimally described. The hack is not simply that data is read faster from arrays than from ranges or cells in the workbook. This part is trivial and fairly obvious. Reading from an array does not require any interfacing between VBA and the Excel spreadsheet layer. Reading from specific cells obviously must require that. The real hack here is in loading the entire data range into an array in a single read, which is such a monumental time saver when working data. You can do it with the CurrentRegion or just by using Range(cells(1,1), cells(eRow, eCol)), and now any single cell access will happen at array speed instead of spreadsheet speed.
    Lastly, the "halt on all errors" approach to stopping error handling from running away from the error. That is a good tool to remember, but of course the example code used was a bit on the rubbish side. One should aspire to write code that doesn't crash and burn just just because a data range had a string where a numerical value was expected, and if crashing is the right thing to do then a proper crash message should be composed. In a perfect world, there would even be enough time for that.
    One last thing to comment, please do not use these very relaxed and casual variable names that Paul is using. Just calling an array "arr" and declaring it as standard variant is like naming your kid "person". It is not a big deal in small examples but if you have 30+ variables with all kinds of junk in them then having half-decent naming of variables becomes nice. It is a bit more typing, but if you ever run into a legacy project with a few thousand lines of code then you'll hero-worship the predecessor that took that took the time to do that. Conversely, imagine having that legacy project but everything is a mix of a, b, c, aa, ab, ba, and so forth? And everything is just variant all over the place.

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

    Great hints! I was kinda lucky I knew them before, because our coach is awesome. But his availability is limited.
    I just learned about the split from him last month and it made my jaw drop.
    Speed of arrays is, I think, well known if you code a bit longer. But yeah, that was something I learned from him as well.
    DataBodyRange I've learned last month by myself (digging).
    I generally prefer to work on tables, because of power query and quality of life in general.
    CurrentRange is useful, but I somehow end up using UsedRange, because it also resets the scroll bar.
    It's useful in cases when you use same workbook during the month and with each day there's more and more data and then on the new month the sheet is barely filled. Without UsedRange the vertical scroll would be small like there was the data from whole month.
    The minus, or sometimes advantage, of UsedRange is that it would grab the range of all of the data, even the data after empty column. So for example cannot be used when next to the table there are supporting cells, such as =TODAY() and =MAX(ItemsDateColumn) - to get today's date and report's date.
    So both are useful in their own way.
    PS Due to the fact you do very great job at explaining things I have few recommendations:
    I am currently learning conversion from arrays to collections and from collections to arrays to print the results.
    It's so not intuitive to me and I still mostly copy the code and rewrite it for my own purposes.
    I could use some more hints. Especially adding new items to existing collections while in the loop.
    Probably that's because I still don't understand "New" before variable type. Too much knowledge in short time :D
    Eh... I've just checked your playlist... sorry. You have it all. So I better jump into it.
    Also Enums and Type various use cases would be awesome, too.
    I found that usage of Type makes code very clean in some cases - when you can group some parameters (strings, integers etc) into groups.
    I cannot find many use cases for Enums though :( but it's also not intuitive for me as most things I would like to put in Enums are strings with spaces and slashes, and that doesn't work.

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

      Thanks very much for your feedback. If you're not sure about enums then checkout the Range.Paste function. The parameters like xlPasteValues, xlPasteAll etc. are enums. There are actually lots of them within VBA.

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

      @@Excelmacromastery oh I understand the existing ones, which are built into excel. I kind of don't understand what and when is worth to put into Enum which is created by myself in Class module.
      I have already watched your 3 videos on classes and code architecture since yesterday. Such a dense but great stuff. Thank you!

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

    Final tip about turning error handling back on:
    Can this be done within VBA?

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

    Excellent tips!

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

    Very interesting ton link table and VBA. 🙂

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

    Hi sir,, how to implement mouse click and drag during running simulation using vba?

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

    Very nice video sir ! Thank you

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

    Amazing content, thank you!

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

    Awesome sir, thanks for knowledge update

  • @123mailashish
    @123mailashish 3 ปีที่แล้ว

    SIR Paul!!!

  • @Powner187
    @Powner187 5 หลายเดือนก่อน

    Hi! Error 50290 worked around? I can recreate the error by opening a drop down list and while it is open, click on a macro that takes me to a different sheet like shData.Select shData.Range("A1").Select
    Works unless dropdown list in cell is open. 50290 Error occurs.
    Any workarounds?

    • @Powner187
      @Powner187 5 หลายเดือนก่อน

      To make shape macro not bug out when drop down list is open, I effectively coded my own double-click for macro shapes in vba. Never found a single video to do so. Static, if Long = 1 then subtract 1, Mytime = hour(time) * 60 + minute(time) * 60 + second(time) exit sub elseif long = 0 then add1, mytime2 = same formula as mytime,
      Timediff = mytime2 -mytime end if
      If my time greater than 1 then exit sub
      Elseif timediff less or = To 1 then
      Call sub end if.
      That's my solution.

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

    Thanks

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

    Genious! That is so helpful

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

      I had a tool that takes templated ranges from worksheets, with place locators in it and creates text based ladder logic. I would go through many thousands of lines of tag information and replace the items from the tag worksheet into the placeholders in the templates, and then write the templated ranges out to a new workbook that could then be copied into the PLC program. . The original tool I built would run over night to complete, assuming we didn’t hit an error. After seeing your videos I converted all the code to use arrays instead, and some of your other optimization techniques, and it would complete the exact same task in a few minutes at most. I just never thought to look for a better way, and what you don’t know, you don’t think to look for it. Thanks again so much for your content Paul.

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

      That's great to hear. It's very true what you say. If you are new to VBA you may have no idea that you can use arrays to write your code much faster. Most online examples use range so you think that is the proper way.

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

    Great video. We want more hacks! :D

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

    People say VBA is dead. But it will be here as long as there is Excel. Hahaha.
    Btw, instead of doing loop, what's your thoughts on range.formula then insert excel formula using any worksheet.function? I used if to automate my vlookup function. Hahahaha

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

    The CONTENT of what you covered is Excellent, but THE SPEED in which you covered the material was UNREALISTIC for anyone to learn/understand. You both speak and move the mouse around @ 100 mph.

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

    Congratulations and many thanks. I am looking forward for your new video.

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

    Your videos are always great, however you should consider ditching this Windows 98 mouse you use and take a more silent one. Yout keys are silent but the mouse click is pretty annoying...

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

      lol - That's a sound effect not a real mouse.

  • @timwu-CMC-599T
    @timwu-CMC-599T 3 ปีที่แล้ว

    array is much faster than range.

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

    Paul, you really need to include some info on what references you are including (and those that cannot be included because of conflicts). Initially the code would not compile. Sorry if i missed it somewhere.

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

      The code should work as is. What error did you have?

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

    6. hack VBA = macro parameters
    7. hack VBA = click and hide / view

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

    Call me a dreamer, but one day I'm hoping that a worksheet will have a row zero for column headings. Preferably optional by worksheet. Maybe even negative numbered rows above that for the rubbish that idiot users put above actual data that should be in a header.

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

    I hate VBA. I've never understood it and I'm not even motivated to watch this video. Sorry.

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

      @Phoenixspin
      I do not mean to be unkind. Try doing other things for a few months, then maybe come back to VBA later. Or maybe never. Your path will present itself at some point.