How to Use Arrays Instead of Ranges in Excel VBA

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 2. 08. 2024
  • Learn how to write real-world Excel VBA code: 👉courses.excelmacromastery.com/
    FREE CHEAT SHEET: Get the free cheat sheet on VBA arrays here: (bit.ly/2MXsnz9)
    Subscribe to the channel here: bit.ly/36hpTCY
    How to Use Arrays Instead of Ranges in Excel VBA
    Most people don't realise that Arrays are exponentially faster than Ranges in Excel VBA.
    In this video I cover in detail how to replace ranges with arrays and make your code run faster.
    Useful VBA Shortcut Keys:
    View the Immediate Window: Ctrl + G
    View the Watch Window: Alt + V + H
    Tab: To move lines of code to the right(Indent).
    Shift + Tab: To move lines of code to the left(Outdent).
    Shift + F2: Get the definition of the item under the cursor.
    Ctrl + Shift + F2: Go to the last cursor position.
    Ctrl + Space: AutoComplete Word.
    Alt + F11: Switch between Excel and the VBA Editor.
    Ctrl + G: view the Immediate Window
    Ctrl + R: View the Project Explorer Window.
    Ctrl + Shift + 8(or Ctrl + *): Get the current region on a worksheet.
    F4: View the Properties Window.
    F5: Run the code from the current sub.
    F8: Step into the code.
    F9(or click left margin): Add a breakpoint to pause the code.

