Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Alert if value is already in listbox

I am trying to set up an alert (msgbox) on a userform.
If the user clicks on a value in Listbox1, AND the value is not listed in
Listbox2, then the value is added to the Listbox2 list.
1). If the value in Listbox1 is already added to Listbox2 i want a msgbox to
state that is already there.
2). I also want a value removed from Listbox2 if it is selected, how do i
Remove an item from a listbox when clicked on ?


Thus far i have:

Private Sub ListBox1_Click()
' Need to ensure only unique values are added to Listbox2 by a msgbox Alert
when a duplicate value is clicked on in Listbox1
ListBox2.AddItem ListBox1.Value
End Sub


Private Sub ListBox2_Click()
ListBox2.Removeitem ' This gives me a error though !
End Sub

--
Corey ....
The Silliest Question is generally
the one i forgot to ask.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Alert if value is already in listbox

First, if you're trying to set up a couple of listboxes that you can "transfer"
items between, you may want to review this post:

http://groups.google.com/groups?selm...BDD4%40msn.com

There is a typo in the code--
Me.ListBox2.AddItemme.ListBox1.List (iCtr)
should be:
Me.ListBox2.AddItem Me.ListBox1.List(iCtr)

If you really meant what you wrote...

(Instead of using a msgbox, I used a label)


Option Explicit
Dim BlkProc As Boolean
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub ListBox1_Click()
Dim iCtr As Long
Dim FoundAMatch As Boolean

Me.Label1.Caption = ""

If Me.ListBox1.ListIndex < 0 Then
'nothing selected
Else
FoundAMatch = False
For iCtr = 0 To Me.ListBox2.ListCount - 1
If LCase(Me.ListBox1.Value) = LCase(Me.ListBox2.List(iCtr)) Then
Me.Label1.Caption = "Already there!"
FoundAMatch = True
Exit For
End If
Next iCtr

If FoundAMatch = True Then
'do nothing
Else
Me.ListBox2.AddItem Me.ListBox1.Value
End If

End If

End Sub
Private Sub ListBox2_Click()
If BlkProc = True Then
Exit Sub
End If

With Me.ListBox2
If .ListIndex < 0 Then
'do nothing
Else
BlkProc = True
.RemoveItem .ListIndex
BlkProc = False
.ListIndex = -1
End If
End With
End Sub
Private Sub UserForm_Initialize()
Dim iCtr As Long
With Me.ListBox1
.MultiSelect = fmMultiSelectSingle
For iCtr = 1 To 5
.AddItem "A" & iCtr
Next iCtr
End With

With Me.ListBox2
.MultiSelect = fmMultiSelectSingle
For iCtr = 1 To 5
.AddItem "B" & iCtr
Next iCtr
End With

Me.Label1.Caption = ""
End Sub

Corey wrote:

I am trying to set up an alert (msgbox) on a userform.
If the user clicks on a value in Listbox1, AND the value is not listed in
Listbox2, then the value is added to the Listbox2 list.
1). If the value in Listbox1 is already added to Listbox2 i want a msgbox to
state that is already there.
2). I also want a value removed from Listbox2 if it is selected, how do i
Remove an item from a listbox when clicked on ?

Thus far i have:

Private Sub ListBox1_Click()
' Need to ensure only unique values are added to Listbox2 by a msgbox Alert
when a duplicate value is clicked on in Listbox1
ListBox2.AddItem ListBox1.Value
End Sub

Private Sub ListBox2_Click()
ListBox2.Removeitem ' This gives me a error though !
End Sub

--
Corey ....
The Silliest Question is generally
the one i forgot to ask.


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Alert if value is already in listbox

Thanks Dave,
I ended up scrapping the 2nd Listbox and using several textboxes, but this
still allowed a duplicate selection, but it is manageable.

Corey....

"Dave Peterson" wrote in message
...
First, if you're trying to set up a couple of listboxes that you can
"transfer"
items between, you may want to review this post:

http://groups.google.com/groups?selm...BDD4%40msn.com

There is a typo in the code--
Me.ListBox2.AddItemme.ListBox1.List (iCtr)
should be:
Me.ListBox2.AddItem Me.ListBox1.List(iCtr)

If you really meant what you wrote...

(Instead of using a msgbox, I used a label)


Option Explicit
Dim BlkProc As Boolean
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub ListBox1_Click()
Dim iCtr As Long
Dim FoundAMatch As Boolean

Me.Label1.Caption = ""

If Me.ListBox1.ListIndex < 0 Then
'nothing selected
Else
FoundAMatch = False
For iCtr = 0 To Me.ListBox2.ListCount - 1
If LCase(Me.ListBox1.Value) = LCase(Me.ListBox2.List(iCtr))
Then
Me.Label1.Caption = "Already there!"
FoundAMatch = True
Exit For
End If
Next iCtr

If FoundAMatch = True Then
'do nothing
Else
Me.ListBox2.AddItem Me.ListBox1.Value
End If

End If

End Sub
Private Sub ListBox2_Click()
If BlkProc = True Then
Exit Sub
End If

With Me.ListBox2
If .ListIndex < 0 Then
'do nothing
Else
BlkProc = True
.RemoveItem .ListIndex
BlkProc = False
.ListIndex = -1
End If
End With
End Sub
Private Sub UserForm_Initialize()
Dim iCtr As Long
With Me.ListBox1
.MultiSelect = fmMultiSelectSingle
For iCtr = 1 To 5
.AddItem "A" & iCtr
Next iCtr
End With

With Me.ListBox2
.MultiSelect = fmMultiSelectSingle
For iCtr = 1 To 5
.AddItem "B" & iCtr
Next iCtr
End With

Me.Label1.Caption = ""
End Sub

Corey wrote:

I am trying to set up an alert (msgbox) on a userform.
If the user clicks on a value in Listbox1, AND the value is not listed in
Listbox2, then the value is added to the Listbox2 list.
1). If the value in Listbox1 is already added to Listbox2 i want a msgbox
to
state that is already there.
2). I also want a value removed from Listbox2 if it is selected, how do i
Remove an item from a listbox when clicked on ?

Thus far i have:

Private Sub ListBox1_Click()
' Need to ensure only unique values are added to Listbox2 by a msgbox
Alert
when a duplicate value is clicked on in Listbox1
ListBox2.AddItem ListBox1.Value
End Sub

Private Sub ListBox2_Click()
ListBox2.Removeitem ' This gives me a error though !
End Sub

--
Corey ....
The Silliest Question is generally
the one i forgot to ask.


--

Dave Peterson



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
How to Create Sound Alert and Email Alert when Macro is Finish Tom Ogilvy Excel Programming 0 November 30th 06 11:12 PM
How to Create Sound Alert and Email Alert when Macro is Finish Bob Phillips Excel Programming 0 November 30th 06 09:21 PM
How to Create Sound Alert and Email Alert when Macro is Finished Bob Davison Excel Programming 0 November 30th 06 08:14 PM
VBA: Creating listbox similar to the one in Pivot table (Listbox+Checkbox) modjoe23 Excel Programming 3 August 18th 05 02:35 PM
listbox.value not equal to listbox.list(listbox.listindex,0) ARB Excel Programming 0 October 22nd 03 12:46 AM


All times are GMT +1. The time now is 12:01 AM.

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

About Us

"It's about Microsoft Excel"