How to Declare (Dim) and Set VBA Variables (use data types correctly)

  Рет қаралды 175,553

Leila Gharani

Leila Gharani

Күн бұрын

Join 400,000+ professionals in our courses here 👉 link.xelplus.com/yt-d-all-cou...
It’s best practice to declare your Excel VBA variables before you use them. Declaring variables means that you give VBA a heads up that it needs to free up some memory space for your variables which will in most cases be less than the default. That's why it's important to understand the concept of data types and how much memory space each of these types will take. The less memory you allocate to a variable, the faster your code will run. I show the range associated to each data type - such as numbers, text, date, currency etc in the video.
🎓 Get access to the full course here: www.xelplus.com/course/excel-...
Key Highlights:
- Default Data Type: By default, VBA assigns the data type called "Variant," which adapts based on the data it holds, leading to larger memory consumption.
- Memory Efficiency: Different data types consume varying amounts of memory. For instance, a "Byte" takes up only one byte of memory, while a "Variant" uses around 16 bytes.
- Data Type Ranges: Each data type has a specific range it can handle, which becomes crucial in scenarios where the data exceeds that range.
- Common Data Types: Explore popular data types like "Byte," "Integer," "Long," "Boolean," "Double," "String," and "Object," each with its own purpose and memory footprint.
- Variable Declaration: Learn how to declare variables using the "DIM" keyword, and understand naming conventions for variables.
★ Links to related videos: ★
Learn about Visual Basic Editor: • Excel VBA tutorial for...
Properties & methods in VBA: • Learn How to Use Prope...
Blog Post: www.xelplus.com/excel-vba-dat...
★ My Online Excel Courses ★ ► www.xelplus.com/courses/
➡️ Join this channel to get access to perks: / @leilagharani
👕☕ Get the Official XelPlus MERCH: xelplus.creator-spring.com/
🎓 Not sure which of my Excel courses fits best for you? Take the quiz: www.xelplus.com/course-quiz/
🎥 RESOURCES I recommend: www.xelplus.com/resources/
🚩Let’s connect on social:
Instagram: / lgharani
LinkedIn: / xelplus
Note: This description contains affiliate links, which means at no additional cost to you, we will receive a small commission if you make a purchase using the links. This helps support the channel and allows us to continue to make videos like this. Thank you for your support!
#ExcelVBA

