Excel VBA WHAT IS VBA AND WHY WE USE IT Microsoft Excel is an extremely powerful tool that you can use to manipulate, analyze, and present data. Sometimes though, despite the rich set of features in the standard Excel user interface (UI), you might want to find an easier way to perform a mundane, repetitive task, or to perform some task that the UI does not seem to address. VBA is the acronym for Visual Basic for Applications. It is an integration of the Microsoft's event-driven programming language Visual Basic with Microsoft Office applications such as Microsoft Excel, Microsoft Word, Microsoft PowerPoint and many more. By running Visual Basic IDE (Integrated Development Environment) within the Microsoft Office applications, we can build customized solutions and programs to enhance the capabilities of Microsoft Office Applications. Office applications like Excel have Visual Basic for applications (VBA), a programming language that gives you the ability to extend those applications. VBA works by running macros, step-by-step procedures written in Visual Basic. Learning to program might seem intimidating, but with some patience and some examples such as the ones in this course, many students find that learning even a small amount of VBA code makes their work easier and gives them the ability to do things in Office that they did not think were possible. Once you have learned some VBA, it becomes much easier to learn a whole lot more so the possibilities here are limitless. By far the most common reason to use VBA in Excel is to automate repetitive tasks. For example, suppose that you have a few dozen workbooks, each of which has a few dozen worksheets, and each of those needs to have some changes made to it. The changes could be as simple as applying new formatting to some fixed range of cells or as complex as looking at some statistical characteristics of the data on each sheet, choosing the best type of chart to display data with those characteristics, and then creating and formatting the chart accordingly. Either way, you would probably rather not have to
perform those tasks manually, at least not more than a few times. Instead, you could automate the tasks by using VBA to write explicit instructions for Excel to follow. VBA is not just for repetitive tasks though. You can also use VBA to build new capabilities into Excel (for example, you could develop new algorithms to analyze your data, then use the charting capabilities in Excel to display the results), and to perform tasks that integrate Excel with other Office applications such as Microsoft Access 2010. In fact, of all the Office applications, Excel is the one most used as something that resembles a general development platform. In addition to all the obvious tasks that involve lists and accounting, developers use Excel in a range of tasks from data visualization to software prototyping. HOW TO USE VBA But before we get started, let's begin by making sure that the tools we need are visible. If you can see the Developer menu in excel then you can skip the following, otherwise, please read the following. Click on File > Options > Customize the Ribbon and then check "Developer". A new tab (Developer) will be added:
HOW TO RUN VBA EDITOR? To work with VBA code, we'll need an editor, which is installed by default. You can open it by pressing the shortcut key combination "Alt F11" or choose a sheet and right click on its name and choose view code or you can go the developer menu and choose visual basic icon (the icon to the left) How to display and hide main windows: Click on the menu view, then click on the window name to show it. To close the window, simply click on the x button on the top right corner of it.
Project Window This window will show you all the files that you have opened. The Project Window uses a tree view where you can drill down into each file that you have open and see the areas in which you can insert VBA code. Notice that in my screenshot above that there are two files that are open in my Excel application: Book1 (a workbook) and VBHTMLMaker (an add-in). In Book1 you can see 3 subfolders: Microsoft Objects -This folder houses a code area for your workbook (ThisWorkbook), and your workbooks spreasheets (Sheet1) Forms - This folder stores any userforms that you create. I touched a little bit on this in Lesson One so I won't repeat myself here. If you do not see this folder you can add it by right clicking anywhere within the projects folder tree and going to Insert -> Form. Modules - A Module folder stores you macro and function code. If you do not see this folder then that means that the project most likely does not have any macro code in it (note: there can still be code stored in the forms folder or in the Objects folder). You can add this folder by right clicking anywhere within the projects folder tree and going to Insert -> Module. Properties Window The Properties Window will allow you to modify certain aspects of whatever object, form, or module you have highlighted. Typically, the only thing I change using this window is the Name field. This is a good idea because you can give your modules or forms a more meaningful name than the default names that the Visual Basic Editor provides. Custom names in the Name field can only be one word in length. Code Writing Area This is where the magic happens! In this area you can actually write and edit your VBA code. Each macro must begin with a Sub statement (which is opened with Sub [insert your macro name] ( ) and closed with End Sub). Notice also that the VBA Editor color-codes some key words in a few different colors. This helps make your code more organized. I have two major tips that I like to share with people when they are first learning to write VBA code:
Use Indentations - Always try to use indentations (via the Tab key) within your code. There are various methodologies to tabulating code but as long as you are consistent and it makes sense it will help you enormously when you are trying to add to or debug your code. It also helps when someone else is trying to help you with your code. Write in Lowercase - If you haven't noticed already, every word in the VBA language has at least one capitalize letter. How is this an advantage? Well, the Visual Basic Editor is not case sensitive and it likes to correct you when it can. This means that is you type in "workbook", the editor will automatically change it to "Workbook". My rule of thumb is to type everything in lowercase and if the VB editor doesn't capitalize at least one letter, I know that I either misspelled that word or that word is not defined. Having the Visual Basic Editor correct every word I type has really made my code less buggy and prevent a lot of frustration over the years. Immediate Window I like to refer to this area as my piece of scratch paper. The Immediate window lets you do all sorts of tests while writing and running your code. You can use the code Debug.Print to tell VBA to send the information that follows to the Immediate window. This could be the output value of a function, the value of a cell, or what a current application property is set to. When I first began writing VBA code I had no idea the Immediate window ever existed (it's usually hidden by default but you can use the shortcut Ctrl + g to view it), but once learned everything it could do I never stopped incorporating its functionality into my code writing and testing processes. Watch Window The Watch Window is kind of like an X-ray machine. It will show you all the data that is stored inside a variable! Some variables (like the ones you create in your code) will not have very much data stored in them. However, if you were to "watch" a variable that was assigned to a cell, you would see a whole bunch of data (font color, value, height, fill color, etc...). This is mostly usefully when you are trying to debug your code and want to understand what value your variable has at any given point in your code.
In order to watch a variable, you need to highlight your variables text and click the Add Watch button (this is the eyeglasses icon located on the debugging toolbar). You should then see your variable appear in the Watch Window. Once you start running through your code and load a value to your variable, you should see an option (plus sign) to drill down or expand out the contents that is now stored in the variable. SOURCES: http://vbatricksntips.blogspot.in/p/blog-page.html https://www.thespreadsheetguru.com/vba-intro/the-visual-basic-editor