是否可以使用列表框更改工作表中的行顺序?我在网上搜索了大约一个小时没有结果。我正在使用以下代码执行任务:
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
译员更加
答案 0 :(得分:1)
你当然可以做到这一点!
这是一个快速执行此操作的代码,我会留给您将其放置在需要的地方:
For i = 0 To ListBox1.ListCount - 1
ActiveWorkbook.Sheets("Sheet1").Range("A" & CStr(i + 1)).Value = ListBox1.List(i)
Next i
您可能需要更改for循环内部的内容以更好地反映您自己的代码。要写入特定范围,只需添加您想要的任何起始行号!