Option Explicit
Private Sub UserForm_Activate()
populateBox
End Sub
Private Sub CommandButton_Add_Click()
Dim iRow As Long
If TextBox_Item.Value = "" Then
MsgBox "You forgot to enter an item", , "Error"
Exit Sub
End If
With ThisWorkbook.Sheets("ListHolder")
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
End Sub
Private Sub CommandButton_Remove_Click()
Dim i As Long
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
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
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()
ThisWorkbook.Save
Unload Me
End Sub
Private Sub CommandButton_Cancel_Click()
Unload Me
End Sub
|