是否可以使用列表框更改工作表中的行顺序?我在网上搜索了大约一个小时,没有得到任何结果。我正在使用以下代码执行任务:
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
希望有人能给我一个提示!
更新
我想要的是,例如,如果我在工作表的一列中有这些值:
第1周
第2周
第3周
第4周
然后我想用ListBox中的SpinButton重新捕获这些值的顺序;
第2周
第4周
第1周
第3周
你肯定能做到!
这里有一个通用的快速代码,我会让你把它放在需要的地方:
For i = 0 To ListBox1.ListCount - 1
ActiveWorkbook.Sheets("Sheet1").Range("A" & CStr(i + 1)).Value = ListBox1.List(i)
Next i
您可能需要更改for循环中的内容,以便更好地反映您自己的代码。对于写入特定范围,只需添加您想要的任何起始行号即可!