Komentáƙe • 261

  • @Excelmacromastery
    @Excelmacromastery  Pƙed měsĂ­cem

    Learn how to write real-world Excel VBA code: 👉courses.excelmacromastery.com/

  • @BenjaminHouot
    @BenjaminHouot Pƙed 4 lety +57

    Best vba channel i ever found !

  • @TimePasser
    @TimePasser Pƙed 3 lety +2

    This is exactly what I am looking for. I work in telecom industry. It took me hours to process raw data.. now .. down to less than 20 mins with a humble system. Thank you .. where have you been 10 years ago ? I can't thank you enough .

  • @mike_case
    @mike_case Pƙed 4 lety +9

    Thank you Paul for all your content!!!
    Dictionaries, arrays, collections and classes are the best.

  • @haiderbokhari1
    @haiderbokhari1 Pƙed 4 lety +6

    Amazing tutoring as always! Keep these videos coming!! They're making a world of a difference to my (currently limited) VBA skills.
    I had a thought, would you consider doing a video on how to use VBA generically? Like copy+paste files across different folders, or using VBA to send a standard email message? Not pushing for this but thought it could be interesting. Thanks!!

  • @stevennye5075
    @stevennye5075 Pƙed 4 lety +3

    I am self-taught and I didn't fully grasp this content the first couple of times I looked at it. I decided I needed to better understand it and it now makes sense.

  • @davidevecchio2474
    @davidevecchio2474 Pƙed 3 lety +1

    Thanks a lot! A very clear explanation that addressed exactly what I was looking for. You're a great teacher!

  • @krn14242
    @krn14242 Pƙed 4 lety +2

    Great job Paul. Love using arrays now. So much more efficient.

  • @gonzaortin739
    @gonzaortin739 Pƙed rokem +8

    The information you give us is so clean, easy-to-understand and implement
 I just love your channel, Im hoping to buy the handbook soon

  • @johnhackwood1568
    @johnhackwood1568 Pƙed 4 lety

    Another really 1st class video, really simple and robust code that covers so many practical situations

  • @terryy6938
    @terryy6938 Pƙed 3 lety +1

    Absolutely a clear and concise explanation of using arrays. Well done! You gained another subscriber.

  • @querrythis
    @querrythis Pƙed 2 lety

    Incredible how efficient you codes are, yet explain it so simple. Genius!

  • @natejewkes
    @natejewkes Pƙed 4 lety +3

    Where have you been all my life? This is awesome!

  • @fmichaelhemmer3762
    @fmichaelhemmer3762 Pƙed 2 lety +3

    Absolutely fabulous instruction. Seeing the array filled with one line of code and then watching it crunch 2500 records in a blink, was the most fun I've had in a while... sad, but still fun! Thank you Paul.

  • @jastonkong1409
    @jastonkong1409 Pƙed 3 lety +1

    Clear and concise, I love it!!!

  • @grzesiekhallomoto5920
    @grzesiekhallomoto5920 Pƙed 4 lety

    Magnificent.... I have very complex range operations which take my program 281 seconds to perform. With this method I reduced the time to under 5%.... Best ever YT content I've encountered. Keep doing, don't stop. Ever! It has the potential to be great...

  • @Mandelbrot567
    @Mandelbrot567 Pƙed 4 lety +1

    I have done some of this before, but not the resize aspect. Great tips, thanks Paul.

  • @cortinas54
    @cortinas54 Pƙed 4 lety +4

    as always all your videos are amaizing and greats!

  • @luismoreyra6804
    @luismoreyra6804 Pƙed 2 lety +1

    Amazing!! I watched tons of vba videos (I'm newbie) and by far this video gives a incredible edge because of its clear and concise information. Excellent!! Great work, please keep it up!!

  • @jxggxr_dxv
    @jxggxr_dxv Pƙed 4 lety +2

    I like the format of your videos and the way you explain things. Even if I know some of them, I still watch just because you're one of the very few on youtube that is actually enjoyable to listen to.

  • @b3lg4r4th8
    @b3lg4r4th8 Pƙed 4 lety +2

    Excellent video.I can see the value of arrays, as opposed to ranges, and am in the process of changing one of my user forms.However, what the video does not cover and I am interested in is the following:Placing the data from the user form back into the table/spreadsheet.

  • @andrewbeeby2887
    @andrewbeeby2887 Pƙed 4 lety +1

    Great VBA teacher, I've tried various different tutorials and keep coming back here!

  • @cherifnadjib3373
    @cherifnadjib3373 Pƙed 4 lety

    From Algeria, thank you Paul for this vidéo.

  • @EduardoRibeiroEdu
    @EduardoRibeiroEdu Pƙed 4 lety

    Thanks Man! you and Leila are the best in excel tips!

  • @m-squaredcontractors9720
    @m-squaredcontractors9720 Pƙed 4 lety +1

    Wow...i now really appreciate arrays. Very efficient compared to ranges. Thanks Paul

  • @Akens888
    @Akens888 Pƙed 4 lety

    Thanks for uploading this Paul.

  • @Neckbeef82
    @Neckbeef82 Pƙed rokem +1

    Has to be the best vba tutorial I've seen on CZcams. Lots of garbage stuff out there. I'm a self taught VBA user, picked up a few very helpful codes and ideas watching this. Thank you

  • @coryc9040
    @coryc9040 Pƙed 4 lety

    From someone putzing around with VBA for a few years, finding R Studio was a godsend. Perhaps there are special instances where you might have to use Excel, but I'd advise anyone who thinks they should spend time learning to code VBA to give R a try.

  • @minhphan3580
    @minhphan3580 Pƙed 4 lety

    You sir, are next level. AMAZING!!!

  • @franciscovwilliams
    @franciscovwilliams Pƙed 4 lety

    Amazing explanation! Congrats!

  • @peajayjay3622
    @peajayjay3622 Pƙed rokem

    Absolutely loved this tutorial! Thank you sir!

  • @Victor-ol1lo
    @Victor-ol1lo Pƙed 4 lety

    Great video !! Thanks for sharing with us !

  • @user-yk2sg5pd5g
    @user-yk2sg5pd5g Pƙed 4 lety

    Paul, Thank you for great instruction. Is it possible if I do samething with a recordset pulled out of query?

  • @alexkim7270
    @alexkim7270 Pƙed 4 lety +1

    Thank you so much! This is great.

  • @mangobrother
    @mangobrother Pƙed 4 lety

    Thank you so much. Great video. I am learning a lot. Very useful tips.

  • @frikduplessis3869
    @frikduplessis3869 Pƙed 4 lety

    Thank you Paul a brilliant video again,

  • @kman3d
    @kman3d Pƙed 2 lety +1

    Thank you Paul! I was wondering if it is possible to get other cell attributes (like cell background color) and not only cell value into an array ( without iterating through the entire range)? I have a very large range which I need to get the background colors of each cell, and it takea very long to run...

  • @redfeather22sa
    @redfeather22sa Pƙed 2 lety

    Best Video on the Interweb , Internet & Galaxy !! Star Paul !! Your an Amazing Star !!

  • @manassehkisame6286
    @manassehkisame6286 Pƙed 2 lety

    This video got me up a high wall...by making my code cut thru a dead end(error) and in a fast way...Genius!

  • @csemanate
    @csemanate Pƙed 4 lety

    Learning a lot. Thanks!!

  • @bonniebaker6226
    @bonniebaker6226 Pƙed 2 lety

    Loving your videos. A lot of my data is in word documents and I want to use VBA to move it to excel. Is there a similar way to copy a word table into an array without looping through all cells or using a copy/paste command?

  • @priyankagupta951
    @priyankagupta951 Pƙed 3 lety

    A million thank you, could you please suggest if we substarct same value what you have mention but I want to add column in the end and calculation value mention . Can we do that?

  • @hericklenin
    @hericklenin Pƙed 4 lety +1

    Thanks a lot, this is awesome.

  • @SuiGenerisGuy
    @SuiGenerisGuy Pƙed 4 lety

    I stumbled upon something quite interesting when playing around with this. If you try to set an array, it will return the properties of each cell in a range instead of the value of the cells in the range. Very useful to get a number of properties information

    • @Excelmacromastery
      @Excelmacromastery  Pƙed 4 lety

      If you use Set with a variant to a range it will return the range rather than the array.

  • @MrAshfaqahmed
    @MrAshfaqahmed Pƙed 3 lety

    Great work Sir, you real world examples are so fascinating. I have a question to in the same situation.

  • @NabhdeepBansal
    @NabhdeepBansal Pƙed 3 lety

    Hello. Wonderful lecture! Thanks.
    My question: is there a way by which I store my database table in an array when my workbook opens AND use/call the array at different times wherever required??

  • @rrrprogram8667
    @rrrprogram8667 Pƙed 4 lety

    Awesome paul.... U r the master of vba... Great to have u as a teacher

  • @choijaeho653
    @choijaeho653 Pƙed 2 lety

    Thank you so much for the very useful tips using arrays in vba!! :)

  • @scotolivera8207
    @scotolivera8207 Pƙed 4 lety

    Amazing teacher, each video I watch I learn a new trick. Thanks a lot

  • @MGE0007
    @MGE0007 Pƙed 4 lety

    Good stuff.... taking baby steps with my VBA project but every step is in the right direction with these videos!!... (oh and of course I have liked this video!)..

  • @gd6noob
    @gd6noob Pƙed 3 lety

    Always loved watching your videos.
    Just a quick question.
    How can I adjust this to lets say I want Columns A, C to E.

  • @1yyymmmddd
    @1yyymmmddd Pƙed 4 lety

    Great video ! Thanks !!!

  • @verkar1965
    @verkar1965 Pƙed 4 lety

    hi thanks for your videos... is it possible to redim preserve arr(1 to x,1) when arr is a variant ? thanks

  • @hemant0088
    @hemant0088 Pƙed 4 lety

    Awesome content. Keep sharing useful tips and tricks.
    It is much helpful for developers life. :) :)

  • @eduarchavarria
    @eduarchavarria Pƙed 4 lety +2

    This is a fascinating topic. I think I can adapt this to copying non-contiguous ranges from one workbook to another, but I'm not quite sure.

  • @danielgomezmendoza4851
    @danielgomezmendoza4851 Pƙed rokem

    You helped so much, thank you!

  • @mthwmwn7844
    @mthwmwn7844 Pƙed 4 lety

    Great video as usual. Quick one. I've been coding VBA for approx 5 years and consider myself a moderate to advanced user. One thing I encounter when setting ranges and pasting arrays back it pastes values and I loose formulas. Is there a method of inputting formulas into the array when it is pasting back out?

  • @niczoom
    @niczoom Pƙed 3 lety

    Great tips, thanks!

  • @sonderfall1979
    @sonderfall1979 Pƙed 4 lety +1

    BEST VBA ADVICE EVER!!!! Wow, you are awesome :-)

  • @alesloom
    @alesloom Pƙed 4 lety

    Thank you very much. I have a question: what is the best way to delete a row of data from array without changing the range

  • @tomculhane7353
    @tomculhane7353 Pƙed 2 lety

    Paul, that's really excellent. I have used arrays like this before but your code is so short. I didn't know you could write the data by saying "= arr". I will use that a lot from now on.
    In your demonstration, the Amount values written to column L are not the same as read from column E. Is there some data type issue here do you know?
    Thanks

  • @entropyincrease1
    @entropyincrease1 Pƙed 2 lety

    You're a beast. Your content is the best.

  • @garyknight6766
    @garyknight6766 Pƙed 4 lety

    Always great stuff, very well presented

  • @donbogdala5428
    @donbogdala5428 Pƙed 2 měsĂ­ci

    Awesome work!

  • @adammorait7429
    @adammorait7429 Pƙed 3 lety

    Many thanks, excellent and well explained, it is crystal clear, your video helped me a lot.

  • @davidlinton4127
    @davidlinton4127 Pƙed 3 lety

    Great stuff Paul, big fan From Florida USA....

  • @smanzoli
    @smanzoli Pƙed 2 lety

    Thaks!!! Have just changed the code in one of my macros to use arrays. I go through 200k lines in a table. Before, it took 3m20s, now it finishes in 12s, 16x faster!

  • @michaelalbry
    @michaelalbry Pƙed 2 lety +1

    This is a minor thing: My attempt to transfer range logic to array logic failed, because I used "for each"-loops to access the cells in the range. This is not working. So I just have to rebuild my code around two dim array logic. The speed gain is still unbelievable, and I am more than grateful for this contribution. Originally I thought that for each logic should work as in the following code e seems to be a reference to the range/cell element: for each e in myRange e.value="Changed" next e. This would result in a cell value of "Changed" throughout the whole range. Any suggestion, how to transfer that?

  • @mahdizahzah51
    @mahdizahzah51 Pƙed 4 lety +2

    Mille merci Mr
    Malgré que je suis trÚs faible en anglais mais j'essaye le maximum de vous suivre car je sais bien que votre vidéo est trÚs utile

    • @BabyBugBug
      @BabyBugBug Pƙed 4 lety

      mahdi zahzah je vous applaudis d’avoir pris le temps pour comprendre ce qui n’est Ă©videment pas facile Ă  apprendre surtout en une autre langue. Continuez de faire de votre mieux et je vous assure que vous rĂ©ussirez!

  • @redangrybird7564
    @redangrybird7564 Pƙed rokem

    Mr Kelly, you are a champion 🏆 😎👍

  • @veebee3969
    @veebee3969 Pƙed rokem

    Thank you. Great video.

  • @shrutikadongre1046
    @shrutikadongre1046 Pƙed 2 lety

    Thankyou so much for this tutorial, really helped me a lot! and saved my time.. thanks a lot yet again!! :))

  • @israelgonzalez677
    @israelgonzalez677 Pƙed 2 lety

    Awesome! Can we access to the cell properties and methods within the array?

  • @Dr.RiccoMastermind
    @Dr.RiccoMastermind Pƙed rokem

    Hey Paul, amazing Tutorials of yours!! Will this code also adapt to added rows within oder at the end of the original table, as it does with added rows?đŸ™đŸ˜ŽđŸ‡©đŸ‡Ș

  • @ShivaKumar-xo4hw
    @ShivaKumar-xo4hw Pƙed 4 lety

    Thanks for your Awesome Video Sir. We are selecting the around 5 columns in array and copy, pasting it to different columns arr with same 5 columns. But I have different scenario. So my question is can I select 5 columns in array, copy, paste only any 3 column Eg. Column A, B, E.. Only by using array. Please help me Or Give me alternate idea

  • @tymothylim6550
    @tymothylim6550 Pƙed 4 lety

    Thank you for the video :) It was helpful for me.

  • @lindsaydempsey5683
    @lindsaydempsey5683 Pƙed 3 lety

    Can you have a UDF in an Add-in and extract information from a named range within the same Add-in from one of the internal worksheets? Or is the vba module the only part of the Add-in that can be used/accessed?

  • @Monduras
    @Monduras Pƙed 2 lety

    thanks paul - one question - how would you use array if you want to grab ranges that are not congruent (i.e. range(a:a), range(c:c), etc.) in one go? it seems that array = range doesn't like this. i've tried to use union to do that but no luck.

  • @Ganesh749
    @Ganesh749 Pƙed 4 lety

    Thank you Paul for sharing great stuff about VBA. I am avid reader of your website content excelmastery. can you please write a or share stuff about excel vba with access connectvity

  • @shumzbeazley7056
    @shumzbeazley7056 Pƙed 3 lety

    Thank you 💜

  • @shafiqarzayeva8799
    @shafiqarzayeva8799 Pƙed 4 lety

    Thanks a lot!!!))

  • @TheJaebeomPark
    @TheJaebeomPark Pƙed 3 lety

    Awesome!

  • @shaikhaziz9703
    @shaikhaziz9703 Pƙed 3 lety

    this is on fire guys...the arrays functions can makes us our projects much easier and fastest....i am also trying to learning this arrays functions codings,....can someone pls explain me why the sir has used i,1 and i,5 in 6:43 minutes
    i want to use count of columns so what would be just basic formula for counting columns in arrays.

  • @churchun
    @churchun Pƙed 3 lety +1

    A query, in an array (not in excel cells) is it possible to include background color? or put background color to certain elements of that array?

  • @RyuBeach
    @RyuBeach Pƙed 5 měsĂ­ci

    ì„€ëȘ…ê°ì‚Źí•©ë‹ˆë‹€

  • @jawalim1996
    @jawalim1996 Pƙed 3 lety

    amazing video, thanks, you are my best vba mastery legend.

  • @WieEiPiehGuy
    @WieEiPiehGuy Pƙed 4 lety +2

    Thanks, very informative. I have a question though: How do I manage to format my cells to achieve a good layout? My current Excel sheet is rather slow as I copy a row and insert it, but with this (slow) technique I don't have to care about number/text/date/currency formats or cell formats (yellow background, bold numbers) etc. as Excel takes care of that for me. Should I work with arrays and after my "tables" are filled with data, another macro formats the cells according to my wishes? Or is there another approach?

    • @WieEiPiehGuy
      @WieEiPiehGuy Pƙed 4 lety +1

      I "tweaked" my Excel spreadsheet and was able to speed up the operations by a factor of 17 (of course, I did not use 'select' and similar bad techniques from the start). I did this by using arrays and, instead of adding one row per operation, added all the needed rows at once and inserted the array contents into those empty with one go. The format of the cells (styles, number formats, borders...) is fixed at the end for the entire "table". Thanks :)

  • @TheJoeFletch
    @TheJoeFletch Pƙed 9 měsĂ­ci

    This is really really great and has reduce the processing speed of my code dramatically. The only issue that I have encountered is writing the data back to the same location (I am pulling the data from an Excel table) that is filtered. I see very strange results where the first row of the array is inserted into the last line that is displayed. Any suggestions on how to paste back to a range / table that is filtered? I could remove the filter and then re-apply it, but if that data that is changed in the array, then the same filter would not be applied.

  • @stevennye5075
    @stevennye5075 Pƙed 4 lety

    very useful

  • @imboom-
    @imboom- Pƙed 10 měsĂ­ci

    Thanks for the video, Paul! I had a question! I'm looking to copy all the info from a sheet and paste it to another sheet. Based off the indent value of a second column, I will offset it on a row in the new sheet(higher indent = higher offset, indentlevel 1 goes to A:D on row 1, indentlevel 2 goes E:H on row 2, ect) then continue through the entire sheet. I'm seeing if I can apply this to that but the problem is my first row has an embedded link.. is there a way to add that into the array and keep the link as well when it gets pasted?

  • @121960864
    @121960864 Pƙed 27 dny

    thank you very much!

  • @pentatradeagency3343
    @pentatradeagency3343 Pƙed 2 lety

    Sir, Thanks for uploading such nice videos on very basic yet foundation to onward VBA coding. Sir, unfortunately when I run codes, error message pop out. The error message contain a "Run-time error 13 Type Mismatch". Please advice me on correct the error. Lots of love to your continued efforts. Keep going, Sir.

  • @ghalebevaldo4416
    @ghalebevaldo4416 Pƙed 3 lety

    Great,thanx

  • @chandrachurdatta5844
    @chandrachurdatta5844 Pƙed 3 lety

    20 Minutes to 33 seconds!!! Thank you very much.

  • @Ts18762
    @Ts18762 Pƙed rokem

    Mindblowing 💯💯

  • @petermaina7111
    @petermaina7111 Pƙed 3 lety

    you are a G.O.A.T. probably also take us through append

  • @fritzbaden8085
    @fritzbaden8085 Pƙed 3 lety

    This guy is a frickin' genius.

  • @vairamanigopalan1268
    @vairamanigopalan1268 Pƙed 3 lety

    Paul this is awesome. Can this be used in Class Module. if not please show us how to do it. Thanks in advance.

  • @MaydayAggro
    @MaydayAggro Pƙed 3 lety

    What if you want to remove duplicates in your new range and do a sumif from the original amount column based on either ID or country in the new amount column? I need to do exactly this from a range of about 20000 rows. I'd like to use an array bc it's so much faster, but I am not sure how to convert the range to a shorter "remove duplicates" array with the sumif.

  • @satishk2410
    @satishk2410 Pƙed 4 lety +2

    Really awsem, never seen such effective and shorter code in my life, again thanks for creating this video, love you, really helped me a lot