Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 58
Default command button that copies and pastes from one list box to another

I inserted the below code to accomplish the below. Why do I get an error for
the i ?

Private Sub CommandButton1_Click()
If ListBox2.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then
'See if item already existss
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,365
Default command button that copies and pastes from one list box to another

You're basing i counter on ListBox2 list items, perhaps in the first If
ListBox3 doesn't have as many items in the list? Then you get an error
because you're trying to reference something that doesn't exist.

I think you probably meant:
For i = 0 to ListBox3.ListCount-1


"gbpg" wrote:

I inserted the below code to accomplish the below. Why do I get an error for
the i ?

Private Sub CommandButton1_Click()
If ListBox2.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then
'See if item already existss
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

End Sub

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 58
Default command button that copies and pastes from one list box to ano



"JLatham" wrote:

You're basing i counter on ListBox2 list items, perhaps in the first If
ListBox3 doesn't have as many items in the list? Then you get an error
because you're trying to reference something that doesn't exist.

I think you probably meant:
For i = 0 to ListBox3.ListCount-1


"gbpg" wrote:

I inserted the below code to accomplish the below. Why do I get an error for
the i ?

Private Sub CommandButton1_Click()
If ListBox2.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then
'See if item already existss
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

End Sub

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 58
Default command button that copies and pastes from one list box to ano

If ListBox2.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then
'See if item already exists
For i = 0 To ListBox3.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

End Sub

this still does not work. I get an error compile errror

"JLatham" wrote:

You're basing i counter on ListBox2 list items, perhaps in the first If
ListBox3 doesn't have as many items in the list? Then you get an error
because you're trying to reference something that doesn't exist.

I think you probably meant:
For i = 0 to ListBox3.ListCount-1


"gbpg" wrote:

I inserted the below code to accomplish the below. Why do I get an error for
the i ?

Private Sub CommandButton1_Click()
If ListBox2.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then
'See if item already existss
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

End Sub

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,365
Default command button that copies and pastes from one list box to ano

What does the compiler error message say? And where is cbDuplicates declared
and set up? By the way: did your list box(es) come from the Controls Toolbox
or the Forms toolbox, and are they on worksheets or userforms?

Your code as it now stands?
If ListBox2.ListIndex = -1 Then Exit Sub

If Not cbDuplicates Then
'See if item already exists
For i = 0 To ListBox3.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

Let's analyze what your code is doing and see if that doesn't ring a bell
somewhere.

if ListBox2.ListIndex = -1 Then Exit Sub
we will assume you're making it past that point and there is something in
ListBox2

First Test: is cbDuplicates False (or zero)? If it is False or 0, then
For Each item in ListBox3's list
If current ListBox2 value is one of ListBox3's items then
Beep and QUIT! at the very first match, don't finish looking at all
of them.
I interpret this to mean that you do want to add ListBox2.Value to
ListBox3's list when there's no match found
However, because the
ListBox3.AddItem ListBox2.Value is outside of the ...
If Not cbDuplicates Then

End If
....block, the ListBox3.AddItem ListBox2.Value statement is ALWAYS going to
be executed, even if ListBox2.Value is already an item in ListBox3's list.
That statement probably needs to go up ahead of the last End If statement.


"gbpg" wrote:

If ListBox2.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then
'See if item already exists
For i = 0 To ListBox3.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

End Sub

this still does not work. I get an error compile errror

"JLatham" wrote:

You're basing i counter on ListBox2 list items, perhaps in the first If
ListBox3 doesn't have as many items in the list? Then you get an error
because you're trying to reference something that doesn't exist.

I think you probably meant:
For i = 0 to ListBox3.ListCount-1


"gbpg" wrote:

I inserted the below code to accomplish the below. Why do I get an error for
the i ?

Private Sub CommandButton1_Click()
If ListBox2.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then
'See if item already existss
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

End Sub



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 58
Default command button that copies and pastes from one list box to ano

I don't really need the cbDupicates.I will take this out. It doesn't work I
will send the error message.

"JLatham" wrote:

What does the compiler error message say? And where is cbDuplicates declared
and set up? By the way: did your list box(es) come from the Controls Toolbox
or the Forms toolbox, and are they on worksheets or userforms?

Your code as it now stands?
If ListBox2.ListIndex = -1 Then Exit Sub

If Not cbDuplicates Then
'See if item already exists
For i = 0 To ListBox3.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

Let's analyze what your code is doing and see if that doesn't ring a bell
somewhere.

if ListBox2.ListIndex = -1 Then Exit Sub
we will assume you're making it past that point and there is something in
ListBox2

