I Made this VBA Code 2,880,952% faster

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 พ.ค. 2024
  • I Made this VBA Code 2,880,952% faster
    A user recently sent me Excel VBA code that was taking 6 hours to run and asked if I could somehow improve the speed. In this video I show how I made this code run in mere milliseconds with a few changes. If you want to avoid having slow VBA code in the future then this video is a must watch.
    💻🔧 #VBA #VBAFast #VBASpeed
    Join my course
    Effective Excel VBA
    ▸bit.ly/3wDYEnX
    SUBSCRIBE TO THE CHANNEL: bit.ly/36hpTCY
    DOWNLOAD THE CODE: shorturl.at/ltxLZ
    Useful VBA Shortcut Keys
    ========================
    Debugging:
    Compile the code: Alt + D + L OR Alt + D + Enter
    Run the code from the current sub: F5
    Step into the code line by line: F8
    Add a breakpoint to pause the code: F9(or click left margin)
    Windows:
    View the Immediate Window: Ctrl + G
    View the Watch Window: Alt + V + H
    View the Properties Window: F4
    Switch between Excel and the VBA Editor: Alt + F11
    View the Project Explorer Window: Ctrl + R
    Writing Code:
    Search keyword under cursor: Ctrl + F3
    Search the word last searched for: F3
    Auto complete word: Ctrl + Space
    Get the definition of the item under the cursor: Shift + F2
    Go to the last cursor position: Ctrl + Shift + F2
    Get the current region on a worksheet: Ctrl + Shift + 8(or Ctrl + *)
    To move lines of code to the right(Indent): Tab
    To move lines of code to the left(Outdent): Shift + Tab
    Delete a Line: Ctrl + Y(note: this clears the clipboard)
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @Excelmacromastery
    @Excelmacromastery  22 วันที่ผ่านมา +24

    Let me know in the comments below if you would like more videos like this👇

    • @mikheilchigogidze4224
      @mikheilchigogidze4224 21 วันที่ผ่านมา +2

      This is great. Thanks a lot! Can you please do a video, on how would you schedule an excel macro? Macro to run without opening an excel.

    • @jastonkong1409
      @jastonkong1409 21 วันที่ผ่านมา +2

      Your work is very inspiring. You keep me interested in learning VBA. Thank you so much for such selfless sharing! Also, never in a million year i would have thought that simple line of clearing contents could cause that much trouble! lol

    • @sweetdreams6697
      @sweetdreams6697 16 วันที่ผ่านมา

      I loved it

    • @luiss.l7904
      @luiss.l7904 15 วันที่ผ่านมา

      There is still one thing to work on:
      combined = data(i,2) & data (i,3) e data(i,4) has a N level of complexity
      This concatenation operation is done N(loop size) times unecessarily.
      Use worksheet function to vectorially create a single array with the already concatenated values of all rows.

    • @markd.9538
      @markd.9538 12 วันที่ผ่านมา

      This idea would have come in very handy for me about 4 years ago. I had to write complex (similar to this, but but non-trivial) scripts that ran across invoice support templates that checked and updated columns to the customer's very specific demands. The code had to look across about 120 columns of data, and the spreadsheets contained hundreds of thousands of rows. In the end it all worked but we had the same problem: the constant updates to the sheet during the process slowed it down to a crawl. Come due day, it would eat a huge chunk of the day for the person running and checking it. If I ever have to implement (or refactor) anything like this in the future, I'm going to be doing it your way! Thank you for the great advice here.

  • @HSkraekelig
    @HSkraekelig 21 วันที่ผ่านมา +12

    Paul, I've been using your web site for hints and tricks for years now. I probably would have stopped at the first fix, but the second is 200% more brilliant. Don't change what doesn't need changing! More please.

  • @Info-God
    @Info-God 21 วันที่ผ่านมา +5

    The most efficient combination. Dictionaries+Arrays.

  • @barrygeldham390
    @barrygeldham390 20 วันที่ผ่านมา +5

    Very interesting Paul. Just a rather pedantic point, 2.8 million percent is not 2.8 million times faster, only a mere 28,000 times faster. Still quite impressive.

  • @eshaich
    @eshaich 15 วันที่ผ่านมา +2

    Pro as usual. btw I believe you can do it even faster with the "evaluate" VBA function. no loops and it does the calculation for all the range
    ows at once.

  • @Elesario
    @Elesario 21 วันที่ผ่านมา +1

    An alternative solution would be to set up a temporary column with a function that can detect whether the item is the first of the group or not, and then use that column to filter the data and clear the visible cells in the first column. Then it's simple to remove the filter and the helper column. It would work best with the calculation mode set manual and triggering the calculation for only the point prior to filtering.
    In the example provided it's unlikely to be faster, but it might scale better (haven't tested). The reason I suggest this is because moving data between the Excel and VBA engines can be slow, so sometimes you get better performance directly from the Excel calculation system (which is multithreaded), and you're only pushing a few commands at it from VBA's side.

  • @dantealighieri8031
    @dantealighieri8031 21 วันที่ผ่านมา +2

    You make excel coding very enjoyable to learn. i dont even know half of what you did but it was intriguing.

    • @Excelmacromastery
      @Excelmacromastery  21 วันที่ผ่านมา

      Thanks. Glad you like it.

    • @Info-God
      @Info-God 20 วันที่ผ่านมา

      Dictionaries are a just like that: an array organised as you look at a ... dictionary. Try to organise a set of data as a ... dictionary....unique items.

  • @RobGijrath
    @RobGijrath 20 วันที่ผ่านมา +1

    That is very efficient. Please show us more videos like this.

  • @BrittKinsler
    @BrittKinsler 20 วันที่ผ่านมา +1

    Excellent video and looking forward to more! Thanks!

  • @janezklun
    @janezklun 6 วันที่ผ่านมา

    Thank you Paula for such useful videos, yes more

  • @hammeedabdo.82
    @hammeedabdo.82 22 วันที่ผ่านมา +1

    Thank you Paul, we need more videos like this.

  • @stevegarside5026
    @stevegarside5026 21 วันที่ผ่านมา +1

    Fantastic coding and described in a way that people can understand. Thanks for sharing!

  • @kapibara2440
    @kapibara2440 21 วันที่ผ่านมา

    Smart and clean. Great work Paul! Your videos are valuable 🎉

  • @user-xx5et3fj7b
    @user-xx5et3fj7b 21 วันที่ผ่านมา

    That was splendid indeed! Thank you very much!

  • @Tocotronic000000
    @Tocotronic000000 21 วันที่ผ่านมา +1

    Fantastic tutorial. We need more like this. Keep up the excelent Work. Greetings from germany

  • @nadermounir8228
    @nadermounir8228 20 วันที่ผ่านมา

    Brilliant video Thank you very much for your effort and for showing us a real world example. I am currently enrolled in your course (just started)

  • @sudheerallamuri7132
    @sudheerallamuri7132 19 วันที่ผ่านมา

    Thank you Sir.. I like your Vedios ❤❤

  • @joeburkeson8946
    @joeburkeson8946 22 วันที่ผ่านมา +1

    Easier & faster what's not to like? Another fine tutorial with real world application, thanks.

  • @dermotbalson
    @dermotbalson 18 วันที่ผ่านมา +1

    My preference is never to overwrite any original data, both because making an error may lose it forever, and also because in business, work needs to be checked, and you can't do that without the original data.

    • @notaras1985
      @notaras1985 14 วันที่ผ่านมา

      There's zero reason the data will be lost

  • @iincitr
    @iincitr 21 วันที่ผ่านมา +1

    Amazing. Thank you.

  • @benlinford_work
    @benlinford_work 22 วันที่ผ่านมา

    Love it, Paul!

  • @brianminder2561
    @brianminder2561 21 วันที่ผ่านมา

    Excellent! I vote for more.

  • @egozMaster
    @egozMaster 17 วันที่ผ่านมา +1

    you should remove the With statement as well

  • @mylife1131
    @mylife1131 20 วันที่ผ่านมา

    Harika video için teşekkür ederim Paul.

  • @rajuhubli2
    @rajuhubli2 14 วันที่ผ่านมา

    Great video sir

  • @sandeepkothari5000
    @sandeepkothari5000 21 วันที่ผ่านมา

    SURE, WE LIKE YOUR VIDEOS immensely & wany more

  • @Bhuvan_C_Paranji
    @Bhuvan_C_Paranji 19 วันที่ผ่านมา +1

    Hi Paul, I have one issue from Power Bi data, Its taking long time to execute. please give me any idea to lessthan 1 minute to get the power bi data with filter options.

  • @sriranga188
    @sriranga188 16 วันที่ผ่านมา

    Yes we need more videos

  • @sasifexcelsolutions4509
    @sasifexcelsolutions4509 4 วันที่ผ่านมา

    Hi, thanks for this, question, how would I force only xlsb when the user want to save as a file, I want the dialog picker or with 365 only show xlsb to show, this was possible before 365 but don't know how to do it in 365

  • @strannostrannovasrr
    @strannostrannovasrr 20 วันที่ผ่านมา +1

    Is it possible to dump only 1 column from a multi column variant array on a worksheet at once? I mean without using a for-next-loop?

    • @Excelmacromastery
      @Excelmacromastery  20 วันที่ผ่านมา +1

      You can only do it with the first column. Resize the output range to the size of this column. Range(rowCount,1) = arr

  • @stevetatterton
    @stevetatterton 20 วันที่ผ่านมา

    Do you need to use LBound of the arrays when it will be 1 unless you specify otherwise, which you don't do here as it's not needed? Or is iterating from LBound to UBound seen as more elegant? I would find 1 easier to use as the For statement is then less cumbersome.

    • @Excelmacromastery
      @Excelmacromastery  20 วันที่ผ่านมา

      Using lbound is a defensive measure in case a zero based array used. But you'll get away using 1 in most cases.

  • @kofigyebiadusei2205
    @kofigyebiadusei2205 15 วันที่ผ่านมา

    When will make an introductory video for us who are beginners. Thank you

  • @JithinDevan
    @JithinDevan 22 วันที่ผ่านมา +2

    Thank you Paul - is there any way to speed up below requirement?
    I need to list all filenames from a folder and their last modified dates into a table. The folder has over 20,000 files and it’s taking ~5 mins to run the macro.

    • @sinus2220
      @sinus2220 21 วันที่ผ่านมา +2

      You can call Shell and run DIR command from there.

    • @Excelmacromastery
      @Excelmacromastery  21 วันที่ผ่านมา +2

      I think that would be fast. You can easily send to a file

    • @Back1Ply
      @Back1Ply 21 วันที่ผ่านมา +1

      Power query has from folder connector that lists files and last modified date

    • @JithinDevan
      @JithinDevan 21 วันที่ผ่านมา

      Thank you all for suggestions

    • @PIESvcs
      @PIESvcs 21 วันที่ผ่านมา

      When I see this, I'm reminded that the Data, Advanced Filter allows Unique data items to be filtered in place or copied to another location (using header rows, I think).
      I am wondering if this may be faster using the native Excel operability.
      One question I had (and benefit to using Paul's method) is if we want to know the number of occurrences for each Unique entry, an additional array may be useful, for example, if all of those monitors were to be assigned their serial numbers, assigned to users, maintained and depreciated. I needed to do this for network hardware in 1988 using QuickBASIC before VBA.

  • @daves4026
    @daves4026 13 วันที่ผ่านมา

    Bravo

  • @pupazzetti2680
    @pupazzetti2680 20 วันที่ผ่านมา +1

    Yet you could make it even faster by using `Value2` (as you have previously taught in other videos). Just for curiosity, how much faster?

    • @Excelmacromastery
      @Excelmacromastery  18 วันที่ผ่านมา

      It would only make a difference if you had a large number of writes.

  • @elecirlisboa9155
    @elecirlisboa9155 20 วันที่ผ่านมา +1

    Excelente vídeo parabéns, aprendi como reduzir o tempo em algumas macros, obrigado!!

  • @richardgreves6161
    @richardgreves6161 20 วันที่ผ่านมา +2

    tried to download the code and I get Web server is returning an unknown error

    • @Info-God
      @Info-God 19 วันที่ผ่านมา

      Type it. This way you learn.

  • @wizardofaus8473
    @wizardofaus8473 21 วันที่ผ่านมา

    More videos like this please. Also, more videos not like this please. Basically, I would like more videos, as long as they are you teaching VBA. Thank you Paul

  • @nivrin13
    @nivrin13 21 วันที่ผ่านมา

    How do you calculate the speed of the macro?

    • @Excelmacromastery
      @Excelmacromastery  21 วันที่ผ่านมา

      Using the Windows api timer

    • @Info-God
      @Info-God 20 วันที่ผ่านมา

      Define TStart and TEnd, then the difference is the TLapse

  • @rahul7rock
    @rahul7rock 22 วันที่ผ่านมา

    Dictionary and Array is a deadly combination as always

  • @YO-BIZZY
    @YO-BIZZY 21 วันที่ผ่านมา +1

    MATT PARKER OPTIMISES CODE

  • @mback3713
    @mback3713 21 วันที่ผ่านมา +1

    Try using Collection instead of Dictionary. ;)

    • @Excelmacromastery
      @Excelmacromastery  21 วันที่ผ่านมา

      Why? They won't be faster.

    • @nivrin13
      @nivrin13 21 วันที่ผ่านมา +1

      Collection allows double entries, dictionary doesn't, so it's already filtering the data

    • @mback3713
      @mback3713 19 วันที่ผ่านมา

      @nivrin13 um.... collection will error if you try to retrieve an element from it that is not in the collection. you use `on error resume next` then in your loop check the collection `_ = col[key]`, then switch to do something `if err.number then` and don't forget to `err.clear` after failing to retrieve a value. it works... a little hokey, but is faster than `if key in dict`

    • @mback3713
      @mback3713 19 วันที่ผ่านมา

      @Excelmacromastery collection is "native" and doesn't require an import - and is more lightweight and faster than dictionary. It is a little "hokey" as you need to use error handling (the native "Err" object) to detect if a key/value pair exists in the collection, but... try it... sounds like you may be a little surprised at the result.

    • @nivrin13
      @nivrin13 19 วันที่ผ่านมา

      @@mback3713 Alright. I actually wanted to use the collection method in the past. This is a better approach. Will save it. Cheers!

  • @Hughahugha361
    @Hughahugha361 21 วันที่ผ่านมา +1

    The code is nearly as fast as you deleting comments...

    • @Excelmacromastery
      @Excelmacromastery  21 วันที่ผ่านมา +2

      I haven't deleted any comments😀

  • @user-gj3kz7cm3x
    @user-gj3kz7cm3x 20 วันที่ผ่านมา +3

    How to improve it further: use literally anything but VBA.

    • @junkertom7766
      @junkertom7766 12 วันที่ผ่านมา +1

      Tell me you are a clueless VBA hater without telling that you are a clueless VBA hater.

    • @user-gj3kz7cm3x
      @user-gj3kz7cm3x 12 วันที่ผ่านมา +1

      @@junkertom7766 it is literally ranked the worst programming language. Personally I don’t think that is fair because it isn’t a real programming language.

    • @laurentbosc2896
      @laurentbosc2896 9 วันที่ผ่านมา

      @@user-gj3kz7cm3x ok and what else ? in the real World Vba is probably one of the most used. So this is a very simple view to tell that. And it should ont be taken alone but as one element of the Excel toolbox. With Power Query and power pivot, it gives powerful possibilities very easily, without any extra installation. This is why it is very used and so popular.

    • @laurentbosc2896
      @laurentbosc2896 9 วันที่ผ่านมา

      And it is a programming language, literally speaking… not comparable to others ok, but it is powerful for other reasons as explained

  • @MJLCabral
    @MJLCabral 21 วันที่ผ่านมา

    FANTASTIC!!!!! More videos!!!!!