SlideShare una empresa de Scribd logo
1 de 36
Descargar para leer sin conexión
LECTURE 8

ListBoxes
Selecting items
SUMMARY
 ListBoxes and CheckBoxes
 Selecting items from a list
 Also select corresponding information

 Show selected items

ListBoxes with multiple columns
LISTBOX PROPERTIES
Displays a list of items
0
1
2
3
4
5
6
7

Select ONE item

Select MULTI items

Each item has a position in the ListBox
 Count from the 0 position
ADD ITEMS TO A LISTBOX

.RowSource
.List
.AddItem
LISTBOX PROPERTIES: ADD A LIST
Items from a Named Range

If the named range is a column
 ListBox.RowSource = “RangeName”
 Where RangeName is the name of the range in Excel
List_CarBrands.RowSource = “CarBrands”
LISTBOX PROPERTIES: ADD A LIST
Items from a Named Range

If the named range is a row
 ListBox.List = Application.Transpose(Range(“RangeName”))
 Where RangeName is the name of the range in Excel
List_CarBrands.List =Application.Transpose(Range(“CarBrands”))
LISTBOX PROPERTIES: ADD A LIST
Items from an array

ListBox.List = NameOfArray

List_CarBrands.List = carBrands

 Where carBrands() is an array containing the names of the
brands of cars
 carBrands() has been declared and assigned the correct size and
dimension. I have assigned the names from excel to the
corresponding element of the array using a For loop.
LISTBOX PROPERTIES: ADD ITEMS
Items one at a time

ListBox.AddItem ListItem
 Where ListItem is the item you want to add to the list
 There is NO =
 If ListItem is text it needs to be in “ ”
List_CarBrands.AddItem “BMW”

You can use a With statement just like we did for
ComboBoxes.
With List_CarBrands
.AddItem “BMW”
.AddItem “Fiat”
End With

You can use ranges
instead of text
CLEAR THE LIST

.Clear
LISTBOX PROPERTIES: CLEAR LIST
Clear entire list

ListBox.Clear
If you are showing what the user has selected in a
second ListBox, you may need to clear it if they
change their selection
ListBox.RowSource
Add a named range (column)

ListBox.List
Add a named range (row)
Add items from an array

ListBox.AddItem
Add items one by one

ListBox.Clear
Clear all items in the ListBox

QUICK
SUMMARY
L i s t B ox
p ro p e r t i e s fo r
adding and
c l e a r i n g i te m s
f ro m a L i s t B ox
EXERCISE 1. ADD ITEMS TO A LIST
Download Lecture 8 Student Example.xlsm
Open Userform1
Use one of the methods just shown to add the car
brands to List_CarBrands.
GET INFO ABOUT WHAT IS
IN THE LISTBOX
(AND USER SELECTIONS)

.ListCount
.Value
.ListIndex
.List(i)
.Selected(i)
LISTBOX PROPERTIES: COUNT ITEMS
Count number of list items

ListBox.ListCount
 Returns the number of items shown in the ListBox

Dim numCars as Integer
numCars = List_CarBrands.ListCount
 What is the value of numCars?
For i = 1 to List_CarBrands.ListCount
…
Next i
 How many times will this loop run?
LISTBOX PROPERTIES: WHAT’S SELECTED?
What item was selected?

ListBox.Value
 Returns the item that is selected in the list
 ONLY works for Single Select ListBoxes NOT Multi Select

MsgBox (List_CarBrands.Value)
 What will be shown in the message box?

Range(“SelectedCar”).Value = List_CarBrands.Value
 What will be outputted to the above named range?
LISTBOX PROPERTIES: WHAT’S SELECTED?
The position of selected item

ListBox.ListIndex
 Returns the position of the item that is selected in the list
 Will only return a number

 ONLY works for Single Select ListBoxes NOT Multi Select
 Counts from 0.

MsgBox (List_CarBrands.ListIndex)
 What will be shown in the message box?
LISTBOX PROPERTIES: GET ITEMS FROM LIST
What is the ith list item?

ListBox.List(i)
 Returns the i th item from the list
 Works for Single Select and Multi Select
 Counts from 0
 We cannot use it to add items to a list
 E.g., ListBox.List(i) = …

MsgBox (List_CarBrands.List(2))
 What will be shown in the message box?
