Presentation is loading. Please wait.

Presentation is loading. Please wait.

Author/Host: James Malyk [Module 1][Module 1]. 1 The Intent of this Module The intent behind the creation of this development module came to me over the.

Similar presentations


Presentation on theme: "Author/Host: James Malyk [Module 1][Module 1]. 1 The Intent of this Module The intent behind the creation of this development module came to me over the."— Presentation transcript:

1 Author/Host: James Malyk [Module 1][Module 1]

2 1 The Intent of this Module The intent behind the creation of this development module came to me over the past couple of years; many of you have seen what I have created and have asked me to teach you these processes. Although I have many years of programming experience, I also have experience in what I’ve come to learn as best practices for what we can and should do with this tool in a work environment. I am going to teach this process based off real examples of what you may be using in your day-to-day work. I’ve designed this course to flow in a way which I feel is consistent with the basis of standard programming learning techniques. Although some of what I have written here may feel irrelevant, it’s designed so that you will be able to research on your own and see the ways that others teach; and be able to hopefully follow what they write. There is a huge base of VBA tutorials and knowledge available all over the internet, but I know that personally some of you learn far better when there’s someone to help.. And when what you’re learning can actually be applied to events in your work environment which are meaningful to you. Being told by a book or a website to copy and write code with the expectation of learning it is something which is quite useless in my opinion. I learn by experience, by being faced with an uncertainty and making it into a reality. I would recommend using VBA personally to enhance your own work (as opposed to enhancing others work – at least while you’re learning). Personally it will allow you the freedom to write code, to make mistakes (to learn from them) and to build up both your knowledge and techniques without the critiquing of others. When you build code for others you’ll find that they will put in place unforeseen data elements and generally will come at you harshly (and blame you) when they break your code. I have developed a message board which I will use for collaborative ideas, best practices, questions and comments. This will be a good basis for a community of questions which many of you may have; and it will allow others to possibly assist you if I am unavailable. [The messageboard can be accessed through this link: http://armaggonvba.boards.net]http://armaggonvba.boards.net Introduction to VBA - (Development Workshop)

3 2 The History and Logic of VBA Many years ago (in 1964) the BASIC programming language was constructed from system code (1’s and 0’s) to assist people with simplified programming instructions which could be coded into their computer. VBA is a programming language built off libraries of code created by programmers which have been developed from BASIC through to Visual Basic, then to Visual Basic for Applications – [VBA]) VBA uses sets of programmed code in windows to instruct your programs to act in a certain way. The ‘language’ you use is an interpretation of what you’re telling your computer to do.. There’s a very close relation between program logic and actual real life logic we use every day, we just need to know how to tell our computer to follow that logic.. And we need the knowledge to tell our computer to do something… if it can’t do what we want it to do, we need tell it how. This is the major difference between using recorded macro code vs. coded VBA,.. You can make your computer do anything. I will not go too far at the moment into the depths of what you can make your computer do solely through the use of VBA; but I will start at the basics.. And maybe some day I’ll teach you something more than just the scope of this class. Introduction to VBA - (Development Workshop)

4 3 VBA for Excel and the VBA Interface VBA is used universally through most major Microsoft applications – including Outlook, Word, Excel, Access, Powerpoint and Project), each system contains its own code set which is specifically programmed to control and manipulate data within its own program. The primary focus of this module is VBA for Excel. To engage the VBA interface (within any Microsoft application which is controllable via VBA code) press Alt-F11, this will bring up the VBA interface which is a backbone behind the active document you have up. For the latest versions of Excel you’ll need to ‘Enable Macros’ if the program prompts you to, in order to code or use a macro. Programming Standards Writing clean code is essential programming etiquette.. You write it this way so you can re-read it yourself. The upside to this is that you can follow the code for debugging and ease of use code for restructuring, the downside is that you have to press tab repeatedly; which is a very un-used key for most individuals. If you’re going to use passwords on your code, use easy to remember passwords, write them down and distribute them to many individuals; it is very easy to forget a password. I personally do not use passwords; but I can understand the reasoning. Another etiquette technique which many coders employ is the heavy use of comments within your code.. This allows you to write instructions on each line and within each section of your code to instruct yourself on what you were writing. This is a good technique to get used to, especially if others are reading and using your code or macros. I will explain how to place comments into your code as we progress through this class. Introduction to VBA - (Development Workshop)

5 4 Standardized Definitions In programming (or specifically for this class’s learning exercises) we need to use specific definitions for terminology to ensure that we’re all on the same page. It’s very hard to explain something to you if you don’t understand what I’m talking about. For example some individuals refer to the physical computer as the CPU, some call it the Hard-Drive, the Tower, the ‘Computer’, the ‘Box’, and some don’t actually have a name for it. I personally call it many things, usually the ‘PC’ when I’m referring to it. If I came to you and said unplug your tower, you might be a bit confused. If you came to me and said ‘unplug your Hard-Drive’ I might physically go into my computer and remove the Hard-Drive from the computer case and hand it to you… theoretically. Anyway, this is the reason I have created this section and I will touch on a few of the more common used definitions I use just to get us on the same base. Introduction to VBA - (Development Workshop)