Пікірлер: 110
@LeilaGharani
@LeilaGharani 4 ай бұрын
Get access to the full Excel VBA course here: www.xelplus.com/course/excel-vba-excel-macros/
@excelisfun
@excelisfun 6 жыл бұрын
Thanks for the great Data Type video!!
@LeilaGharani
@LeilaGharani 6 жыл бұрын
You're very welcome Mike :) Thank you for your support.
@dhruvilbarbhaya884
@dhruvilbarbhaya884 4 жыл бұрын
During such lock down, watching such easy explanation of tough topic is awesome.. U r up to the point and so clear, and same time so easy......
@dschulz5647
@dschulz5647 5 жыл бұрын
This helped explain a bunch for me. Usually videos don't help much when it comes to coding but this was actually very helpful. Thank you!
@LeilaGharani
@LeilaGharani 5 жыл бұрын
I'm glad it was helpful.
@tannertucker22
@tannertucker22 4 жыл бұрын
Thank you. Your vba and excel courses are excellent.
@tanwen1077
@tanwen1077 3 жыл бұрын
Love your videos, they are clear and easy to understand. :D
@1gopalakrishnarao
@1gopalakrishnarao 6 жыл бұрын
Very informative, waiting some more Videos on the VBA, eagerly. Your Explanation is Excellent, and crystal clear.
@LeilaGharani
@LeilaGharani 6 жыл бұрын
Thank you very much Gopala! Very happy to hear you like the VBA series. I will be alternating between Excel and VBA in the next weeks :)
@marcellokassa6975
@marcellokassa6975 6 жыл бұрын
Thank you Leila. You are always amazing!
@LeilaGharani
@LeilaGharani 6 жыл бұрын
You're very welcome Marcel. Thank you for dropping by :)
@jennydrumm7223
@jennydrumm7223 6 жыл бұрын
Another great video! Dim, Set and such have been the bane of my existence, so It's good to have it explained clearly. I'll be watching this a couple more times, just to be sure I really get a grasp on it! (People have tried to explain these to me before, but they get into too much "tech jargon" that makes my brain spin around, LOL!) Thank you!
@LeilaGharani
@LeilaGharani 6 жыл бұрын
I'm glad this video helps :) Thank you for your support Jenny.
@divyarajsinhchauhan
@divyarajsinhchauhan 2 жыл бұрын
Can you please tell me DIM Full Form
@md.mahbubrahman7891
@md.mahbubrahman7891 5 жыл бұрын
Very Informative & useful. Thank you for uploading & expecting more.
@LeilaGharani
@LeilaGharani 5 жыл бұрын
I'll definitely do that :)
@felipao1976
@felipao1976 2 жыл бұрын
Useful and clear as always, I love
@dddoda
@dddoda 3 жыл бұрын
oh! Just the info I was looking for. Now my macro works! Thanks.
@LeilaGharani
@LeilaGharani 3 жыл бұрын
Great!
@sridharlagishetty2078
@sridharlagishetty2078 6 жыл бұрын
Thanks you for the information
@joshzink97
@joshzink97 6 жыл бұрын
Thank you for the error example. That has been frustrating to work through and your explanation is very helpful.
@LeilaGharani
@LeilaGharani 6 жыл бұрын
You're very welcome. I'm glad to hear the explanation was helpful.
@vivekphadke16
@vivekphadke16 6 жыл бұрын
I can easily remember what you teach. Thanks a lot.
@LeilaGharani
@LeilaGharani 6 жыл бұрын
Thank you! I'm glad to hear that Vivek :)
@learnandgrowwithfun289
@learnandgrowwithfun289 6 жыл бұрын
superb. i like the way you touch every small point while explaining.
@LeilaGharani
@LeilaGharani 6 жыл бұрын
Thank you! Glad you like the explanation.
@pedrocamara6451
@pedrocamara6451 4 жыл бұрын
Your tutorials are very good. thank you.
@LeilaGharani
@LeilaGharani 4 жыл бұрын
Glad to hear that Pedro.
@naheda.ikhtier6596
@naheda.ikhtier6596 3 жыл бұрын
Your every video is very helpful 😊😊
@Melki
@Melki 3 жыл бұрын
Thank you, very helpful
@firdausbasheer3066
@firdausbasheer3066 4 жыл бұрын
Amazing tutorials
@amc8437
@amc8437 4 жыл бұрын
Intriguing rabbit hole I have stumbled upon.
@255205titel
@255205titel 5 жыл бұрын
Thanks Leila, helps me a lot!
@LeilaGharani
@LeilaGharani 5 жыл бұрын
I'm glad it is helpful!
@stevennye5075
@stevennye5075 4 жыл бұрын
good review!
@HamidurRahman
@HamidurRahman 6 жыл бұрын
Thanks for such a basic concept sharing,
@LeilaGharani
@LeilaGharani 6 жыл бұрын
You're very welcome Hamidur.
@jokkiossaka3306
@jokkiossaka3306 4 жыл бұрын
Thank you very much Leila!
@LeilaGharani
@LeilaGharani 4 жыл бұрын
You're very welcome. Hope it will come in handy.
@eduardooc8531
@eduardooc8531 3 жыл бұрын
I appreciate it thank you very much
@Thanogr1
@Thanogr1 5 жыл бұрын
LET used in old BASIC programming language. I remembered it first time i saw Spectrum's Sinclair Basic
@johnborg6005
@johnborg6005 6 жыл бұрын
Thanks Leila :) :)
@ismailismaili0071
@ismailismaili0071 6 жыл бұрын
thank you so much Ms. Leila I wish you could do more videos because i really want to learn VBA
@LeilaGharani
@LeilaGharani 6 жыл бұрын
I'll do my best :) it's not easy to get in more than 1 video per week. I'll alternate between usual Excel and VBA....
@udaykant3504
@udaykant3504 6 жыл бұрын
Explained in systematic way
@LeilaGharani
@LeilaGharani 6 жыл бұрын
Thank you. Glad you like it Uday.
@stefandlucas
@stefandlucas 2 жыл бұрын
Awesome.
@HamidurRahman
@HamidurRahman 6 жыл бұрын
Helpful :)
@HoppiHopp
@HoppiHopp 6 жыл бұрын
Die Übersicht ist extrem hilfreich! ☺️
@LeilaGharani
@LeilaGharani 6 жыл бұрын
Freut mich. Vielen Dank.
@HoppiHopp
@HoppiHopp 6 жыл бұрын
I heard you speaking German in one of the lectures in your "Unlock Excel VBA and Excel Macros" course (3/16) when you explained the syntax of the object model etc. and I was tempted :)
@LeilaGharani
@LeilaGharani 6 жыл бұрын
:) I live in Austria.
@meniporat3527
@meniporat3527 2 жыл бұрын
Hi Leila, a minor correction: The largest positive number that the INTEGER data type can hold is not: 32768 but: 32767. Thank you for your excellent videos 🙂
@HamidurRahman
@HamidurRahman 6 жыл бұрын
Helpful
@yugalsharma3842
@yugalsharma3842 6 жыл бұрын
Well explained.....
@LeilaGharani
@LeilaGharani 6 жыл бұрын
Thank you. Glad you like it.
@zeFoksXIII08
@zeFoksXIII08 4 жыл бұрын
Thank you
@shivankmittal8864
@shivankmittal8864 6 жыл бұрын
Really Amazing
@LeilaGharani
@LeilaGharani 6 жыл бұрын
I'm glad you like it. Thank you for your support.
@abdallah.kandiel
@abdallah.kandiel 6 жыл бұрын
This is an amazing video Welcome And happy thursday ur highness
@LeilaGharani
@LeilaGharani 6 жыл бұрын
Happy Thursday :) You're very welcome. Hope your workload is better this week....
@abdallah.kandiel
@abdallah.kandiel 6 жыл бұрын
Leila Gharani it is Thanks 4 asking This is so kind if you But lucky me . If you know about fasting and ramadan month We are in holly month any way i like to share with you Our lovely istructor
@miteshkumar5477
@miteshkumar5477 5 жыл бұрын
So could you please let us know the usage of "new" keyword, as far as i understood "new" keyword is used to instantiate object variables. Thanks in advance !
@indianpigeon3680
@indianpigeon3680 5 жыл бұрын
Love u lots
@MrLbott
@MrLbott 5 жыл бұрын
You are amazing...
@LeilaGharani
@LeilaGharani 5 жыл бұрын
Glad you like it :)
@MIMGURU
@MIMGURU 2 жыл бұрын
Good👍
@Progressmarini
@Progressmarini Жыл бұрын
Can I use the set statement at the module level? I have declared many variables as sheets and don't want to have to set them in every sub() I have in the same module. thank you
@MrAmit777
@MrAmit777 3 жыл бұрын
Thankyou
@hosseinhosseinpoor4845
@hosseinhosseinpoor4845 3 жыл бұрын
thanks thanks thanks
@nabilsn411
@nabilsn411 5 жыл бұрын
Cool thx
@aev6075
@aev6075 2 жыл бұрын
10/10
@roberto-martins
@roberto-martins 4 жыл бұрын
Thanks.
@LeilaGharani
@LeilaGharani 4 жыл бұрын
My pleasure Roberto.
@amruthabathini3455
@amruthabathini3455 4 жыл бұрын
Hi LEILA, Your video is very helpful and it is very clear to understand. Can you please help me with the following scenario: 1) I have a workbook of sheet1 with two columns and a few rows. 2) Applied filter for sheet1 data 3) I have duplicated the sheet1 in the same workbook and renamed it as "Test" 4) Deleted the duplicates in sheet2 5) Now, I need your help in copying a cell value(the data is dynamic) of sheet2 and paste it in the filtered column of sheet1 so that I can write the comments beside the two columns of sheet1. **NOTE: DATA IS DYNAMIC The columns are as below: SHEET1: COLUMN-1 COLUMN-2 abc apple abc windows 123 android abc os b phone 123 android abc blackberry abc apple b nokia abc lenovo SHEET2: COLUMN-1 COLUMN-2 abc apple 123 android b phone I need the final result like this in SHEET1 COLUMN-1 COLUMN-2 COMMENT abc apple abc windows same as apple 123 android abc os same as apple b phone 123 android same as android abc blackberry same as windows abc apple same as apple b nokia same as phone abc lenovo same as apple IT WOULD BE REALLY HELPFUL IF YOU CAN GUIDE ME ASAP.
@mdcs1992
@mdcs1992 4 жыл бұрын
I have multiple disabilities and health problems. In my sixties now and lost my job because of them......but a love of Excel makes me want to learn vba. I recorded a macro that "Cleared Content" from multiple non contiguous Ranges. I went into gab to run it but kept getting an error at lines of code starting with "Union Range." Can't understand it. Is it simple to rectify? Your channel is IMHO the best on You Tube for Excel. Well explained. Great examples and explanations. Superb. Thank you very much
@LeilaGharani
@LeilaGharani 4 жыл бұрын
Make sure you didn't record in relative mode. If you go to the developer tab make sure the "use relative reference" toggle is off. If these ranges are fixed ranges, it would be best to get direct references - otherwise you could run into errors.
@Trendinvideowalasam
@Trendinvideowalasam 4 жыл бұрын
This video is good.in some videos of others I saw no dim is used in the beginning. Can u define all the codes why it is used for and when with elaboration.
@prayushijain4087
@prayushijain4087 3 жыл бұрын
Check this too kzbin.info
@tigertiger51
@tigertiger51 3 жыл бұрын
What will happen if I used integer data type for a data with a value of 32,769 or more?
@subashanandofficial
@subashanandofficial 6 жыл бұрын
thanks
@pinkeishz9106
@pinkeishz9106 6 жыл бұрын
Leila, Thanks for sharing. I've question in your example of LastRow=Rows.count what if it is not declared no Dim direct LastRow... What problem will it cause?
@LeilaGharani
@LeilaGharani 6 жыл бұрын
If you don't declare it, your macro will still run without problems (as long as you don't have option explicit turned on) but declaring it makes it more efficient specially in terms of macro performance time....
@althma5203
@althma5203 4 жыл бұрын
If the variable isn’t declared it will default as Variant, and will be less efficient (i.e. will use more memory)
@jamesjujare2599
@jamesjujare2599 6 жыл бұрын
Thank you Leila....!! Can we have more videos on VBA
@LeilaGharani
@LeilaGharani 6 жыл бұрын
You're very welcome. Yes - I will be alternating between VBA, charts, formulas etc....
@jamesjujare2599
@jamesjujare2599 6 жыл бұрын
Thank u.. Leila
@teymurrashidli7560
@teymurrashidli7560 5 жыл бұрын
İn VBA round(10.25,1)=10.2 but in normal excel round(10.25,1)=10.3 I need calculate exactly in excel VBA. How can I round in VBA exactly? I need round(10.25,1)=10.3 in VBA
@Pro-gamer_20108
@Pro-gamer_20108 Жыл бұрын
Hi looking code that help me to add cell data while we use send email cose
@Manasa1140
@Manasa1140 2 жыл бұрын
Hi madam can you please do videos on averageif using vba in which user changes data every time it automatically updates.
@mohammadalmomani1193
@mohammadalmomani1193 5 жыл бұрын
Hello I am trying to find an vba code to copy and paste every range of rows like(A1:C19).copy and paste in word file as image then copy from (A20:C39) and paste in the same word file and so on for the rest of the table in excel. they have told me that I need to use the looping in excel but I don't know exactly how to do it …. please help if you can
@LeilaGharani
@LeilaGharani 5 жыл бұрын
I have a whole section (no 10) on Looping in my VBA course: courses.xelplus.com/p/excel-vba-excel-macros
@poultryland2609
@poultryland2609 3 жыл бұрын
The only problem with it is that text you are typing fades away while you are typing, please fix it, your lectures are superb
@kumarsandy2008
@kumarsandy2008 6 жыл бұрын
hi , i am a beginner to vba macros, and want to get skilled in this field, i want to learn all the looping process and array......and charts with vba....could you please help....
@LeilaGharani
@LeilaGharani 6 жыл бұрын
I have a section on arrays and charts inside the complete course. Feel free to check out the content (link is in the description of the video) to see if it covers the topics you're looking for.
@sohailmoosa
@sohailmoosa 4 жыл бұрын
Hi Leila, I'm currently your student using Udemy Business. And I feel I'm lucky to get enrolled with an excellent teacher. I couldn't figure out how to send you a query in Udemy and hence ended up searching for you here and would ask one. I have currently completed lecture 35 that speaks abt declaring variables. I'm unable to understand why do we beed variables? Is it necessary to have variables? What are variables? And can't I do without them in vba. I am a novice and have seen many codes and most of them have variables. But I always had these questions in my mind. I have googled alot, but nothing has solved this question yet. Please reply.
@LeilaGharani
@LeilaGharani 4 жыл бұрын
If you do not declare variables it usually consumes much more memory and making your code slower. Also, it makes it easier to refer to it later.
@sohailmoosa
@sohailmoosa 4 жыл бұрын
@@LeilaGharani I sincerely appreciate you taking out time and replying to me. Kudos to you. I'll keep this point in mind and proceed with the course. May be it doesn't make sense to me right now, but later it may. You've been an excellent presenter and tutor. And I trust you for that.
@jashanbrar8244
@jashanbrar8244 5 жыл бұрын
Very good ese hi ielts ke bare mai b batayo
@LeilaGharani
@LeilaGharani 5 жыл бұрын
Glad you like it!
@golubhai1910
@golubhai1910 2 жыл бұрын
set is basically a setter and getter concept from oo programming I guess
@world_of_tech1213
@world_of_tech1213 6 жыл бұрын
I am going through all your session even i do macro coding intermediate level.. having one suggestion if you give session number for each lesson ..make it more easily for us to gone through
@LeilaGharani
@LeilaGharani 6 жыл бұрын
Thank you for the suggestion. I've included them in a playlist so they can be viewed in order....
@world_of_tech1213
@world_of_tech1213 6 жыл бұрын
Leila Gharani Thank you Leila...can you put a lesson for ms access and excel marco . How to transfer data from excel to access and how to use query for fetching data from access. I hope that would be useful for many people..
@60_daysff_per_year
@60_daysff_per_year 2 жыл бұрын
You have to be careful when you use a long long variable.
@latiffesa
@latiffesa 3 жыл бұрын
You are beautiful
@anuragkumar2531
@anuragkumar2531 3 ай бұрын
Good
Excel VBA IF THEN Statement (with ELSEIF & Looping in cells)
12:10
Leila Gharani
Рет қаралды 311 М.
How to Create & Use Excel Macros (Real world example)
10:09
Leila Gharani
Рет қаралды 1,6 МЛН
Универ. 13 лет спустя - ВСЕ СЕРИИ ПОДРЯД
9:07:11
Комедии 2023
Рет қаралды 6 МЛН
Climbing to 18M Subscribers 🎉
00:32
Matt Larose
Рет қаралды 36 МЛН
Дибала против вратаря Легенды
00:33
Mr. Oleynik
Рет қаралды 3,3 МЛН
Sprinting with More and More Money
00:29
MrBeast
Рет қаралды 192 МЛН
Introducing Python in Excel
19:01
Leila Gharani
Рет қаралды 1,5 МЛН
How to use Custom Types To Write Ultra Fast VBA code
12:51
Excel Macro Mastery
Рет қаралды 28 М.
Learn Excel VBA to Automate Anything
14:02
Kenji Explains
Рет қаралды 358 М.
5 Things I wish I knew When I started using Excel VBA
12:45
Excel Macro Mastery
Рет қаралды 334 М.
Excel Macros & VBA - Tutorial for Beginners
50:20
Kevin Stratvert
Рет қаралды 1,1 МЛН
Learn Excel MACROS in 10 Minutes Using Real World Examples
9:13
Kenji Explains
Рет қаралды 1 МЛН
Excel's NEW Checkboxes Are Incredibly Cool! Here's why
14:24
Leila Gharani
Рет қаралды 491 М.
Free Excel VBA Course #12 - Using Object Variables in Excel VBA
13:17
Универ. 13 лет спустя - ВСЕ СЕРИИ ПОДРЯД
9:07:11
Комедии 2023
Рет қаралды 6 МЛН