SlideShare ist ein Scribd-Unternehmen logo
1 von 63
Getting Started
with UserformsLECTURE 2
More on using named ranges in VBA
Userforms and their…
Controls and
Events
Properties of userform controls
Coding controls within VBA
SUMMARY
NAMED RANGES
 What if I have a large range, with…
Many rows and
Many columns?
But I want do calculations with…
Just ONE column, OR
Just ONE row?
NAMED RANGES
 We can use code to refer to any specific
Column, or
Row
NAMED RANGES
Range(“StockPrices”)
 We can use code to refer to any specific
Column, or
Row
NAMED RANGES
Range(“StockPrices”).Columns(1)
 We can use code to refer to any specific
Column, or
Row
NAMED RANGES
Range(“StockPrices”).Columns(2)
 We can use code to refer to any specific
Column, or
Row
NAMED RANGES
Range(“StockPrices”).Columns(3)
Let’s find the average of Stock 2
USING FUNCTIONS WITH RANGES
Cells(13, 2) = Application.Worksheetfunction.Average(Range(“StockPrices”).Columns(2))
The cell I want to put
the average in
Let’s find the average of Stock 2
USING FUNCTIONS WITH RANGES
Cells(13, 2) = Application.Worksheetfunction.Average(Range(“StockPrices”).Columns(2))
I want VBA to evaluate
the Average using
Excel’s built-in function
Let’s find the average of Stock 2
USING FUNCTIONS WITH RANGES
Cells(13, 2) = Application.Worksheetfunction.Average(Range(“StockPrices”).Columns(2))
The range I want to find
the average of:
Column 2 (Stock 2)
Of “StockPrices”
Download Lecture 2 Student Example.xlsm
Use Module 1
Use procedure “AverageStockPrices”
Use built-in functions to calculate the average of
stock 1 and stock 3 and output the result in row
13, just below the prices for each stock.
EXERCISE. FIND THE AVERAGE OF STOCK 3
The
Basics
USERFORMS
INSERT A USERFORM
User Interface
Use these controls to
construct the UserForm
Put the following 4 controls on your userform
Label
Textbox
Combobox
Command button
INSERT A USERFORM
Single click on the userform
The properties window should be
visible (bottom left of screen).
Single click on your controls
The properties window will change
PROPERTIES WINDOW
Can’t see the properties window?
SHOW THE PROPERTIES WINDOW
The userform has its own properties
Each control has its own properties
We can modify these properties…
Using the Properties window, or
Using code in VBA
PROPERTIES
For example
To set the Height property of
UserForm1 to 250, either…
• Change the height in the
properties window (right)
to 250, or
• Write this in your code:
UserForm1.Height = 250
PROPERTIES WINDOW
Properties Window for UserForm1
The caption is what
the user sees.
Change the caption on your
useform to your name
PROPERTIES WINDOW
Properties Window for UserForm1
The (Name) of the userform
within your VBA Code
Change the (Name) of your
useform to Userform_Lec2
MODIFY THE DESIGN
Change the
BackColor.
Change the BackColor of:
• Your userform, or
• One of your controls
MODIFY THE DESIGN
Add a picture
Add a Picture to
your userform
Run your userform (F5)
Click on your controls. Do they do anything?
No.
Nothing will happen until you write code to tell VBA what
to do with each control.
This is important! VBA does not know what you want to
do. It cannot read your mind. Just because you label a
command button ‘Calculate’ does not mean it will calculate
anything. You have to tell VBA what to do.
Exit the userform
RUN A USERFORM
Choose one control
Disable it using the Enabled property
Choose a different control
Make it invisible using the Visible property
Run your userform again.
Check the controls that were disabled or made
invisible. Did it work?
Enable and make Visible all controls
MODIFY ATTRIBUTES OF CONTROLS
A FEW CONTROLS
Label
• Label elements on the
Userform.
• Insert instructions for
the user.
A FEW CONTROLS
Textbox
• Allow the user to
enter information
A FEW CONTROLS
Combobox
• Drop-down list of
choices.
• User selects one item
from list.
A FEW CONTROLS
CommandButton
• Write code to execute
when clicked.
• Use for Ok, Next, Cancel,
Exit, back, Clear form, etc.
 To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