First Test: is cbDuplicates False (or zero)? If it is False or 0, then
For Each item in ListBox3's list
If current ListBox2 value is one of ListBox3's items then
Beep and QUIT! at the very first match, don't finish looking at all
of them.
I interpret this to mean that you do want to add ListBox2.Value to
ListBox3's list when there's no match found
However, because the
ListBox3.AddItem ListBox2.Value is outside of the ...
If Not cbDuplicates Then

End If
...block, the ListBox3.AddItem ListBox2.Value statement is ALWAYS going to
be executed, even if ListBox2.Value is already an item in ListBox3's list.
That statement probably needs to go up ahead of the last End If statement.


"gbpg" wrote:

If ListBox2.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then
'See if item already exists
For i = 0 To ListBox3.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

End Sub

this still does not work. I get an error compile errror

"JLatham" wrote:

You're basing i counter on ListBox2 list items, perhaps in the first If
ListBox3 doesn't have as many items in the list? Then you get an error
because you're trying to reference something that doesn't exist.

I think you probably meant:
For i = 0 to ListBox3.ListCount-1


"gbpg" wrote:

I inserted the below code to accomplish the below. Why do I get an error for
the i ?

Private Sub CommandButton1_Click()
If ListBox2.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then
'See if item already existss
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

End Sub

  #7   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 58
Default command button that copies and pastes from one list box to ano

I get compile error variable not defined for the i

"JLatham" wrote:

What does the compiler error message say? And where is cbDuplicates declared
and set up? By the way: did your list box(es) come from the Controls Toolbox
or the Forms toolbox, and are they on worksheets or userforms?

Your code as it now stands?
If ListBox2.ListIndex = -1 Then Exit Sub

If Not cbDuplicates Then
'See if item already exists
For i = 0 To ListBox3.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

Let's analyze what your code is doing and see if that doesn't ring a bell
somewhere.

if ListBox2.ListIndex = -1 Then Exit Sub
we will assume you're making it past that point and there is something in
ListBox2

First Test: is cbDuplicates False (or zero)? If it is False or 0, then
For Each item in ListBox3's list
If current ListBox2 value is one of ListBox3's items then
Beep and QUIT! at the very first match, don't finish looking at all
of them.
I interpret this to mean that you do want to add ListBox2.Value to
ListBox3's list when there's no match found
However, because the
ListBox3.AddItem ListBox2.Value is outside of the ...
If Not cbDuplicates Then

End If
...block, the ListBox3.AddItem ListBox2.Value statement is ALWAYS going to
be executed, even if ListBox2.Value is already an item in ListBox3's list.
That statement probably needs to go up ahead of the last End If statement.


"gbpg" wrote:

If ListBox2.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then
'See if item already exists
For i = 0 To ListBox3.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

End Sub

this still does not work. I get an error compile errror

"JLatham" wrote:

You're basing i counter on ListBox2 list items, perhaps in the first If
ListBox3 doesn't have as many items in the list? Then you get an error
because you're trying to reference something that doesn't exist.

I think you probably meant:
For i = 0 to ListBox3.ListCount-1


"gbpg" wrote:

I inserted the below code to accomplish the below. Why do I get an error for
the i ?

Private Sub CommandButton1_Click()
If ListBox2.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then
'See if item already existss
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

End Sub

  #8   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,365
Default command button that copies and pastes from one list box to ano

Your code module probably has a statement at the very beginning:
Option Explicit