LISTBOX PROPERTIES: WHAT’S SELECTED?
What item(s) were selected?

ListBox.Selected(i) is True or False
 = True if item is selected
 = False if item is not selected
 Where i is a number or variable
 i represents a position in the ListBox
 Counts from 0

i=0
i=1
i=2
i=3
i=4
i=5
i=6
i=7

Use .Selected(i) to:
 Test whether an item in the list has been selected from
Multi-select ListBox (use .ListIndex or .Value for single select)
 Test whether ListBox.Selected(i) is TRUE or FALSE using an Ifstatement or Select Case (you’ll need a For-loop too)
EXERCISE 2. TEST WHAT IS SELECTED
Open UserForm1
When the command button is clicked
 Loop through each item in the ListBox (use .ListCount)
 Inside the loop, test if the i th item has been selected
 If it has been selected, then show the selection in a
message box.

i=1

i=4

i=5

i=7
ListBox.ListCount
Count the number of list items

ListBox.Value
Return the selected item

ListBox.ListIndex
Return the position of selected item

ListBox.List(i)
Return the i th list item

ListBox.Selected(i)
TRUE if the i th item is selected
FALSE if the i th item is not selected

QUICK
SUMMARY
L i s t B ox
p ro p e r t i e s fo r
g ett i n g i nfo
a b o u t l i s t i te m s
and user
selections
SELECT LIST ITEMS
WITHIN YOUR CODE

.Selected(i)
LISTBOX PROPERTIES: MAKE SELECTIONS
Select or unselect list items

ListBox.Selected(i) is True or False
 = True if item is selected
 = False if item is not selected
 Where i is a number or variable
 i represents a position in the ListBox
 Counts from 0

Use .Selected(i) to:
 Select an item in the list from within the code
 Deselect an item in the list from within the code

i=0
i=1
i=2
i=3
i=4
i=5
i=6
i=7
EXAMPLE. SELECT ITEMS IN A LIST
 Write code to select Mercedes from the listbox:
 Mercedes is 4 th , but counting from 0 it is 3 rd .

List_CarBrands.Selected(3) = True
EXAMPLE. SELECT ITEMS IN A LIST
Write code to select Fiat, Mercedes and Porche:

List_CarBrands.Selected(1) = True
List_CarBrands.Selected(3) = True
List_CarBrands.Selected(5) = True

Make sure List_CarBrands
is MultiSelect
EXERCISE 3. SELECT ITEMS IN A LIST
Open Userform2. Add car brands to the ListBox.
A checkbox can have a value of TRUE or FALSE
 checkbox1.value = True (means it’s checked)
 checkbox1.value = False (means it’s not checked)

In the procedure for clicking the check box:
 Write code to check if the value of the checkbox is true
 If it is true (so it’s checked) then select all items in the ListBox

Make sure List_CarBrands
is MultiSelect
EXERCISE 4. UNSELECT ITEMS IN A LIST
Open Userform2
Amend your code from Exercise 2
 If the checkbox is de-selected (i.e., the value is not true)
then unselect every item in the list.
LISTBOX PROPERTIES
What’s the difference between…
 ListBox.Value,
 ListBox.List(),
 ListBox.ListIndex and
 ListBox.Selected()?

These 2 only work for
single select ListBoxes

ListBox.Selected() is equal to TRUE or FALSE only.
ListBox.ListIndex is equal to the numbered
position of the selected item.
ListBox.Value is equal to the value of the selected
item.
ListBox.List() is equal to the value of the i th item.
That item may or may not have been selected.
LISTBOXES

Multi-column
MULTI-COLUMN LISTBOXES

Change the ColumnCount property to the number
of columns you want
Use .RowSource to add a named range
MULTI-COLUMN LISTBOXES

The number of columns
MULTI-COLUMN LISTBOXES

Do you want column
headings?
True = Yes, False = No
COLUMNHEADINGS = TRUE

Takes the row ABOVE your range as the headings
MULTI-COLUMN LISTBOXES

Adjust width of columns
Separate with ;
EXERCISE 5. MULTI-COLUMN LISTBOX
Insert a new userform.
Insert a large ListBox big enough to show all car
brands and prices.
 Make the ListBox multi-column.
 Name a range containing all prices as wells as row labels.
 Show this named range in your ListBox.
