Must Be Missing Something Obvious About ListBoxes

  • Thread starter Johnny Meredith
  • Start date
J

Johnny Meredith

I have a four-column list box with the following code in the change
event (not all columns are used in this example):
..
..
With lstbox
Range("Name1").Value = .Columns(1)
Range("Name2").Value = .Columns(0)
Range("Name3").Value = .Columns(2)
end with
..
..

If I comment out the last two range lines, it works. If I don't, a
very strange thing happens. If I place a breakpoint on the first line,
the executions breaks here as normal; but when I hit play it breaks on
this line again, and, on the third play, throws exception #381.
Meanwhile, I notice that the lstbox reference has become null.

If I dim a module-level interger as a counter (adding one to it at the
top of the event handler), it appears that the code is being called 3
times.

Am I using the Columns property incorrectly? Why is this code
executing three times?

Thanks,
Johnny
 
G

Guest

Your events are recursive. When you assign the value to the cell a change is
initiated and the event fires again. You need to change the application
settings as follows (it is safest to use an error handler here)

on error goto errorhandler
application.enableevents = false
....
With lstbox
Range("Name1").Value = .Columns(1)
Range("Name2").Value = .Columns(0)
Range("Name3").Value = .Columns(2)
end with
....
ErrorHandler:
application.enableevents = true
End sub
 
C

Carl

It seems that you are looking at the properties of the Listbox but trying to
assign values to a range at the same time. I am not sure that flies.
 

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