I double clicked a
UserForm
 To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
Any code I put here will run when
the userform is clicked
 Enter the code below into this procedure
 Run your userform
 Click on the userform. What happened?
PROCEDURES FOR A USERFORM
 To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
I double clicked
CommandButton1
 To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
Any code I put here will run when
CommandButton1 is clicked
 Enter the code below into this procedure
 Run your userform
 Click on the command button. What happened?
PROCEDURES FOR A USERFORM
 To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
I double clicked
Label1
 To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
Any code I put here will run when
Label1 is clicked
 To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
I double clicked
Combobox1
 To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
Any code I put here will run when
ComboBox1 is changed
 To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
Any code I put here will run when the drop-
down menu of ComboBox1 is clicked
 To access the Form Module for a userform:
Double click on the userform, OR
Double click ANY control that’s on the userform
PROCEDURES FOR A USERFORM
Any code I put here will run when the drop-
down menu of ComboBox1 is clicked
 Enter the code below into this procedure
 Run your userform
 Click on the userform. What happened?
PROCEDURES FOR A USERFORM
MsgBox (“You clicked drop-down menu”)
 Select any control from the Control List
 Select under what condition you want the code to
run from the Even List
CHOOSING EVENTS
The
The
 For example,
If I want code to run when the userform starts, then I
choose:
 Userform from the Controls List
 Initialise from the Event List
If I want code to run when a button is clicked, then I
choose:
 The specific command button from the Controls List
 Click from the Event List
Most common mistake:
I want to add items to my combo box, so I choose:
 Combobox from the Controls List
 Change from the Event List
CHOOSING EVENTS
 For example,
If I want code to run when the userform starts, then I
choose:
 Userform from the Controls List
 Initialise from the Event List
If I want code to run when a button is clicked, then I
choose:
 The specific command button from the Controls List
 Click from the Event List
Most common mistake:
I want to add items to my combo box, so I choose:
 Combobox from the Controls List
 Change from the Event List
CHOOSING EVENTS
WRONG!
Most common mistake:
I want to add items to my combo box, so I choose:
 Combobox from the Controls List
 Change from the Event List
Why is this wrong?
The combobox won’t show the items until AFTER the user
clicks on it and changes it’s value…
But, the user can’t do this because it will be empty!
Correct way to do it:
Adding items should be done before the user sees the
userform (so they can make a selection)
 Userform from the Controls List
 Initialise from the Event List
CHOOSING EVENTS
WRONG!
INITIALISE A USERFORM
Prepare the Userform for the user to use:
Clear TextBoxes.
Fill ComboBoxes (the drop-down menu)
Give ComboBoxes an initial value (e.g. “<Select One>”)
Make sure labels have captions (i.e., they say something)
Chose an initial OptionButton
Deselect all CheckBoxes (or choose an initial one)
Populate ListBoxes
Put the cursor in the first box you want the user to enter
information.
Etc…
INITIALISE A USERFORM
Code goes in a specific procedure
USERFORM INITIALISE
Choose Userform from
the
Select Initialise from
the
USERFORM INITIALISE
Any code I put here will run BEFORE the user
sees the userform
From
within your
VBA code
CODING CONTROLS
Give a textbox a value
TextBox1.value = “Hello”
TextBox2.value = 17
ASSIGNING VALUES WITHIN VBA
Use .value after
the (Name) of
the textbox
Give a Label a value
Label1.caption = “Hello”
Label2.caption = 17
ASSIGNING VALUES WITHIN VBA
Use .caption
after the (Name)
of the Label
Add items to a combo box
ComboBox1.AddItem “Item ONE”
ComboBox1.AddItem “Item TWO”
ASSIGNING VALUES WITHIN VBA
BEFORE running the code AFTER running the code AFTER clicking drop-down
menu
Use .AddItem
after the (Name)
of the combobox
If you want to add multiple items you can also use
 A named range. The range MUST be a column in Excel.
