我是一個IT愛好者,但我不太擅長編程或VBA。作爲一個側面項目,我正在編寫一些數據,並希望使用戶友好。我是新來的論壇,所以任何建議都會受到歡迎。用戶自定義列表框的排序功能(升序)Excel
我有一個用戶窗體,有一個列表框,它有一個大量的城市列表,但列表是未排序的。我知道我可以進入最後一頁,我有國家資本列表連接到列表框,並直接在工作表中對列進行排序,但那會毀了我的國家列表,所以我想對用戶表單列表框中的列表進行排序, 有沒有辦法做到這一點?
我也希望能夠在Userform本身內添加一個Userform'find'函數,因爲我已經這樣做了,但我不確定如何使它工作,儘管嘗試了一些代碼,但我失敗了,如果你確實知道,那麼聽到任何類型的建議都會很棒,謝謝您。
請在下面的鏈接中找到描述目標和我目前擁有的代碼的圖片。
文件:
https://www.sendspace.com/file/d4iaui
Sub Listb(target)
Location.ListBox1.List = Range("countrycapital").Value
For j = 0 To Location.ListBox1.ListCount - 1
Location.ListBox1.Selected(j) = False
Next j
currentrow = target.Row
'Location.Cells(19, 2) = Sheets("Practice List").Cells(target.Row, 3)
locval = target & ","
k = 0
For i = 1 To Len(locval)
Length = Abs(k - Application.WorksheetFunction.Search(",", locval, i))
Values = Mid(locval, i, Length - 1)
For j = 0 To Location.ListBox1.ListCount - 1
If Location.ListBox1.List(j) = Values Then
Location.ListBox1.Selected(j) = True
GoTo nxt
End If
Next j
nxt:
i = Application.WorksheetFunction.Search(",", locval, i)
k = i
Next i
Location.Show
End Sub
Sub newlocation()
Location.ListBox1.List = Range("countrycapital").Value
For j = 0 To Location.ListBox1.ListCount - 1
Location.ListBox1.Selected(j) = False
Next j
Location.Show
End Sub
Private Sub CommandButton1_Click()
Call ThisWorkbook.checkcriteria
End Sub
Private Sub CommandButton2_Click()
End Sub
Private Sub ListBox1_Click()
End Sub
Private Sub UserForm_Initialize()
Dim vaItems As Variant
Dim i As Long, j As Long
Dim vTemp As Variant
Me.ListBox1.AddItem "B" 'these new added values show on the userform
Me.ListBox1.AddItem "A" ' instead, I would like the original Listbox1...
Me.ListBox1.AddItem "D" ' ...incorporated within the sort function
Me.ListBox1.AddItem "C"
'Put the items in a variant array
vaItems = Me.ListBox1.List
'Steal code from John Walkenbach’s Excel Power Programming
'with VBA to sort the array
For i = LBound(vaItems, 1) To UBound(vaItems, 1) - 1
For j = i + 1 To UBound(vaItems, 1)
If vaItems(i, 0) > vaItems(j, 0) Then
vTemp = vaItems(i, 0)
vaItems(i, 0) = vaItems(j, 0)
vaItems(j, 0) = vTemp
End If
Next j
Next i
'Clear the listbox
Me.ListBox1.Clear
'Add the sorted array back to the listbox
For i = LBound(vaItems, 1) To UBound(vaItems, 1)
Me.ListBox1.AddItem vaItems(i, 0)
Next i
End Sub
Private Sub ListBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
HookListBoxScroll Location, Location.ListBox1
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
UnhookListBoxScroll
End Sub
請在您的帖子中包含所有相關代碼,並且**不要**僅包含指向託管網站的鏈接,特別是不要將您的代碼作爲圖片。 – 2015-03-02 05:25:54
內森,感謝您的建議:圖片不是代碼,圖片顯示了用戶表單和我的主要目標是什麼。我會發布相關的代碼,看看你是否有一些時間來看看它。再次感謝 – phenomenarchi 2015-03-02 06:08:24
對存儲位置的範圍進行排序並不容易? – 2015-03-02 07:23:10