Even More VBA IE 469 Spring 2017.

Slides:



Advertisements
Similar presentations
Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In.
Advertisements

By Angel Aguila. Apply the range name Category to cells A1:C1, as you normally would. When lists are subject to updates, use a dynamic range instead.
Microsoft Expression Web-Illustrated Unit J: Creating Forms.
Creating a User Form. A Better Interface Our programs can use input boxes for input and write on the spreadsheet page for output This works ok but is.
Microsoft Office XP Microsoft Excel
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Excel Project 7 Using Macros and Visual Basic for Applications (VBA) with Excel.
Using Macros and Visual Basic for Applications (VBA) with Excel
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Excel and VBA Creating an Excel Application
Exploring Office Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)
Visual Basic for Applications Class III. User Forms  We place controls on User Forms to get input from the user.  Common controls include text boxes,
Macros Excel built-in functions are great but limited Macros are a means for the user to define new functions A macro is a single command that automates.
Introduction To Form Builder
Microsoft Excel 2010 Chapter 8
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Lab 9 – User Forms Design. User Forms What are user forms? –Known as dialog boxes –Major ways for getting user input An example of using user forms: Monthly.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.
VBA for Excel. What is a spreadsheet? u An Excel spreadsheet is a set of worksheets  Each worksheets is made up of rows and columns of cells  Rows are.
IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.
Automating Tasks with Visual Basic. Introduction  When can’t find a readymade macro action that does the job you want, you can use Visual Basic code.
Ch 11: Userforms CP212 Winter Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize.
Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Visual Basic for Applications Macro Programming For Microsoft Office.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Working with option button, check box, and list box controls Visual Basic for Applications 13.
ME 142 Engineering Computation I Custom Dialog Boxes.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Chapter 9 Macros And Visual Basic For Applications.
Visual Basic Programming Introduction VB is one of the High level language VB has evolved from the BASIC language. BASIC stands for Beginners All-purpose.
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
More Form Tools Combo Box (displays a list) Check Box (yes/no) Frame (groups option buttons) Option Button (exclusive choice) Ref Edit (user selects cells)
Lab 10. User Forms Design – Code Part ► Lab 9 Review ► Continue ‘Student Record’ Example ► A Car Loan Application.
1 Chapter 4 – Decisions 4.1 Relational and Logical Operators (see other set of slides) 4.2 If Blocks (see other set of slides) 4.3 Select Case Blocks (see.
Chapter 5 Introduction To Form Builder. Lesson C Objectives  Use sequences to automatically generate primary key values in a form  Create lists of values.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
Lecture 6 – Working with VBA Sub Procedures Dr Joanna Wyrobek 1.
Introducing UserForms Joanna Wyrobek 1. Ms Excel UserForms A UserForm object is a window or dialog box that makes up part of an application's user interface.
Working with UserForms Dr Joanna Wyrobek 1. Displaying a modeless UserForm By default, UserForms are displayed modally. This means that the UserForm must.
VBA Excel Macro 1.Create a Macro: To create a macro in Excel VBA, Create a Macro: To create a macro in Excel VBA,Create a Macro: To create a macro.
The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 12 Introducing Visual Basic for Applications Microsoft Office Excel.
Chapter 2: The Visual Studio .NET Development Environment
VBA - Excel VBA is Visual Basic for Applications
Chapter 8: Writing Graphical User Interfaces
3.01 Apply Controls Associated With Visual Studio Form
Data Validation and Protecting Workbook
Computer Programming I
Performing Mail Merges
Visual programming Chapter 1: Introduction
3.01 Apply Controls Associated With Visual Studio Form
Access Project 8 Using Visual Basic for Applications (VBA) and Creating Multi-Page Forms.
More VBA IE 469 Fall 2017.
Using Procedures and Exception Handling
Microsoft Office Illustrated
Excel VBA Day 3 of 3 Tom Vorves.
User Forms.
Chapter 7 Advanced Form Techniques
UserForm.
More VBA IE 469 Spring 2018.
Excel: Tables Participation Project
Lesson 1 - Automating Tasks
Microsoft Office Excel 2003
Formulas Formulas are entered in the worksheet cell and must begin with an equal sign "=". The formula then includes the addresses of the cells whose values.
CHAPTER FOUR VARIABLES AND CONSTANTS
Presentation transcript:

Even More VBA IE 469 Spring 2017

Error Handling First of all as a VBA coder, you have to adhere to the VBA Syntax! Or else... x = 2 Range("A1").Valu = x

Error Handling Single Step Debugging vs Debugging with Breakpoints By pressing F8, you can single step through your code. The is very useful because it allows you to see the effect of each code line on your worksheet. OR, you set a breakpoint to halt execution at a specific code line.

Error Handling Your code may be just OK. But... If you do not handle any possible error, then a user input given above will crash the code!

Error Handling Solution 1: Ignoring the erroneous cells: Dim rng As Range, cell As Range Set rng = Selection For Each cell In rng On Error Resume Next cell.Value = Sqr(cell.Value) Next cell

Error Handling Solution 2: Warning user Dim rng As Range, cell As Range Set rng = Selection For Each cell In rng On Error GoTo InvalidValue: cell.Value = Sqr(cell.Value) Next cell Exit Sub InvalidValue: MsgBox "can't calculate square root at cell " & cell.Address Resume Next

ActiveX Controls

Text Box Add the following code line to the command button: TextBox1.Text = "Data imported successfully"

List Box

List Box To add items to a list, use the following command: With Sheet1.ListBox1     .AddItem "Paris"     .AddItem "New York"     .AddItem "London" End With Note: use Sheet2 if your list box is located on the second worksheet, Sheet3 if your list box is located on the third worksheet, etc. If you use these code lines outside the Workbook Open event, you might want to add the following code line before these code lines. This code line clears the list box. This way your items won't be added multiple times if you execute your code more than once. ListBox1.Clear

List Box To link this list box to a cell, right click on the list box (make sure design mode is selected) and click on Properties. Fill in D3 for LinkedCell.

Combo Box A combo box is a drop-down list from where a user can select an item or fill in his/her own choice.

Combo Box Create a Workbook Open Event. Code added to the Workbook Open Event will be executed by Excel VBA when you open the workbook.

Combo Box To add items to the combo box, add the following code lines to the Workbook Open Event With Sheet1.ComboBox1     .AddItem "Paris"     .AddItem "New York"     .AddItem "London" End With Note: use Sheet2 if your combo box is located on the second worksheet, Sheet3 if your combo box is located on the third worksheet, etc. If you use these code lines outside the Workbook Open event, you might want to add the code lines below before these code lines. The first code line clears the combo box. This way your items won't be added multiple times if you execute your code more than once. The second code line clears your own choice. ComboBox1.Clear ComboBox1.Value = ""

Combo Box To link this combo box to a cell, right click on the combo box (make sure design mode is selected) and click on Properties. Fill in D2 for LinkedCell.

Check Box A check box is a field which can be checked to store information. 

Check Box Add the following code lines: If CheckBox1.Value = True Then Range("D2").Value = 1 If CheckBox1.Value = False Then Range("D2").Value = 0

Check Box Click the check box on the sheet (make sure Design Mode is deselected).

Option Buttons (Radio Buttons) Option Buttons (a.k.a. Radio Buttons) are used to select an option among many. (Only one can be selected!)

Option Buttons (Radio Buttons) Drag two option buttons on your worksheet.

Option Buttons (Radio Buttons) Right click the first option button (make sure Design Mode is selected). If OptionButton1.Value = True Then Range("D3").Value = 10 Right click the second option button (make sure Design Mode is selected). If OptionButton2.Value = True Then Range("D3").Value = 20

Spin Button A spin button can be used to increment a number in a cell. 

Spin Button  To link this spin button to a cell, add the following code line. Range("C3").Value = SpinButton1.Value

Spin Button You can set a maximum and minimum by adding the following code lines. SpinButton1.Max = 100 SpinButton1.Min = 0 To change the incremental value, use the SmallChange property. SpinButton1.SmallChange = 2

ActiveX Controls All of these objects are useful on Worksheets. But they are more useful on UserForms!

User Forms We will create a user form to collect participant info for our Dinner Organization.

User Forms Open the Visual Basic Editor. If the Project Explorer is not visible, click View, Project Explorer. Click Insert, Userform. If the Toolbox does not appear automatically, click View, Toolbox. Your screen should be set up as below.

User Forms Add the controls: Caption will be displayed on the form. Name is the VBA object name. Control Name Caption Userform DinnerPlannerUserForm Dinner Planner Text Box NameTextBox   PhoneTextBox List Box CityListBox Combo Box DinnerComboBox Check Box DateCheckBox1 June 13th DateCheckBox2 June 20th DateCheckBox3 June 27th Frame CarFrame Car Option Button CarOptionButton1 Yes CarOptionButton2 No MoneyTextBox Spin Button MoneySpinButton Command Button OKButton OK ClearButton Clear CancelButton Cancel 7 Labels No need to change Name:, Phone Number:, etc.

User Forms To show the Userform, place a command button on your worksheet and add the following code line: Private Sub CommandButton1_Click() DinnerPlannerUserForm.Show End Sub

User Forms We are now going to create the Sub UserForm_Initialize. When you use the Show method for the Userform, this sub will automatically be executed. Open the Visual Basic Editor. In the Project Explorer, right click on DinnerPlannerUserForm and then click View Code. Choose Userform from the left drop-down list. Choose Initialize from the right drop-down list.

User Forms Private Sub UserForm_Initialize() 'Empty NameTextBox NameTextBox.Value = "" 'Empty PhoneTextBox PhoneTextBox.Value = "" 'Empty CityListBox CityListBox.Clear 'Fill CityListBox With CityListBox     .AddItem "San Francisco"     .AddItem "Oakland"     .AddItem "Richmond" End With

User Forms 'Empty DinnerComboBox DinnerComboBox.Clear 'Fill DinnerComboBox With DinnerComboBox     .AddItem "Italian"     .AddItem "Chinese"     .AddItem "Frites and Meat" End With 'Uncheck DataCheckBoxes DateCheckBox1.Value = False DateCheckBox2.Value = False DateCheckBox3.Value = False 'Set no car as default CarOptionButton2.Value = True 'Empty MoneyTextBox MoneyTextBox.Value = "" 'Set Focus on NameTextBox NameTextBox.SetFocus End Sub With this code, text boxes are emptied, list boxes and combo boxes are filled, check boxes are unchecked, etc.

User Forms We have now created the first part of the Userform. Although it looks neat already, nothing will happen yet when we click the command buttons on the Userform. Open the Visual Basic Editor. In the Project Explorer, double click on DinnerPlannerUserForm. Double click on the Money spin button. Add the following code line: Private Sub MoneySpinButton_Change() MoneyTextBox.Text = MoneySpinButton.Value End Sub This code line updates the text box when you use the spin button.

User Forms Double click on the OK button. Add the following code lines: Private Sub OKButton_Click() Dim emptyRow As Long 'Make Sheet1 active Sheet1.Activate 'Determine emptyRow emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1 'Transfer information Cells(emptyRow, 1).Value = NameTextBox.Value Cells(emptyRow, 2).Value = PhoneTextBox.Value Cells(emptyRow, 3).Value = CityListBox.Value Cells(emptyRow, 4).Value = DinnerComboBox.Value

User Forms If DateCheckBox1.Value = True Then Cells(emptyRow, 5).Value = DateCheckBox1.Caption If DateCheckBox2.Value = True Then Cells(emptyRow, 5).Value = Cells(emptyRow, 5).Value & " " & DateCheckBox2.Caption If DateCheckBox3.Value = True Then Cells(emptyRow, 5).Value = Cells(emptyRow, 5).Value & " " & DateCheckBox3.Caption If CarOptionButton1.Value = True Then     Cells(emptyRow, 6).Value = "Yes" Else     Cells(emptyRow, 6).Value = "No" End If Cells(emptyRow, 7).Value = MoneyTextBox.Value End Sub First, we activate Sheet1. Next, we determine emptyRow. The variable emptyRow is the first empty row and increases every time a record is added. Finally, we transfer the information from the Userform to the specific columns of emptyRow.

User Forms Double click on the Clear button. Add the following code line: Private Sub ClearButton_Click() Call UserForm_Initialize End Sub This code line calls the Sub UserForm_Initialize when you click on the Clear button.

User Forms Double click on the Cancel Button. Add the following code line: Private Sub CancelButton_Click() Unload Me End Sub

User Forms

Next time... Using Solver via VBA