Or a With statment:
ASSIGNING VALUES WITHIN VBA
Type text in “ ” or use items from a named range
Add items to a combo box
Use .AddItem property, or
Use .RowSource property
ASSIGNING VALUES WITHIN VBA
BEFORE running the code AFTER running the code AFTER clicking drop-down
menu
Should a this be blank? Will the user know what to do?
Show an initial value in a combobox
ComboBox1.value = “<Select One>”
ASSIGNING VALUES WITHIN VBA
BEFORE running the code AFTER running the code AFTER clicking drop-down
menu
Which combobox item was selected?
ComboBox1.ListIndex
ASSIGNING VALUES WITHIN VBA
AFTER clicking drop-down
menu
-1
0
1
VBA counts the initial value as item -1 in the list
If no item was selected the value of
Combobox1.ListIndex would be -1
Which combobox item was selected?
ComboBox1.ListIndex
ASSIGNING VALUES WITHIN VBA
AFTER clicking drop-down
menu
-1
0
1
VBA counts the 1st list item as item 0
If “ItemONE” was selected the value of
Combobox1.ListIndex would be 0
Which combobox item was selected?
ComboBox1.ListIndex
ASSIGNING VALUES WITHIN VBA
AFTER clicking drop-down
menu
-1
0
1
VBA counts the 2nd list item as item 1
If “ItemTWO” was selected the value of
Combobox1.ListIndex would be 1
Which combobox item was selected?
ComboBox1.ListIndex
ASSIGNING VALUES WITHIN VBA
AFTER clicking drop-down
menu
AFTER selecting an item
from the drop-down
The value of
Combobox1.ListIndex
is 1
-1
0
1
Which combobox item was selected?
ComboBox1.ListIndex
ASSIGNING VALUES WITHIN VBA
AFTER clicking drop-down
menu
What would be the value of
ComboBox1.ListIndex if “ItemFOUR”
was selected?
You are ready to move on when…
LO6: You can write code to use a specific row or column of a named
range. E.g., apply a built-in function to a specific row or column of
named range rather than the entire range.
LO7: You can add controls to a userform and edit their properties via
the properties window. You also understand that properties of a
control or userform can be changed within the code.
LO8: You can describe the purpose and use of the following controls:
label, textbox, combobox and command button. You can also
correctly choose which of these controls should be used on a
userform based on the requirements of the program.
LO9: You understand that code must be written in the correct
procedure. In addition, you can choose the correct control (from the
Control List) and event (from the Event List) when creating a userform
procedure.
These LOs will be practiced in the lab.
LEARNING OUTCOMES (LO)
THE END

Weitere ähnliche Inhalte

Was ist angesagt?

Buttons In .net Visual Basic
Buttons In .net Visual BasicButtons In .net Visual Basic
Buttons In .net Visual Basicmanish maurya
 
LAYERS asp.net ppt
LAYERS asp.net pptLAYERS asp.net ppt
LAYERS asp.net pptIMEI
 
The visual studio start page is shown in the figure below
The visual studio start page is shown in the figure belowThe visual studio start page is shown in the figure below
The visual studio start page is shown in the figure belowTan Ps
 
Power Point Project 5
Power Point Project 5Power Point Project 5
Power Point Project 5lonetree
 
Access tips access and sql part 5 more instant queries 1
Access tips  access and sql part 5  more instant queries 1Access tips  access and sql part 5  more instant queries 1
Access tips access and sql part 5 more instant queries 1quest2900
 
4.7.14&amp;17.7.14&amp;23.6.15&amp;10.9.15
4.7.14&amp;17.7.14&amp;23.6.15&amp;10.9.154.7.14&amp;17.7.14&amp;23.6.15&amp;10.9.15
4.7.14&amp;17.7.14&amp;23.6.15&amp;10.9.15Rajes Wari
 
Creating a quiz using visual basic 6
Creating a quiz using visual basic 6Creating a quiz using visual basic 6
Creating a quiz using visual basic 6Ella Marie Wico
 
