Excel VBA(用户表单)关于列表框
是否可以使用列表框更改工作表中的行顺序?我在网上搜索了大约一个小时,没有结果。我正在使用以下代码执行此任务:Excel VBA(用户表单)关于列表框,excel,listbox,Excel,Listbox,是否可以使用列表框更改工作表中的行顺序?我在网上搜索了大约一个小时,没有结果。我正在使用以下代码执行此任务: 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
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
Private子用户表单_Initialize()
ListBox1.RowSource=“Sheet1!A1:A15”
端接头
选项显式
常量UP为整数=-1
Const DOWN为整数=1
私有子SpinButton1_SpinUp()
如果Me.ListBox1.ListCount>1,则
调用MoveListItem(向上,Me.ListBox1)
如果结束
端接头
私有子SpinButton1_向下扩展()
如果Me.ListBox1.ListCount>1,则
调用MoveListItem(向下,Me.ListBox1)
如果结束
端接头
私有子移动列表项(ByVal intDirection为整数,\u
ByRef ListBox1作为列表框)
Dim intNewPosition作为整数
将strTemp设置为字符串
intNewPosition=ListBox1.ListIndex+intDirection
strTemp=ListBox1.List(intNewPosition)
如果intNewPosition>-1\u
然后intNewPosition
希望有人能给我一个提示
更新强>
我想要的是,例如,如果我在工作表的一列中有这些值:
星期一
周2
第3周
第4周
然后我想用列表框中的SpinButton重新排列这些值的顺序
周2
第4周
星期一
第三周你肯定能做到 这里有一个简单的代码,通常可以实现这一点,我将把它留给您,让您把它放在需要的地方:
For i = 0 To ListBox1.ListCount - 1
ActiveWorkbook.Sheets("Sheet1").Range("A" & CStr(i + 1)).Value = ListBox1.List(i)
Next i
您可能需要更改for循环中的内容,以更好地反映您自己的代码。对于写入特定范围,只需添加任何您想要的起始行号 行的更改顺序是什么意思?分类?你能提供你的数据的前后顺序吗?这将有助于我们了解您想要做的事情。@我已经更新了我的问题,希望您能提供帮助!