on Listbox in Userform....

X

x taol

hello.

The userform have two buttons and one listbox.

i want to move up or down a item of listbox.
first, select the item of listbox and if press a button(up) of two
button, the item move up one step....

but vba code, i don't know that.


*** Sent via Developersdex http://www.developersdex.com ***
 
A

Andy Pope

Hi,

Try this.
'-----------------------------
Private Sub CommandButton1_Click()

If ListBox1.ListIndex > 0 Then
ListBox1.ListIndex = ListBox1.ListIndex - 1
End If

End Sub

Private Sub CommandButton2_Click()

If ListBox1.ListIndex < ListBox1.ListCount - 1 Then
ListBox1.ListIndex = ListBox1.ListIndex + 1
End If

End Sub

Private Sub UserForm_Initialize()

ListBox1.List = Array(1, 2, 3, 4, 5, 6)
CommandButton1.Caption = "Up"
CommandButton2.Caption = "Down"

End Sub
'-----------------------------

Cheers
Andy
 
B

Bob Phillips

Private Sub CommandButton2_Click()
With Me.ListBox1
If .ListIndex > 0 Then .ListIndex = .ListIndex - 1
End With
End Sub

Private Sub CommandButton3_Click()
With Me.ListBox1
If .ListIndex < .ListCount - 1 Then .ListIndex = .ListIndex + 1
End With
End Sub


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
X

x taol

oh, no...
i don't want to move just.
i want to move of the item. for example,

item
aaa
bbb
ccc
ddd

in that case, if "ccc" is selected, press the Up button, the sequency is
that.
aaa
ccc
bbb
ddd


*** Sent via Developersdex http://www.developersdex.com ***
 
A

Andy Pope

To move list items up and down try

Private Sub CommandButton1_Click()

Dim strTemp As String
Dim lngIndex As Long

If ListBox1.ListIndex > 0 Then
lngIndex = ListBox1.ListIndex
strTemp = ListBox1.List(lngIndex - 1)
ListBox1.List(lngIndex - 1) = ListBox1.List(lngIndex)
ListBox1.List(lngIndex) = strTemp
ListBox1.ListIndex = lngIndex - 1
End If

End Sub

Private Sub CommandButton2_Click()

Dim strTemp As String
Dim lngIndex As Long

If ListBox1.ListIndex >= 0 And _
ListBox1.ListIndex < (ListBox1.ListCount - 1) Then
lngIndex = ListBox1.ListIndex
strTemp = ListBox1.List(lngIndex + 1)
ListBox1.List(lngIndex + 1) = ListBox1.List(lngIndex)
ListBox1.List(lngIndex) = strTemp
ListBox1.ListIndex = lngIndex + 1
End If

End Sub

Cheers
Andy
 
B

Bob Phillips

Here is one way.

What I am doing is capturing the listbox elements in an array and changing
the array, then reloadingf the listbox. I have included a dummy routine to
show how I load to start etc.

Option Explicit

Dim aryList

Private Sub CommandButton1_Click()
Dim i As Long, j As Long
Dim iStart As Long
Dim iEnd As Long

iStart = Evaluate("MIN(IF(A1:A1000<>"""",MONTH(A1:A1000)))")
iEnd = Evaluate("MAX(IF(A1:A1000<>"""",MONTH(A1:A1000)))")

ReDim aryList(1 To iEnd - iStart + 1)
For i = iStart To iEnd
j = j + 1
aryList(j) = Format(DateSerial(Year(Date), i, 1), "mmmm")
Next i
ListBox1.List = aryList

End Sub

Private Sub cmdUp_Click()
Dim tmp
With Me.ListBox1
If .ListIndex > 0 Then
tmp = aryList(.ListIndex)
aryList(.ListIndex) = aryList(.ListIndex + 1)
aryList(.ListIndex + 1) = tmp
.List = aryList
End If
End With
End Sub

Private Sub cmdDown_Click()
Dim tmp
With Me.ListBox1
If .ListIndex < .ListCount - 1 Then
tmp = aryList(.ListIndex + 1)
aryList(.ListIndex + 1) = aryList(.ListIndex + 2)
aryList(.ListIndex + 2) = tmp
.List = aryList
End If
End With
End Sub

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
B

Bob Phillips

Private Sub cmdBottom_Click()
ListBox1.ListIndex = ListBox1.ListCount - 1
End Sub

Private Sub cmdTop_Click()
ListBox1.ListIndex = 0
End Sub

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top