Advance communication system manual
Advance communication system manualAdvance communication system manual
Advance communication system manualanuruddhsharma1
 
Visual basic 6 black book
Visual basic 6 black bookVisual basic 6 black book
Visual basic 6 black bookAjay Goyal
 
Universal Plant View
Universal Plant ViewUniversal Plant View
Universal Plant ViewDale Thompson
 

Was ist angesagt? (19)

Windowforms controls c#
Windowforms controls c#Windowforms controls c#
Windowforms controls c#
 
Buttons In .net Visual Basic
Buttons In .net Visual BasicButtons In .net Visual Basic
Buttons In .net Visual Basic
 
SPF WinForm Programs
SPF WinForm ProgramsSPF WinForm Programs
SPF WinForm Programs
 
LAYERS asp.net ppt
LAYERS asp.net pptLAYERS asp.net ppt
LAYERS asp.net ppt
 
The visual studio start page is shown in the figure below
The visual studio start page is shown in the figure belowThe visual studio start page is shown in the figure below
The visual studio start page is shown in the figure below
 
Power Point Project 5
Power Point Project 5Power Point Project 5
Power Point Project 5
 
Access tips access and sql part 5 more instant queries 1
Access tips  access and sql part 5  more instant queries 1Access tips  access and sql part 5  more instant queries 1
Access tips access and sql part 5 more instant queries 1
 
WPF Line of Business Control Templates Styles
WPF Line of Business Control Templates StylesWPF Line of Business Control Templates Styles
WPF Line of Business Control Templates Styles
 
4.7.14&amp;17.7.14&amp;23.6.15&amp;10.9.15
4.7.14&amp;17.7.14&amp;23.6.15&amp;10.9.154.7.14&amp;17.7.14&amp;23.6.15&amp;10.9.15
4.7.14&amp;17.7.14&amp;23.6.15&amp;10.9.15
 
Creating a quiz using visual basic 6
Creating a quiz using visual basic 6Creating a quiz using visual basic 6
Creating a quiz using visual basic 6
 
Advance communication system manual
Advance communication system manualAdvance communication system manual
Advance communication system manual
 
Visual basic 6 black book
Visual basic 6 black bookVisual basic 6 black book
Visual basic 6 black book
 
Universal Plant View
Universal Plant ViewUniversal Plant View
Universal Plant View
 
Vb%20 tutorial
Vb%20 tutorialVb%20 tutorial
Vb%20 tutorial
 
Treeview listview
Treeview listviewTreeview listview
Treeview listview
 
Unit2
Unit2Unit2
Unit2
 
Screen based controls in HCI
Screen based controls in HCIScreen based controls in HCI
Screen based controls in HCI
 
INPUT BOX- VBA
INPUT BOX- VBAINPUT BOX- VBA
INPUT BOX- VBA
 
Tugas testing
Tugas testingTugas testing
Tugas testing
 

Ähnlich wie Ma3696 Lecture 2

Web Server Controls VB Set 1
Web Server Controls VB Set 1Web Server Controls VB Set 1
Web Server Controls VB Set 1sunmitraeducation
 
Visual Basic IDE Introduction
Visual Basic IDE IntroductionVisual Basic IDE Introduction
Visual Basic IDE IntroductionAhllen Javier
 
Visual Basic IDE Intro.pdf
Visual Basic IDE Intro.pdfVisual Basic IDE Intro.pdf
Visual Basic IDE Intro.pdfsheenmarie0212
 
c programming 109.docx
c programming 109.docxc programming 109.docx
c programming 109.docxwrite31
 
Manage Picklist & Global Picklist Value in Salesforce with BOFC
Manage Picklist & Global Picklist Value in Salesforce with BOFCManage Picklist & Global Picklist Value in Salesforce with BOFC
Manage Picklist & Global Picklist Value in Salesforce with BOFCAtocloud
 
CheckBox In C#.pptx
CheckBox In C#.pptxCheckBox In C#.pptx
CheckBox In C#.pptxSlemanIsmail
 
