Free Excel VBA Macro Tutorial - VBA Macro - For Each Loop - Simple Example

Опубликовано: 30 Апрель 2026
на канале: KeeshtuTechnology
20
0

Free Excel VBA Macro Tutorial - VBA Macro - Beginners/Intermediate - Excel VBA macro with for each loop simple example - youtube #youtubechannel #youtubevideo #vba #vbamacro #vbamacros
hi uh welcome to technology platform so i'm just going to take a macro so like in excel macro uh we know so many features introduced by microsoft but how to use that like for example uh if you see this in my screen it will i will just retrieve all the files and the size of the each file i took like this so most of the uh people trying to use that for example uh i will just select any of the folder and in that folder so many subfolders are present and so many files also present so how to filter or extract all the files and folders with size so how to do that so that i am going to explain in this video so before starting this we just want to understand uh the basic thing of macro and how to use that so that it is easy to understand uh for this concept but anyway it is easy to understand but however i'll explain you the basic functionality of macros and then i will

instruct this how to do this extract the each files and based on the size how to get that that i will explain later but before understanding this i will explain the basic thing of macron so now if you see here in the excel you could see this macro here so now in this macro once you click on view macro here you would see this macro name i'll simply type sample

and click on create now this is the scripting window we need to write it and execute based on ours so now i want to explain the simple thing like message box it will pop up the message which is required for for example this is test so just message both and this test it will give you so how it works like that so now uh we written already so we know how to uh extract this but before understanding this uh in this code i use so many for class and uh some functions like that so i will explain this small set of pieces and then i'm going to explain this now you will see this first in the macro you want to understand that um so this is the sheet now you could see this i want to display like this one two three automatic manner not a manual manner via code we want to type this so how to do this first

now um in the window i'm going to declare a variable first by using that b so beam is a keeper and i'm declaring a variable

now i'm going to use for each state for each text is for each syntax is like variable name in list of values

that means um

now i declare a var num now what i am going to do here is like where i'm equal to some list of values here i used array one comma two comma three to three values which is uh represent in array mode and uh that is stored in there now you see this this random values i need to retrieve and just loop it up that's it my task now so in the list of values i'm going to mention where now those one two three values will be extract one by one and store it here like i will check like get

numbers

it has one two three and those values i used here i am incrementing one by one so for that i used another variable like i equal to i plus one so for that i am declaring bin i

now this is we present here now we can see this message box vietnam values want to run this so this is run button this is break and tracer so for that we have debug mode here step into the step into means it will go line by line now if you see the keyword is f8 now i am going to press f8 again if it it comes here where num is not empty now it's an array now so varnum value stored here 1 see this message box 1

two three

now this is clear uh you want to see this one i will press f5 so earlier i used f8 it is one by one line now i am going to execute the whole code this is one okay two okay three so three so instead of that i'm going to use abc

so if it is a character or a string it should be coated with this double quote so any non numerical value should be quoted as double quotes now if you see this a b c like this so you can use this is test one this is this two this is test three now if you see this test one place two three right now let me go back here one two three

so instead of message box i want to store the value here

now uh in excel if you see here the default keywords are there this is cell the row column and range a b is a range this is a cell like that it's already there now what am going to do here is like i am going to store a value here by using keyboard range

open bracket close bracket

now a one now see this a one dot value

equal to let's get known values now if you see this yeah because why it is happening is like there is i used a variable instead of a default one so now we use it like this

you will see it will only one number should be there because i haven't looped it it's not a dynamic one i put it a constant hard coded value so its mention is like 3 here in a1 now if you see a2

it is present in a2 now i want to automate this so 1 should be here 2 should be here 3 should be here