this requires you to declare any variable or constant you are going to use
before you use it. This is a GOOD thing, it makes sure your code is
internally consistent (guards you agains typo's if nothing else). Simply put
this statement somewhere near the beginning of your routine. The usual place
for these is right after the Sub declaration statement.

Dim i As Integer

The 'Dim' declares the variable. Now your code should run. There are other
benefits from Option Explicit and declaring your values early on - one of
those is that code runs faster, and that's important in big code pieces, or
even in small ones with lots of loops.

"gbpg" wrote:

I get compile error variable not defined for the i

"JLatham" wrote:

What does the compiler error message say? And where is cbDuplicates declared
and set up? By the way: did your list box(es) come from the Controls Toolbox
or the Forms toolbox, and are they on worksheets or userforms?

Your code as it now stands?
If ListBox2.ListIndex = -1 Then Exit Sub

If Not cbDuplicates Then
'See if item already exists
For i = 0 To ListBox3.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

Let's analyze what your code is doing and see if that doesn't ring a bell
somewhere.

if ListBox2.ListIndex = -1 Then Exit Sub
we will assume you're making it past that point and there is something in
ListBox2

First Test: is cbDuplicates False (or zero)? If it is False or 0, then
For Each item in ListBox3's list
If current ListBox2 value is one of ListBox3's items then
Beep and QUIT! at the very first match, don't finish looking at all
of them.
I interpret this to mean that you do want to add ListBox2.Value to
ListBox3's list when there's no match found
However, because the
ListBox3.AddItem ListBox2.Value is outside of the ...
If Not cbDuplicates Then

End If
...block, the ListBox3.AddItem ListBox2.Value statement is ALWAYS going to
be executed, even if ListBox2.Value is already an item in ListBox3's list.
That statement probably needs to go up ahead of the last End If statement.


"gbpg" wrote:

If ListBox2.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then
'See if item already exists
For i = 0 To ListBox3.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

End Sub

this still does not work. I get an error compile errror

"JLatham" wrote:

You're basing i counter on ListBox2 list items, perhaps in the first If
ListBox3 doesn't have as many items in the list? Then you get an error
because you're trying to reference something that doesn't exist.

I think you probably meant:
For i = 0 to ListBox3.ListCount-1


"gbpg" wrote:

I inserted the below code to accomplish the below. Why do I get an error for
the i ?

Private Sub CommandButton1_Click()
If ListBox2.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then
'See if item already existss
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

End Sub

  #9   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 58
Default command button that copies and pastes from one list box to ano

Thanks a lot. Worked like a charm!

"JLatham" wrote:

Your code module probably has a statement at the very beginning:
Option Explicit

this requires you to declare any variable or constant you are going to use
before you use it. This is a GOOD thing, it makes sure your code is
internally consistent (guards you agains typo's if nothing else). Simply put
this statement somewhere near the beginning of your routine. The usual place
for these is right after the Sub declaration statement.

Dim i As Integer

The 'Dim' declares the variable. Now your code should run. There are other
benefits from Option Explicit and declaring your values early on - one of
those is that code runs faster, and that's important in big code pieces, or
even in small ones with lots of loops.

"gbpg" wrote:

I get compile error variable not defined for the i

"JLatham" wrote:

What does the compiler error message say? And where is cbDuplicates declared
and set up? By the way: did your list box(es) come from the Controls Toolbox
or the Forms toolbox, and are they on worksheets or userforms?

Your code as it now stands?
If ListBox2.ListIndex = -1 Then Exit Sub

If Not cbDuplicates Then
'See if item already exists
For i = 0 To ListBox3.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

Let's analyze what your code is doing and see if that doesn't ring a bell
somewhere.

if ListBox2.ListIndex = -1 Then Exit Sub
we will assume you're making it past that point and there is something in
ListBox2

First Test: is cbDuplicates False (or zero)? If it is False or 0, then
For Each item in ListBox3's list
If current ListBox2 value is one of ListBox3's items then
Beep and QUIT! at the very first match, don't finish looking at all
of them.
I interpret this to mean that you do want to add ListBox2.Value to
ListBox3's list when there's no match found
However, because the
ListBox3.AddItem ListBox2.Value is outside of the ...
If Not cbDuplicates Then

End If
...block, the ListBox3.AddItem ListBox2.Value statement is ALWAYS going to
be executed, even if ListBox2.Value is already an item in ListBox3's list.
That statement probably needs to go up ahead of the last End If statement.


"gbpg" wrote:

If ListBox2.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then
'See if item already exists
For i = 0 To ListBox3.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

End Sub

this still does not work. I get an error compile errror

"JLatham" wrote:

You're basing i counter on ListBox2 list items, perhaps in the first If
ListBox3 doesn't have as many items in the list? Then you get an error
because you're trying to reference something that doesn't exist.

I think you probably meant:
For i = 0 to ListBox3.ListCount-1


"gbpg" wrote:

I inserted the below code to accomplish the below. Why do I get an error for
the i ?

Private Sub CommandButton1_Click()
If ListBox2.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then
'See if item already existss
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Value = ListBox3.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox3.AddItem ListBox2.Value

End Sub

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
clicking on a button copies the choice *(VBA) Thrava Excel Discussion (Misc queries) 1 October 2nd 06 10:42 PM
Some formulas don't track copies, pastes, fill right, fill down whiten Excel Discussion (Misc queries) 2 October 1st 06 04:41 PM
Where is a list of Command Button uses? KT in Houston Excel Discussion (Misc queries) 0 September 12th 06 05:29 PM
A Macro that Copies from Excel & Pastes into a webpage Eric Excel Discussion (Misc queries) 1 July 14th 06 12:38 AM
Copies 02/22/2005, Pastes 02/21/2001 Carole O Excel Worksheet Functions 3 March 2nd 05 08:28 PM


All times are GMT +1. The time now is 12:27 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"