Getting started with the visual basic editor
Getting started with the visual basic editorGetting started with the visual basic editor
Getting started with the visual basic editorputiadetiara
 
VB PPT by ADI PART4.pdf
VB PPT by ADI PART4.pdfVB PPT by ADI PART4.pdf
VB PPT by ADI PART4.pdfAdiseshaK
 
Oracle forms Lesson 15 debuging triggers
Oracle forms Lesson 15  debuging triggersOracle forms Lesson 15  debuging triggers
Oracle forms Lesson 15 debuging triggersKAMA3
 
IOS Swift language 1st Tutorial
IOS Swift language 1st TutorialIOS Swift language 1st Tutorial
IOS Swift language 1st TutorialHassan A-j
 
Excel VBA.pptx
Excel VBA.pptxExcel VBA.pptx
Excel VBA.pptxGiyaShefin
 

Ähnlich wie Ma3696 Lecture 2 (20)

Tutorials2
Tutorials2Tutorials2
Tutorials2
 
Web Server Controls VB Set 1
Web Server Controls VB Set 1Web Server Controls VB Set 1
Web Server Controls VB Set 1
 
Visual Basic IDE Introduction
Visual Basic IDE IntroductionVisual Basic IDE Introduction
Visual Basic IDE Introduction
 
Visual Basic IDE Intro.pdf
Visual Basic IDE Intro.pdfVisual Basic IDE Intro.pdf
Visual Basic IDE Intro.pdf
 
Module iii part i
Module iii part iModule iii part i
Module iii part i
 
c programming 109.docx
c programming 109.docxc programming 109.docx
c programming 109.docx
 
Manage Picklist & Global Picklist Value in Salesforce with BOFC
Manage Picklist & Global Picklist Value in Salesforce with BOFCManage Picklist & Global Picklist Value in Salesforce with BOFC
Manage Picklist & Global Picklist Value in Salesforce with BOFC
 
CheckBox In C#.pptx
CheckBox In C#.pptxCheckBox In C#.pptx
CheckBox In C#.pptx
 
Getting started with the visual basic editor
Getting started with the visual basic editorGetting started with the visual basic editor
Getting started with the visual basic editor
 
VB PPT by ADI PART4.pdf
VB PPT by ADI PART4.pdfVB PPT by ADI PART4.pdf
VB PPT by ADI PART4.pdf
 
VB PPT by ADI PART4.pdf
VB PPT by ADI PART4.pdfVB PPT by ADI PART4.pdf
VB PPT by ADI PART4.pdf
 
Les15
Les15Les15
Les15
 
Oracle forms Lesson 15 debuging triggers
Oracle forms Lesson 15  debuging triggersOracle forms Lesson 15  debuging triggers
Oracle forms Lesson 15 debuging triggers
 
Visual Basic.pptx
Visual Basic.pptxVisual Basic.pptx
Visual Basic.pptx
 
Visual basic
Visual basicVisual basic
Visual basic
 
IOS Swift language 1st Tutorial
IOS Swift language 1st TutorialIOS Swift language 1st Tutorial
IOS Swift language 1st Tutorial
 
2 front panel
2  front panel2  front panel
2 front panel
 
Excel VBA.pptx
Excel VBA.pptxExcel VBA.pptx
Excel VBA.pptx
 
Excel ch10
Excel ch10Excel ch10
Excel ch10
 
Vb
VbVb
Vb
 

Mehr von Brunel University (9)

MA3696 Lecture 9
MA3696 Lecture 9MA3696 Lecture 9
MA3696 Lecture 9
 
MA3696 Lecture 8
MA3696 Lecture 8MA3696 Lecture 8
MA3696 Lecture 8
 
MA3696 Lecture 7
MA3696 Lecture 7MA3696 Lecture 7
MA3696 Lecture 7
 
MA3696 Lecture 6
MA3696 Lecture 6MA3696 Lecture 6
MA3696 Lecture 6
 
MA3696 Lecture 5
MA3696 Lecture 5MA3696 Lecture 5
MA3696 Lecture 5
 
Ma3696 lecture 4
Ma3696 lecture 4Ma3696 lecture 4
Ma3696 lecture 4
 
