I'm having a worksheet with the name of "Dates" where my data starts from the row 5 meaning the column headings are on the 4th row. I have a total of 8 columns in the sheet.
The sheet keeps on updating daily as I enter data to the respective columns. And this data keeps on updating a list box simultaneously.
I have added two spin buttons to arrange the rows as required by the user. Meaning to move the rows up & down from both the list box & the worksheet simultaneously.
But the spin button that I have added does not seem to be working as required. For this reason I have put up the spin button code hoping to get a suggestion why it is not working for me.
In my opinion may be I have done something wrong with the ranges.
Anyways, I would be happy if someone could let me know what is wrong in my code.
Thanks in advance & here is the code;
Code: Select all
Option Explicit
Private Sub UserForm_Activate()
populateBox 'populates listbox on load
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("Dates")
i = Listbox2.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) = Listbox2.Value
populateBox
Listbox2.Selected(i) = True
End If
End With
End Sub
Private Sub SpinButton1_SpinUp()
Dim i As Long
i = Listbox2.ListIndex + 1
If i > 1 And i < ActiveSheet.UsedRange.Rows.Count Then
With ThisWorkbook.Sheets("Dates")
.Cells(i + 1, 1).Value = .Cells(i, 1).Value
.Cells(i, 1) = Listbox2.Value
End With
populateBox
Listbox2.Selected(i - 2) = True
End If
End Sub
Public Sub populateBox()
Listbox2.Clear
Dim cell As Range
'change to location of your list
With ThisWorkbook.Sheets("Dates")
If .UsedRange.Rows.Count > 1 Then
For Each cell In .Range(.Cells(2, 1), .Cells(.UsedRange.Rows.Count, 1))
Listbox2.AddItem cell.Value
Next cell
End If
End With
End Sub