关于ListBox的Excel VBA(用户表单)

是否有可能使用列表框来更改工作表中的行的顺序? 我已经在网上search了大约一个小时没有ressults。 我正在使用以下代码的任务:

Private Sub UserForm_Initialize() ListBox1.RowSource = "Sheet1!A1:A15" End Sub Option Explicit Const UP As Integer = -1 Const DOWN As Integer = 1 Private Sub SpinButton1_SpinUp() If Me.ListBox1.ListCount > 1 Then Call MoveListItem(UP, Me.ListBox1) End If End Sub Private Sub SpinButton1_SpinDown() If Me.ListBox1.ListCount > 1 Then Call MoveListItem(DOWN, Me.ListBox1) End If End Sub Private Sub MoveListItem(ByVal intDirection As Integer, _ ByRef ListBox1 As ListBox) Dim intNewPosition As Integer Dim strTemp As String intNewPosition = ListBox1.ListIndex + intDirection strTemp = ListBox1.List(intNewPosition) If intNewPosition > -1 _ And intNewPosition < ListBox1.ListCount Then 'Swap listbox items ListBox1.List(intNewPosition) = ListBox1.Value ListBox1.List(intNewPosition - intDirection) = strTemp ListBox1.Selected(intNewPosition) = True End If End Sub 

希望somone能给我一个提示!

UPDATE!

我想要的是,如果我例如在我的工作表中的列中有这些值:

week1

week2

译员更加

week4

然后我想用ListBox中的SpinButton重新设置这些值的顺序;

week2

week4

week1

译员更加

你当然可以做到这一点!

这是一个快速的代码,一般来说,我会把它放在你需要的地方:

 For i = 0 To ListBox1.ListCount - 1 ActiveWorkbook.Sheets("Sheet1").Range("A" & CStr(i + 1)).Value = ListBox1.List(i) Next i 

您可能需要更改for循环中的内容以更好地反映自己的代码。 要写入特定的范围只需添加任何你想要的起始行号!