Excel

Use a spinbutton to change items in listbox and update sheet that populates listbox

Ease of Use

Intermediate

Version tested with

2003 

Submitted by:

tpoynton

Description:

Moves selected item up and down in a listbox using a spinbutton control, and updates the values in the worksheet that populate the listbox. 

Discussion:

This is useful for spreadsheets that provide a user interface for data entry and manipulation. By having users enter data via userform, instead of directly on a worksheet, accuracy is improved and 'novice users' can enter data and perform actions with little knowledge of Excel. Submitted to KB as there are currently no entries regarding spinbutton. 

Code:

instructions for use

			

Option Explicit Private Sub UserForm_Activate() populateBox 'populates listbox on load End Sub Private Sub CommandButton_Add_Click() 'this sub is for adding new items to the listbox Dim iRow As Long 'Error Checking 'change all references to textbox_item to your textbox name If TextBox_Item.Value = "" Then MsgBox "You forgot to enter an item", , "Error" Exit Sub End If 'change line below to reflect location of your list With ThisWorkbook.Sheets("ListHolder") 'change all references to listbox_item to your listbox name If ListBox_Items.Value <> "" Then iRow = ListBox_Items.ListIndex + 2 .Rows(iRow).Insert Shift:=xlDown Else iRow = .UsedRange.Rows.Count + 1 End If .Cells(iRow, 1).Value = TextBox_Item.Value TextBox_Item.Value = "" End With populateBox 'repopulate listbox End Sub Private Sub CommandButton_Remove_Click() Dim i As Long 'change listbox name and sheet location to yours i = ListBox_Items.ListIndex + 1 With ThisWorkbook.Sheets("ListHolder") .Rows.EntireRow(i + 1).Delete End With populateBox End Sub Private Sub SpinButton1_SpinDown() Dim i As Long 'change line below to reflect location of your list, and update listbox name With ThisWorkbook.Sheets("ListHolder") i = ListBox_Items.ListIndex + 1 If i > 0 And i < .UsedRange.Rows.Count - 1 Then .Cells(i + 1, 1).Value = .Cells(i + 2, 1).Value .Cells(i + 2, 1) = ListBox_Items.Value populateBox ListBox_Items.Selected(i) = True End If End With End Sub Private Sub SpinButton1_SpinUp() Dim i As Long i = ListBox_Items.ListIndex + 1 If i > 1 And i < ActiveSheet.UsedRange.Rows.Count Then With ThisWorkbook.Sheets("ListHolder") .Cells(i + 1, 1).Value = .Cells(i, 1).Value .Cells(i, 1) = ListBox_Items.Value End With populateBox ListBox_Items.Selected(i - 2) = True End If End Sub Public Sub populateBox() ListBox_Items.Clear Dim cell As Range 'change to location of your list With ThisWorkbook.Sheets("ListHolder") If .UsedRange.Rows.Count > 1 Then For Each cell In .Range(.Cells(2, 1), .Cells(.UsedRange.Rows.Count, 1)) ListBox_Items.AddItem cell.Value Next cell End If End With End Sub Private Sub CommandButton_Save_Click() 'optional; ensures changes to order of list are saved ThisWorkbook.Save Unload Me End Sub Private Sub CommandButton_Cancel_Click() Unload Me End Sub

How to use:

  1. open Excel
  2. open VBE (alt + F11)
  3. create a userform with a listbox, spinbutton control, textbox, and two commandbuttons
  4. paste code into userform code
  5. create a worksheet with a list in column 1, and header name in row 1, column 1
  6. modify listbox, worksheet, textbox, and commandbutton names to ones in your project
  7. close VBE
 

Test the code:

  1. run userform to test code
 

Sample File:

spinbuttonFun.zip 14.59KB 

Approved by mdmackillop


This entry has been viewed 514 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express