 back to another video. In this video we're going to be looking at again still on the topic of VBA, we're going to be looking at variables. So why are the importance of using variables is and to demonstrate that I've created this short story that we'll go through and also talk about the benefits of using variables and obviously then show how to implement variables into this particular example. So as you'll see on the screen we've got the VBA window or developer window on the left here taking the majority of the screen as that's what we're predominantly going to be using and I've just got an Excel sheet what relates to that window on the right here just so you can see obviously the output. So what I've done so far is we've got a short story here what's going to appear over four lines and if I just run this you'll see exactly how it comes out. You'll recognize the definition of putting here to obviously refer to this sheet to paste out this text. If you aren't familiar with this you want to check out the last video what go through obviously how what basically all this means but otherwise you should better follow along with entering exactly the same text and you'll get the same result. So what I'm going to do is hit F5 and you can see what the output we now have into Excel. So our story is there once was a man named Tim he was 70 years old he liked the name Tim but he didn't like being 70. So this is a simple little story that is constructed together to help us work with variables. So the reason we want to use a variable is in the circumstance that with our story we suddenly decide we want to change the character's name. So the moment the character is called Tim and you can see he appears twice within this story. So if we were to want to change his name to maybe I don't know Paul at the moment we'd have to go into each line and change this to Paul we then have to go into this third line and change that to Paul as well and obviously if we hit F5 you can now see that the name has been updated to Paul and for this short story just updating in two rows isn't too much of a pain but obviously if you had hundreds of rows or even more like you're actually updating a full story or full full line a full big set of codes so I say kind of the words up then obviously that would be quite a painful process and also if we wanted to change the age as well so let's say we wanted to change it to 40 again we've got to look through everywhere where the age appears or there's also 40 at the end here and you can see that obviously it it just opens you up for causing more errors if you forget to update a particular reference and as you can see that's now all updated there so the solution for that is using variables and what a variable is it allows us to store a piece of information within a heading of say character top character name and then what we can do is everywhere we reference the name within the story instead we reference this variable what will dynamically update as that variable changes itself so all we need to do is you see I've entered a couple lines at the top here I'm just going to enter character underscore name and do space equals and then within quotations we're going to call him Tim we'll go back to where we started off and then next thing we'll do is enter another row and this time we'll call we're not cool but we'll set a variable for character age and within that we'll go for the age of 70 and let's just add one more space there so we can see so at the moment obviously we've got our variables here but at the moment we need to now include these into our story so to do that we just need to make a couple of changes so the first one is where we got this name pool here we just need to remove the name pool so at the moment you can see it reads there once was a man named space comma well because we want to retain that space after the word name or named we're going to enter another quotation here and you can then what we're going to do is we're going to do space and and symbol we'll do another space and in this time we're going to type character name but then we'll do another space a second and symbol and then lastly a quotation so you can see how this is now constructed together so we've got a string was his first part here there once was a man named with a quotation mark we then got the part what refers to our variable so the character name and then we've got another string that we're adding on to the end of this just to conclude our little comma we have there and what you can see is obviously we're using the and symbols obviously to connect all these two together so if we wanted to just do if we have character name and sorry if I'm going off on this tangent what we could do in here is put there in quotations and so and you can see how that works with text just using these and symbols allows us to basically concatenate multiple streams together obviously for our example here we don't want to include this bit string here we literally just want to include our variable what is character name and then we need to do that again to our second reference what we've got down here so he liked the name Paul so we'll do a quotation and symbol character underscore name and symbol and then we can remove Paul and also include our end there so we can see we've got Tim as our variable at the moment we've got a mixture of names here but if I just now hit f5 you can see how they're both now updated to Tim and then going forwards if I want to now change this name again so let's call him John and hit let's just keep couple of j's keep it all proper hit f5 now you can see that how John is now replaced as the name and this is exactly the same for when we come to update the age so all we now need to do is he was quotation and character underscore age get rid of the 40 and the trick with this but you'll obviously see as it comes out is just remembering to include spaces within the strings either side where you want it so obviously if I was to remove this one from here and now hit in f5 sorry you can see that it's grouped the 70 and the years together so it's just remembering to keep those faces in when you need but that should be quite easy one to sort of identify as you put it in and the last one we've got this 40 at the end here so all you need to do is get rid of the 40 and character underscore age and just another little and at the end there just so we can keep that full stop so let's now change this completely different name let's go for James and let's make him 45 and you can see that the code has completely updated so we have now got our variables working for our character a net character name sorry and the character age and the benefit of using this as well as if you suddenly decide that you know you want to start your story off with these two variables but later on the character age for whatever reason not character age the character name changes for whatever reason all you need to do to override this variable is I'm just going to copy place in here so if we suddenly decide later on that actually we want the character name to now be Jim all we need to do is bring that variable back in again and the second time it's used it's just going to now override what was previously stored so it's not going to replace it with Jim for your enter you can see you've now got Jim in there it could be only very few examples where you might want to do this well actually it'll probably be a few more examples as we go further on with more advanced functionality that we're trying to do with our VB code but that just goes to show you that you can override that as well so at the moment you'll see that both our name and our age are just stored as two to variables but this is this isn't really the correct way that we should be coding our variables we need to actually tell the VB code or tell Excel what type of content we're storing in those variables so at the moment it's just going to store both of these as available what is fine and VB will soon work out what the content is and then include that as it has required so because each of our variables of James and 45 are surrounded by these quotations what's going to happen is it's getting stored as a string so the VB code looks in this it says right it looks like those two are pieces of string therefore I'm going to store those as strings although it's actually stuck it's actually storing them as variables the reason we don't want to store as this variable as we've got here is because it's one best practice and also two you're actually going to be storing and using up more memory than you need so obviously what happens when you define a variable is our program is storing these in its memory these variables so that you can obviously call upon those at any point when you need your code and obviously if you're storing all your variables like this so you're not defining exactly what they are you are going to chance or chances are you're going to be using up a lot more memory than you need to and this could impact you in terms of speed among other things as your code obviously grows and gets bigger so how do we store these as the required variables well simply all we do is we go back to top here and now what I can do is enter the word dim and this time I'm going to do character underscore name and I'm going to type the word as string so a string is basically refers to storing text so string is the variable you'd want to use anywhere where you're storing text the other one we have or the other main one should I say is going to be an integer for numbers so dim character underscore age as integer and then what I also then need to do is I can just amend this to show the 45 is actually not a string and so we'll talk through this so character name as string so a string is basically either a word or a sentence so basically a string of text so anywhere where you want to store text you're going to record it as a string and anywhere when you've got a number so for us we've got a whole number of 45 we want to store that as an integer so for those you're not where an integer is basically a whole number so if we had a decimal place here it wouldn't work because an integer doesn't hold those decimal places it's whole numbers only and I'll be going into more detail about these different data types I just wanted to touch on it briefly in this video so you're aware of the best practice of what we'll be using going forwards so in summary obviously variables are really helpful when we're trying to store information that we want to repetitively use and we're going to be using variables a lot well and you would find you're using variables a lot in your code as obviously progress along when we do use a variable we need to ensure that we're defining what the variable type is so for us you know we have the word James or the name of James which is going to be a string therefore we need to divide define it as a string and for character age we've got the number 45 because age is always referred to in a number terms and first we're going to store that as an integer and this is not only best practice when coding but also it's going to ensure that the data types we're storing is only using the memory that it needs and not using excessive memory to cause us any problems later on and you don't have to do your dims altogether at the start here when you're defining what your variable is you could have your definition of character name here and then you could say what that character name is beneath that but my personal preference and as I'll probably you'll notice as I go along is often like to define the variables at the start and then I'll see the variables or see in term or I'll state what the variable type is at the first and then we'll actually set what that variable value is thereafter so there you go there is that sort of video going through working with variables I think I mentioned before but I'll be doing another video what goes into a bit more or goes more in depth into each variable so it goes into strings and using strings and also how to use numbers with your variable types as well so don't worry we will be going into more detail with that so my suggestion would be have a play around with that try and copy this story into your code and like I said try playing around with changing the character name and the age or also creating your own story and using any other variables that you see fit just so you again become familiar with entering data that is going to be obviously placed into excel and also plan around these variables and getting comfortable with those so if you did enjoy that video please do hit that like button it's not only appreciate by myself but it helps out all important youtube algorithm if you haven't already subscribed please subscribe to the channel and also make sure you hit that bell notification button so they are notified of all of our future videos so thank you very much for watching and I'll see you in the next video before you go don't forget to check out the other videos on our channel you'll see everything from other functions and formulas through to tips and tricks we've also created some playlists so you can see these categories together so make sure you check those out and get all those useful information and obviously as always don't forget to subscribe and hit that bell notification button