Ma3696 Lecture 3
Ma3696 Lecture 3Ma3696 Lecture 3
Ma3696 Lecture 3
 
Ma3696 Lecture 0
Ma3696 Lecture 0Ma3696 Lecture 0
Ma3696 Lecture 0
 
Ma3696 Lecture 1
Ma3696 Lecture 1Ma3696 Lecture 1
Ma3696 Lecture 1
 

Kürzlich hochgeladen

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 

Kürzlich hochgeladen (20)

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 

Ma3696 Lecture 2

  • 2. More on using named ranges in VBA Userforms and their… Controls and Events Properties of userform controls Coding controls within VBA SUMMARY
  • 4.  What if I have a large range, with… Many rows and Many columns? But I want do calculations with… Just ONE column, OR Just ONE row? NAMED RANGES
  • 5.  We can use code to refer to any specific Column, or Row NAMED RANGES Range(“StockPrices”)
  • 6.  We can use code to refer to any specific Column, or Row NAMED RANGES Range(“StockPrices”).Columns(1)
  • 7.  We can use code to refer to any specific Column, or Row NAMED RANGES Range(“StockPrices”).Columns(2)
  • 8.  We can use code to refer to any specific Column, or Row NAMED RANGES Range(“StockPrices”).Columns(3)
  • 9. Let’s find the average of Stock 2 USING FUNCTIONS WITH RANGES Cells(13, 2) = Application.Worksheetfunction.Average(Range(“StockPrices”).Columns(2)) The cell I want to put the average in
  • 10. Let’s find the average of Stock 2 USING FUNCTIONS WITH RANGES Cells(13, 2) = Application.Worksheetfunction.Average(Range(“StockPrices”).Columns(2)) I want VBA to evaluate the Average using Excel’s built-in function
  • 11. Let’s find the average of Stock 2 USING FUNCTIONS WITH RANGES Cells(13, 2) = Application.Worksheetfunction.Average(Range(“StockPrices”).Columns(2)) The range I want to find the average of: Column 2 (Stock 2) Of “StockPrices”
  • 12. Download Lecture 2 Student Example.xlsm Use Module 1 Use procedure “AverageStockPrices” Use built-in functions to calculate the average of stock 1 and stock 3 and output the result in row 13, just below the prices for each stock. EXERCISE. FIND THE AVERAGE OF STOCK 3
  • 14. INSERT A USERFORM User Interface Use these controls to construct the UserForm
  • 15. Put the following 4 controls on your userform Label Textbox Combobox Command button INSERT A USERFORM
  • 16. Single click on the userform The properties window should be visible (bottom left of screen). Single click on your controls The properties window will change PROPERTIES WINDOW
  • 17. Can’t see the properties window? SHOW THE PROPERTIES WINDOW
  • 18. The userform has its own properties Each control has its own properties We can modify these properties… Using the Properties window, or Using code in VBA PROPERTIES For example To set the Height property of UserForm1 to 250, either… • Change the height in the properties window (right) to 250, or • Write this in your code: UserForm1.Height = 250
  • 19. PROPERTIES WINDOW Properties Window for UserForm1 The caption is what the user sees. Change the caption on your useform to your name
  • 20. PROPERTIES WINDOW Properties Window for UserForm1 The (Name) of the userform within your VBA Code Change the (Name) of your useform to Userform_Lec2
  • 21. MODIFY THE DESIGN Change the BackColor. Change the BackColor of: • Your userform, or • One of your controls
  • 22. MODIFY THE DESIGN Add a picture Add a Picture to your userform
  • 23. Run your userform (F5) Click on your controls. Do they do anything? No. Nothing will happen until you write code to tell VBA what to do with each control. This is important! VBA does not know what you want to do. It cannot read your mind. Just because you label a command button ‘Calculate’ does not mean it will calculate anything. You have to tell VBA what to do. Exit the userform RUN A USERFORM
  • 24. Choose one control Disable it using the Enabled property Choose a different control Make it invisible using the Visible property Run your userform again. Check the controls that were disabled or made invisible. Did it work? Enable and make Visible all controls MODIFY ATTRIBUTES OF CONTROLS
  • 25. A FEW CONTROLS Label • Label elements on the Userform. • Insert instructions for the user.
  • 26. A FEW CONTROLS Textbox • Allow the user to enter information
  • 27. A FEW CONTROLS Combobox • Drop-down list of choices. • User selects one item from list.
  • 28. A FEW CONTROLS CommandButton • Write code to execute when clicked. • Use for Ok, Next, Cancel, Exit, back, Clear form, etc.
  • 29.  To access the Form Module for a userform: Double click on the userform, OR Double click ANY control that’s on the userform PROCEDURES FOR A USERFORM I double clicked a UserForm
  • 30.  To access the Form Module for a userform: Double click on the userform, OR Double click ANY control that’s on the userform PROCEDURES FOR A USERFORM Any code I put here will run when the userform is clicked
  • 31.  Enter the code below into this procedure  Run your userform  Click on the userform. What happened? PROCEDURES FOR A USERFORM
  • 32.  To access the Form Module for a userform: Double click on the userform, OR Double click ANY control that’s on the userform PROCEDURES FOR A USERFORM I double clicked CommandButton1
  • 33.  To access the Form Module for a userform: Double click on the userform, OR Double click ANY control that’s on the userform PROCEDURES FOR A USERFORM Any code I put here will run when CommandButton1 is clicked
  • 34.  Enter the code below into this procedure  Run your userform  Click on the command button. What happened? PROCEDURES FOR A USERFORM
  • 35.  To access the Form Module for a userform: Double click on the userform, OR Double click ANY control that’s on the userform PROCEDURES FOR A USERFORM I double clicked Label1
  • 36.  To access the Form Module for a userform: Double click on the userform, OR Double click ANY control that’s on the userform PROCEDURES FOR A USERFORM Any code I put here will run when Label1 is clicked
  • 37.  To access the Form Module for a userform: Double click on the userform, OR Double click ANY control that’s on the userform PROCEDURES FOR A USERFORM I double clicked Combobox1
  • 38.  To access the Form Module for a userform: Double click on the userform, OR Double click ANY control that’s on the userform PROCEDURES FOR A USERFORM Any code I put here will run when ComboBox1 is changed
  • 39.  To access the Form Module for a userform: Double click on the userform, OR Double click ANY control that’s on the userform PROCEDURES FOR A USERFORM Any code I put here will run when the drop- down menu of ComboBox1 is clicked
  • 40.  To access the Form Module for a userform: Double click on the userform, OR Double click ANY control that’s on the userform PROCEDURES FOR A USERFORM Any code I put here will run when the drop- down menu of ComboBox1 is clicked
  • 41.  Enter the code below into this procedure  Run your userform  Click on the userform. What happened? PROCEDURES FOR A USERFORM MsgBox (“You clicked drop-down menu”)
  • 42.  Select any control from the Control List  Select under what condition you want the code to run from the Even List CHOOSING EVENTS The The
  • 43.  For example, If I want code to run when the userform starts, then I choose:  Userform from the Controls List  Initialise from the Event List If I want code to run when a button is clicked, then I choose:  The specific command button from the Controls List  Click from the Event List Most common mistake: I want to add items to my combo box, so I choose:  Combobox from the Controls List  Change from the Event List CHOOSING EVENTS
  • 44.  For example, If I want code to run when the userform starts, then I choose:  Userform from the Controls List  Initialise from the Event List If I want code to run when a button is clicked, then I choose:  The specific command button from the Controls List  Click from the Event List Most common mistake: I want to add items to my combo box, so I choose:  Combobox from the Controls List  Change from the Event List CHOOSING EVENTS WRONG!
  • 45. Most common mistake: I want to add items to my combo box, so I choose:  Combobox from the Controls List  Change from the Event List Why is this wrong? The combobox won’t show the items until AFTER the user clicks on it and changes it’s value… But, the user can’t do this because it will be empty! Correct way to do it: Adding items should be done before the user sees the userform (so they can make a selection)  Userform from the Controls List  Initialise from the Event List CHOOSING EVENTS WRONG!
  • 47. Prepare the Userform for the user to use: Clear TextBoxes. Fill ComboBoxes (the drop-down menu) Give ComboBoxes an initial value (e.g. “<Select One>”) Make sure labels have captions (i.e., they say something) Chose an initial OptionButton Deselect all CheckBoxes (or choose an initial one) Populate ListBoxes Put the cursor in the first box you want the user to enter information. Etc… INITIALISE A USERFORM
  • 48. Code goes in a specific procedure USERFORM INITIALISE Choose Userform from the Select Initialise from the
  • 49. USERFORM INITIALISE Any code I put here will run BEFORE the user sees the userform
  • 51. Give a textbox a value TextBox1.value = “Hello” TextBox2.value = 17 ASSIGNING VALUES WITHIN VBA Use .value after the (Name) of the textbox
  • 52. Give a Label a value Label1.caption = “Hello” Label2.caption = 17 ASSIGNING VALUES WITHIN VBA Use .caption after the (Name) of the Label
  • 53. Add items to a combo box ComboBox1.AddItem “Item ONE” ComboBox1.AddItem “Item TWO” ASSIGNING VALUES WITHIN VBA BEFORE running the code AFTER running the code AFTER clicking drop-down menu Use .AddItem after the (Name) of the combobox
  • 54. If you want to add multiple items you can also use  A named range. The range MUST be a column in Excel. Or a With statment: ASSIGNING VALUES WITHIN VBA Type text in “ ” or use items from a named range
  • 55. Add items to a combo box Use .AddItem property, or Use .RowSource property ASSIGNING VALUES WITHIN VBA BEFORE running the code AFTER running the code AFTER clicking drop-down menu Should a this be blank? Will the user know what to do?
  • 56. Show an initial value in a combobox ComboBox1.value = “<Select One>” ASSIGNING VALUES WITHIN VBA BEFORE running the code AFTER running the code AFTER clicking drop-down menu
  • 57. Which combobox item was selected? ComboBox1.ListIndex ASSIGNING VALUES WITHIN VBA AFTER clicking drop-down menu -1 0 1 VBA counts the initial value as item -1 in the list If no item was selected the value of Combobox1.ListIndex would be -1
  • 58. Which combobox item was selected? ComboBox1.ListIndex ASSIGNING VALUES WITHIN VBA AFTER clicking drop-down menu -1 0 1 VBA counts the 1st list item as item 0 If “ItemONE” was selected the value of Combobox1.ListIndex would be 0
  • 59. Which combobox item was selected? ComboBox1.ListIndex ASSIGNING VALUES WITHIN VBA AFTER clicking drop-down menu -1 0 1 VBA counts the 2nd list item as item 1 If “ItemTWO” was selected the value of Combobox1.ListIndex would be 1
  • 60. Which combobox item was selected? ComboBox1.ListIndex ASSIGNING VALUES WITHIN VBA AFTER clicking drop-down menu AFTER selecting an item from the drop-down The value of Combobox1.ListIndex is 1 -1 0 1
  • 61. Which combobox item was selected? ComboBox1.ListIndex ASSIGNING VALUES WITHIN VBA AFTER clicking drop-down menu What would be the value of ComboBox1.ListIndex if “ItemFOUR” was selected?
  • 62. You are ready to move on when… LO6: You can write code to use a specific row or column of a named range. E.g., apply a built-in function to a specific row or column of named range rather than the entire range. LO7: You can add controls to a userform and edit their properties via the properties window. You also understand that properties of a control or userform can be changed within the code. LO8: You can describe the purpose and use of the following controls: label, textbox, combobox and command button. You can also correctly choose which of these controls should be used on a userform based on the requirements of the program. LO9: You understand that code must be written in the correct procedure. In addition, you can choose the correct control (from the Control List) and event (from the Event List) when creating a userform procedure. These LOs will be practiced in the lab. LEARNING OUTCOMES (LO)