6 5 Syntax This is generally what we call system code, for instance (if you enter the proper syntax within your program, it will work) if you use bad syntax then your program will not work. Workbook This is the actual Excel document which you will see as.xls,.xlsx,.xlsm, it consists of many things (including different spreadsheets, modules, forms and data). Sheet This is what Excel refers to as a spreadsheet, tab, page, etc. A ‘sheet’ is the proper way to specify one table of information that you see, many spreadsheets create a workbook. Module The area where you actually write your code, you can create and maintain many modules if you want to reuse data between different programs, essentially you can think of a module as a library of functions you build. Introduction to VBA - (Development Workshop)

7 6 Loop A loop is a specific function which is run multiple times to carry out the same process. You specify what you’re wanting to happen (such as changing the cell format from 2015-03-2016 to March 16, 2015) and how many times you want it to happen (such as from row 1 to row 50). This is a very critical piece of programming; the majority of all the work you complete in Excel VBA will be through the use of loops. I will explain in depth how to use and code these. Function A function is a pre-programmed set of instructions which you create (or use) to allow for quick reuse of code over and over to repeat the same process. This is what I consider to be the fundamental basis of programming. If you want to think of it in regular – every day logic you could think of a function as a series of steps used to do something, if you wake up in the morning and prepare yourself for your day, you’re working through a series of steps.. in this case the function could be called ‘goToWork’ and the steps would be the various events which take place in your morning routine. Introduction to VBA - (Development Workshop)

8 7 Pretzel This is a term I use, I’m unsure if other programmers use it but I’ve found it over my years of programming, it generally appears when you try to use too much logic in small areas or use too many array dimensions and get very confused. Array A variable which contains multiple areas for data, essentially you could think of this as a spreadsheet in Excel, but it’s held within system memory), you dimensionalize it with many spots to place data [elements], and it holds that amount of data. For instance if you made an array called ‘accountNumbers’ you could give it 10 spots to hold data, then in the first spot you would put one account number, in the second spot another account number, etc. Another.. Possibly simpler definition of this would be a ‘list’ - which if you could imagine a shopping list where you write down various things you want to buy, the paper would you be the ‘array’, and what you’re wanting to buy would be the ‘elements’. Introduction to VBA - (Development Workshop)

9 8 Debug When you write any program, the more complex it becomes to you, the more you will want to see what its actually doing. Especially if it either doesn’t work the way you want it to, or if you get a popup telling you that you have Run-Time errors. Run-Time Errors This is a piece of poorly written code (or if you’re in the process of learning), often this is will appear if you try to make your program do something illogical (like place a text value [A, B, C, etc] within an integer variable [which can only contain numbers]. Or if you try to something you can’t do in the way you’re trying to do it… such as trying to control ADP without opening it. Break This is a way to pause your code. If you need to debug your program code you can click in the left bar beside where you type each line and a purple circle will appear. When you run your code and it reaches the line with the purple circle the code will ‘break’ until you let it proceed. Introduction to VBA - (Development Workshop)

10 9 Programming Logic (1) Generally when we speak of programming, we speak of the logic which drives it.. you need to understand how regular logic can be applied to a program while you’re creating it. This is the difference between designing a program, and writing code.. you design the logic, and you type the code. The below steps are the programming logic used to write a simple program in Excel, I will explain at the end what the program does (in the below set of logic we assume there are two spreadsheets containing only data in the column A, both spreadsheets have different account numbers, some exist duplicated on both spreadsheets, some are unique and will not be found on both): 1. Select Sheet 2 2. Determine the last row used on Sheet 2 3. Set an array (a list within system memory) to hold the account numbers from Sheet 2 (everything from 1 to the last row) 4. Search through each row and place the account numbers in the array 5. Select Sheet 1 6. Determine the last row used on Sheet 1 7. Go to Cell A1 and go through the entire account list from Sheet 2 8. If the Cell A1 account number is found in the Sheet 2 account list then change the Cell colour to Yellow, go to the next Cell (A2) 9. If the Cell A1 account number is not found then do nothing and carry on to the next Cell (A2) 10. Repeat steps 8 and 9 throughout every Row in Sheet 1 11. Sort Sheet 1 by Cell Colour. Introduction to VBA - (Development Workshop)

11 10 Programming Logic (2) The program logic written on the previous page is used to compare two lists of accounts (from separate spreadsheets), it first appends the data from the second spreadsheet into system memory [called an array] then cycles through each account on the first spreadsheet to see if it’s found within the array. If the account is found then it highlights the account found on both spreadsheets in yellow. It then sorts the spreadsheet by colour with the yellow ones at the top). This function is a variety of the ‘vlookup function’ if any of you are familiar with that, but it allows for many separate properties to be set up, and can be incorporated easily into larger programs. Introduction to VBA - (Development Workshop)

