Excel

TheSmallman.com

an XL ideas Lab

Dashboards VBA
  • Home
  • Dashboards
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
    • Power Pivot a User Guide
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About
Menu

Excel Dashboards VBA

Street Address
City, State, Zip
Phone Number
an XL ideas Lab

Your Custom Text Here

Excel Dashboards VBA

  • Home
  • Dashboards
  • Excel Tips
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
  • PowerPivot
    • Power Pivot a User Guide
  • Courses
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About

Select First Item in a List Box Automatically with VBA

September 8, 2017 Marcus Small
ListBoxSelect.GIF

 

Maybe you want to select the first or last item in a list box through the generation of code.  It might be that when the sheet is selected the first item in the list box is selected.  Or you may wish the last item to be selected.  Whatever the case the coding is very similar.  Then all you need to do is decide the trigger for the list box itself. 

 

The first procedure will select the first item in a list box by clicking on the command button (a macro button).

Option Explicit

Private Sub CommandButton1_Click()
Dim i As Long
    For i = ListBox1.ListCount - 1 To 0 Step -1
        ListBox1.ListIndex = i
    Next i
End Sub

The following procedure will select the last item in a list box. 

Private Sub CommandButton2_Click()
Dim i As Long
    For i = 0 To ListBox1.ListCount - 1
        ListBox1.ListIndex = i
    Next i
End Sub

How do each of the procedures work?  Well they work on the premise of counting all of the list items in the list box.  In the first example a simple loop is used to start at the bottom and work up to the top of the list box 'tree'.

For i = Listbox1.Listcount - 1

Which is equal to saying

For i = 6 to 0

It is important to remember that the combo box list count starts at zero (0).

The list is 7 items in total (the length of the combo box). The Step - 1 tells the loop to take 1 off i for each iteration of the loop.

For the second procedure the opposite is called inside the loop.  Start at the top and work down to the bottom then stop.

The following Excel file will show how the above procedure works.

Listboxes.xlsm

 

← Excel Looping Through Sheet Code NamesCPA Australia's Management Accounting Conference →

Featured Posts

Excel Dashboards: Tracking a Crisis

Excel Dashboards: Tracking a Crisis
April 14, 2020

Recent Posts

Populating an Excel Table from a Range of Cells with VBA

Populating an Excel Table from a Range of Cells with VBA June 12, 2025

Fuzzy Distribution with Randbetween

Fuzzy Distribution with Randbetween May 21, 2025

Add Minimum and Maximum for Chart in Cells

Add Minimum and Maximum for Chart in Cells March 12, 2025

Inflation Over Multiple Years in a Single Cell

Inflation Over Multiple Years in a Single Cell January 10, 2025

Hubspot Dashboard

Hubspot Dashboard October 3, 2024

Monthly Dashboard With Supporting Metrics

Monthly Dashboard With Supporting Metrics September 25, 2024

Excel Show Missing Sheet Tabs

Excel Show Missing Sheet Tabs July 29, 2024

Run Macro Overnight Automatically

Run Macro Overnight Automatically June 24, 2024

Split File into Parts and Save to Directory

Split File into Parts and Save to Directory April 20, 2024

Most Popular Author

Most Popular Author December 14, 2023

 

Follow US:

 
 

MarcusSmall@thesmallman.com

 

TheSmallman.com - Making your small systems hum...
© Copyright 2013-2024 theSmallman.com All Rights Reserved.