LEARNING OUTCOMES
You are ready to move on when…
 LO32: You can add items one by one or as a list to a
ListBox.
 LO33: You can determine which ListBox property should be
used to get information about the ListBox and user
selections.
 LO34: You can create a multi-column ListBox using a named
range in Excel.
THE END

Más contenido relacionado

Similar a MA3696 Lecture 8

In this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdfIn this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdf
EvanpZjSandersony
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdf
maheshkumar12354
 
Vb6 ch.7-3 cci
Vb6 ch.7-3 cciVb6 ch.7-3 cci
Vb6 ch.7-3 cci
Fahim Khan
 
Lecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docxLecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docx
SHIVA101531
 
This application is used to keep track of inventory information. A c.pdf
This application is used to keep track of inventory information. A c.pdfThis application is used to keep track of inventory information. A c.pdf
This application is used to keep track of inventory information. A c.pdf
ambikacomputer4301
 
Question 1A Python list is ��.. set of items.an unorderedan or.pdf
Question 1A Python list is ��.. set of items.an unorderedan or.pdfQuestion 1A Python list is ��.. set of items.an unorderedan or.pdf
Question 1A Python list is ��.. set of items.an unorderedan or.pdf
climatecontrolsv
 
Question 1A Python list isset of items.an unorderedan ordere.pdf
Question 1A Python list isset of items.an unorderedan ordere.pdfQuestion 1A Python list isset of items.an unorderedan ordere.pdf
Question 1A Python list isset of items.an unorderedan ordere.pdf
climatecontrolsv
 

Similar a MA3696 Lecture 8 (20)

Array list(1)
Array list(1)Array list(1)
Array list(1)
 
In this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdfIn this homework- you will write a program modify program you wrote in.pdf
In this homework- you will write a program modify program you wrote in.pdf
 
Lists
ListsLists
Lists
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdf
 
Vb6 ch.7-3 cci
Vb6 ch.7-3 cciVb6 ch.7-3 cci
Vb6 ch.7-3 cci
 
Lecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docxLecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docx
 
This application is used to keep track of inventory information. A c.pdf
This application is used to keep track of inventory information. A c.pdfThis application is used to keep track of inventory information. A c.pdf
This application is used to keep track of inventory information. A c.pdf
 
For this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdfFor this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdf
 
AD3251-LINKED LIST,STACK ADT,QUEUE ADT.docx
AD3251-LINKED LIST,STACK ADT,QUEUE ADT.docxAD3251-LINKED LIST,STACK ADT,QUEUE ADT.docx
AD3251-LINKED LIST,STACK ADT,QUEUE ADT.docx
 
Eo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5eEo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5e
 
Eo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5eEo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5e
 
Question 1A Python list is ��.. set of items.an unorderedan or.pdf
Question 1A Python list is ��.. set of items.an unorderedan or.pdfQuestion 1A Python list is ��.. set of items.an unorderedan or.pdf
Question 1A Python list is ��.. set of items.an unorderedan or.pdf
 
javacollections.pdf
javacollections.pdfjavacollections.pdf
javacollections.pdf
 
Question 1A Python list isset of items.an unorderedan ordere.pdf
Question 1A Python list isset of items.an unorderedan ordere.pdfQuestion 1A Python list isset of items.an unorderedan ordere.pdf
Question 1A Python list isset of items.an unorderedan ordere.pdf
 
How to Create Drop Down Lists in Excel, step by step
How to Create Drop Down Lists in Excel, step by stepHow to Create Drop Down Lists in Excel, step by step
How to Create Drop Down Lists in Excel, step by step
 
C# Collection classes
C# Collection classesC# Collection classes
C# Collection classes
 
Ms excel
Ms excelMs excel
Ms excel
 
Built-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdfBuilt-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdf
 
Csharp_List
Csharp_ListCsharp_List
Csharp_List
 
Data Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithmsData Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithms
 

Más de Brunel University (9)

MA3696 Lecture 9
MA3696 Lecture 9MA3696 Lecture 9
MA3696 Lecture 9
 
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 2
Ma3696 Lecture 2Ma3696 Lecture 2
Ma3696 Lecture 2
 
Ma3696 Lecture 0
Ma3696 Lecture 0Ma3696 Lecture 0
Ma3696 Lecture 0
 
