ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   on Listbox in Userform.... (https://www.excelbanter.com/excel-programming/374278-listbox-userform.html)

x taol

on Listbox in Userform....
 
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 ***

Andy Pope

on Listbox in Userform....
 
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

x taol wrote:
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 ***


--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info

Bob Phillips

on Listbox in Userform....
 
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 taol" wrote in message
...
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 ***




x taol

on Listbox in Userform....
 


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 ***

Andy Pope

on Listbox in Userform....
 
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

x taol wrote:

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 ***


--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info

Bob Phillips

on Listbox in Userform....
 
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)

"x taol" wrote in message
...


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 ***




x taol

on Listbox in Userform....
 

thank you Andy Pope

if top button press, the select item move top. how code?


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

Bob Phillips

on Listbox in Userform....
 
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)

"x taol" wrote in message
...

thank you Andy Pope

if top button press, the select item move top. how code?


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





All times are GMT +1. The time now is 02:25 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com