12 11 Defining Variables (1) A variable is anything you want it to be, you define the name and type of variable, then you place something into it. Examples of variables you might see in your life are ‘name, phone number, age, colour of hair, date of birth, awake, current debt’. The type of variables are what you need to set up. You define a variable with the below syntax (starting with the word ‘Dim’ then the variable name, then ‘As’ then the variable type. A variable cannot start with a number (1,2,3,4,5) and it cannot be multiple words. The proper way to set up a variable for ease of reading is ‘lowercaseUpperCaseNumber’, so we would call a variable ‘starting number’ = startingNumber. You cannot use predefined function names as variables (for example the word, IF, FOR, END, SUB, etc). – Excel will give you an error if you use one of these. Dim name as String Dim phoneNumber as Long Dim age as Integer Dim colourOfHair as String Dim dateOfBirth as Date Dim awake as Boolean Dim currentDebt as Double The various types of variables defined above are commonly used for specific purposes and I will further define each type below. String – “This variable type is used to house any type of item you want, it can contain numbers but you won’t be able to use those numbers for calculations unless you convert them to a different type of variable. (JamesRMalyk, Happy1, 1Cake, High5, 12.40)” Integer – “This variable type is used for smaller whole numbers (1, 50, 100, 500)” Long – “This variable type is used for larger numbers (but can contain smaller ones if needed, 50000, 100000000, 5, 999)” Date – ‘This variable type will be used to store a date in system memory (1/1/2013, 12/25/2015)” Boolean – “This variable type is used solely for True or False data (True, False) Double – “This variable type can store larger numbers with decimal places, typically for percentages or dollar/cent values (0.00041, 23.53, 100.00)” Introduction to VBA - (Development Workshop)

13 12 Defining Variables (2) We declare variables using the ‘dim’ function, then we place in the variable name, then we set its type. If you do not declare the variable for usage then the first type of data you place into it becomes the variable type, and you will find that your program / macro may not function the way you want it to do. You can stagger similar types of variables together, like in the below code (using a comma). Dim name, colourOfHair as String Dim phoneNumber, Age as Long The variables defined are just the container though, and they contain no data until you put something into them. You can either define the data you want inside each variable yourself; or you can ‘grab’ data from elsewhere and put it into the variable. Like we see below. Dim name, colourOfHair as String Dim phoneNumber, Age as Long name = “James” colourOfHair = name phoneNumber = 4169566555 age = phoneNumber In the above example we would see the below output, which doesn’t really make much sense; but shows how you can place data into variables by whatever you’d like (or dependent on other variables). name = “James colourofHair = “James” phoneNumber = 4169566555 age = 4169566555 Introduction to VBA - (Development Workshop)

14 13 The ‘If’ Statement The ‘If’ statement/definition is one of the fundamental starting points for logical programming. The cause “If’ and the result ‘Then’ must always go together, but there’s not always a black and white aspect of programming (Else If, and Else) are the grey areas. You must always take into account what else may happen if you do something, or you won’t get very far with programming. The syntax you write for the If statement is quite simple, you type code similar to what is written below (in this scenario we’ll check if the current highlighted cell has the text ‘James’, ‘Malyk’, or anything else). The highlighted areas below are comparable data areas where you can type in any variable comparison needed. If ActiveCell.Text = “James” then … ElseIf ActiveCell.Text = “Malyk” then … Else … End if The …’s would be additional code which you would type in to make your program do something in the particular instances of when we found the text we were looking for. ‘ActiveCell.text’ is just an example of looking at the text in the current Excel cell. It’s a system representation of the text we actually see. The below example is actual code written which would perform a function with different data in its fields. Introduction to VBA - (Development Workshop)

15 14 The ‘For’ Loop The ‘For’ loop is a function built into standard programming (and one you will use regularly in VBA), its purpose is to repeatedly carry out a series of processes over and over for as many times as you require. You define the start and the end of the loop and feed in a counter which is set up to gradually increase as it loops. The syntax you write for the For loop is a bit difficult to look at until you use it, you type code similar to what is written below (in this scenario we’ll loop through the counter ‘J’ 10 times). The highlighted areas below are comparable data areas where you can type in any variable comparison needed. For J = 1 to 10 … Next J The …’s would be additional code which you would type in to make your program do something 10 times (in the above example). You create variables instead of ‘J, 1 and 10 above’ these are all sets of different variable positions (or they can be defined with actual numbers like what I’ve completed above. The below example is actual code written which would perform a function with different data in its fields. Introduction to VBA - (Development Workshop)

16 15 Program Example (1) – Comparison of Values The below example is a step by step - fully commented program example which will: 1. Declare variable (valueAmount) so it can hold a dollar and cents value (Double variable type) 2. Declare variable (accountNumber) so it can hold alpha characters (String variable type) 3. Set the variable (valueAmount) to the amount in [D1] Cell. 4. Loop through the first 100 rows in the ‘B’ column to find the valueAmount 5. If it finds a matching amount it will go to the ‘A’ column and set the variable ‘accountNumber’ to whatever is in the ‘A’ cell. 6. If it finds a matching amount it will also go to the ‘C’ column and make the cell read ‘Value Found in Account: accountNumber’ Introduction to VBA - (Development Workshop)


Download ppt "Author/Host: James Malyk [Module 1][Module 1]. 1 The Intent of this Module The intent behind the creation of this development module came to me over the."

Similar presentations


Ads by Google