Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 101
Default 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 ***
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,489
Default 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
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default 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 ***



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 101
Default 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 ***
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,489
Default 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


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default 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 ***



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 101
Default 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 ***
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default 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 ***



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Listbox in UserForm Dale Fye Excel Discussion (Misc queries) 0 October 11th 07 09:40 PM
userform listbox cannot get listbox.value to transfer back to main sub [email protected] Excel Programming 1 May 17th 06 09:44 PM
UserForm Listbox in VBC Marcia3641 Excel Discussion (Misc queries) 7 July 22nd 05 10:20 AM
UserForm with ListBox Otto Moehrbach[_6_] Excel Programming 4 December 5th 04 07:30 PM
UserForm ListBox Otto Moehrbach[_6_] Excel Programming 3 December 30th 03 06:22 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"