SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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
 

Similar a 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
 

Similar a 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
 

Más de 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
 

Último

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 

Último (20)

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

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)