Ma3696 Lecture 1
Ma3696 Lecture 1Ma3696 Lecture 1
Ma3696 Lecture 1
 

Último

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

MA3696 Lecture 8

  • 2. SUMMARY  ListBoxes and CheckBoxes  Selecting items from a list  Also select corresponding information  Show selected items ListBoxes with multiple columns
  • 3. LISTBOX PROPERTIES Displays a list of items 0 1 2 3 4 5 6 7 Select ONE item Select MULTI items Each item has a position in the ListBox  Count from the 0 position
  • 4. ADD ITEMS TO A LISTBOX .RowSource .List .AddItem
  • 5. LISTBOX PROPERTIES: ADD A LIST Items from a Named Range If the named range is a column  ListBox.RowSource = “RangeName”  Where RangeName is the name of the range in Excel List_CarBrands.RowSource = “CarBrands”
  • 6. LISTBOX PROPERTIES: ADD A LIST Items from a Named Range If the named range is a row  ListBox.List = Application.Transpose(Range(“RangeName”))  Where RangeName is the name of the range in Excel List_CarBrands.List =Application.Transpose(Range(“CarBrands”))
  • 7. LISTBOX PROPERTIES: ADD A LIST Items from an array ListBox.List = NameOfArray List_CarBrands.List = carBrands  Where carBrands() is an array containing the names of the brands of cars  carBrands() has been declared and assigned the correct size and dimension. I have assigned the names from excel to the corresponding element of the array using a For loop.
  • 8. LISTBOX PROPERTIES: ADD ITEMS Items one at a time ListBox.AddItem ListItem  Where ListItem is the item you want to add to the list  There is NO =  If ListItem is text it needs to be in “ ” List_CarBrands.AddItem “BMW” You can use a With statement just like we did for ComboBoxes. With List_CarBrands .AddItem “BMW” .AddItem “Fiat” End With You can use ranges instead of text
  • 10. LISTBOX PROPERTIES: CLEAR LIST Clear entire list ListBox.Clear If you are showing what the user has selected in a second ListBox, you may need to clear it if they change their selection
  • 11. ListBox.RowSource Add a named range (column) ListBox.List Add a named range (row) Add items from an array ListBox.AddItem Add items one by one ListBox.Clear Clear all items in the ListBox QUICK SUMMARY L i s t B ox p ro p e r t i e s fo r adding and c l e a r i n g i te m s f ro m a L i s t B ox
  • 12. EXERCISE 1. ADD ITEMS TO A LIST Download Lecture 8 Student Example.xlsm Open Userform1 Use one of the methods just shown to add the car brands to List_CarBrands.
  • 13. GET INFO ABOUT WHAT IS IN THE LISTBOX (AND USER SELECTIONS) .ListCount .Value .ListIndex .List(i) .Selected(i)
  • 14. LISTBOX PROPERTIES: COUNT ITEMS Count number of list items ListBox.ListCount  Returns the number of items shown in the ListBox Dim numCars as Integer numCars = List_CarBrands.ListCount  What is the value of numCars? For i = 1 to List_CarBrands.ListCount … Next i  How many times will this loop run?
  • 15. LISTBOX PROPERTIES: WHAT’S SELECTED? What item was selected? ListBox.Value  Returns the item that is selected in the list  ONLY works for Single Select ListBoxes NOT Multi Select MsgBox (List_CarBrands.Value)  What will be shown in the message box? Range(“SelectedCar”).Value = List_CarBrands.Value  What will be outputted to the above named range?
  • 16. LISTBOX PROPERTIES: WHAT’S SELECTED? The position of selected item ListBox.ListIndex  Returns the position of the item that is selected in the list  Will only return a number  ONLY works for Single Select ListBoxes NOT Multi Select  Counts from 0. MsgBox (List_CarBrands.ListIndex)  What will be shown in the message box?
  • 17. LISTBOX PROPERTIES: GET ITEMS FROM LIST What is the ith list item? ListBox.List(i)  Returns the i th item from the list  Works for Single Select and Multi Select  Counts from 0  We cannot use it to add items to a list  E.g., ListBox.List(i) = … MsgBox (List_CarBrands.List(2))  What will be shown in the message box?
  • 18. LISTBOX PROPERTIES: WHAT’S SELECTED? What item(s) were selected? ListBox.Selected(i) is True or False  = True if item is selected  = False if item is not selected  Where i is a number or variable  i represents a position in the ListBox  Counts from 0 i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 Use .Selected(i) to:  Test whether an item in the list has been selected from Multi-select ListBox (use .ListIndex or .Value for single select)  Test whether ListBox.Selected(i) is TRUE or FALSE using an Ifstatement or Select Case (you’ll need a For-loop too)
  • 19. EXERCISE 2. TEST WHAT IS SELECTED Open UserForm1 When the command button is clicked  Loop through each item in the ListBox (use .ListCount)  Inside the loop, test if the i th item has been selected  If it has been selected, then show the selection in a message box. i=1 i=4 i=5 i=7
  • 20. ListBox.ListCount Count the number of list items ListBox.Value Return the selected item ListBox.ListIndex Return the position of selected item ListBox.List(i) Return the i th list item ListBox.Selected(i) TRUE if the i th item is selected FALSE if the i th item is not selected QUICK SUMMARY L i s t B ox p ro p e r t i e s fo r g ett i n g i nfo a b o u t l i s t i te m s and user selections
  • 21. SELECT LIST ITEMS WITHIN YOUR CODE .Selected(i)
  • 22. LISTBOX PROPERTIES: MAKE SELECTIONS Select or unselect list items ListBox.Selected(i) is True or False  = True if item is selected  = False if item is not selected  Where i is a number or variable  i represents a position in the ListBox  Counts from 0 Use .Selected(i) to:  Select an item in the list from within the code  Deselect an item in the list from within the code i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7
  • 23. EXAMPLE. SELECT ITEMS IN A LIST  Write code to select Mercedes from the listbox:  Mercedes is 4 th , but counting from 0 it is 3 rd . List_CarBrands.Selected(3) = True
  • 24. EXAMPLE. SELECT ITEMS IN A LIST Write code to select Fiat, Mercedes and Porche: List_CarBrands.Selected(1) = True List_CarBrands.Selected(3) = True List_CarBrands.Selected(5) = True Make sure List_CarBrands is MultiSelect
  • 25. EXERCISE 3. SELECT ITEMS IN A LIST Open Userform2. Add car brands to the ListBox. A checkbox can have a value of TRUE or FALSE  checkbox1.value = True (means it’s checked)  checkbox1.value = False (means it’s not checked) In the procedure for clicking the check box:  Write code to check if the value of the checkbox is true  If it is true (so it’s checked) then select all items in the ListBox Make sure List_CarBrands is MultiSelect
  • 26. EXERCISE 4. UNSELECT ITEMS IN A LIST Open Userform2 Amend your code from Exercise 2  If the checkbox is de-selected (i.e., the value is not true) then unselect every item in the list.
  • 27. LISTBOX PROPERTIES What’s the difference between…  ListBox.Value,  ListBox.List(),  ListBox.ListIndex and  ListBox.Selected()? These 2 only work for single select ListBoxes ListBox.Selected() is equal to TRUE or FALSE only. ListBox.ListIndex is equal to the numbered position of the selected item. ListBox.Value is equal to the value of the selected item. ListBox.List() is equal to the value of the i th item. That item may or may not have been selected.
  • 29. MULTI-COLUMN LISTBOXES Change the ColumnCount property to the number of columns you want Use .RowSource to add a named range
  • 31. MULTI-COLUMN LISTBOXES Do you want column headings? True = Yes, False = No
  • 32. COLUMNHEADINGS = TRUE Takes the row ABOVE your range as the headings
  • 33. MULTI-COLUMN LISTBOXES Adjust width of columns Separate with ;
  • 34. EXERCISE 5. MULTI-COLUMN LISTBOX Insert a new userform. Insert a large ListBox big enough to show all car brands and prices.  Make the ListBox multi-column.  Name a range containing all prices as wells as row labels.  Show this named range in your ListBox.
  • 35. LEARNING OUTCOMES You are ready to move on when…  LO32: You can add items one by one or as a list to a ListBox.  LO33: You can determine which ListBox property should be used to get information about the ListBox and user selections.  LO34: You can create a multi-column ListBox using a named range in Excel.