LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default User Forms and List Boxes

If you have a rowsource set, you shouldn't use additem. If you want to use
AddItem, then don't have a rowsource.

The code you show for
LoadLB4

is a site localization for code from John Walkenbach's site and I have used
it/recommended it many times. So I know it works

However, if the listbox has a rowsource property with an assignment, then I
get a permission denied error.

--
Regards,
Tom Ogilvy

"MikeM" wrote in message
...
Sorry, I'm attaching the 2 codes that I have. I was under the assumption,
(
I know...), that since ListBox4 had a RowSource reference in it to a
Range,
which popuates the LB with the Names, it would understand what I whish to
do?
Quess not. I copied the First Code as written into my WB which is
basically
a copy of the WB that my collegue wrote the code and it works. I'm also
sending the code that I believe you are talking about that will load the
names into ListBox4? Its named: " Sub LoadLB4() "

Private Sub ListBox4_Click()
'***THIS IS THE Segment Selection Screen
'sets the number code corresponding to the selected segment.
'this opens the appropriate 'seg(n)' tab

Dim rng As Range
Set rng = Sheets("1. Segment Prioritization").Range("v1")
Counter = 0
For i = 0 To ListBox4.ListCount - 1
If ListBox4.Selected(i) = True Then
Counter = Counter + 1
rng(Counter).Value = ListBox4.List(i)
End If
Next
Unload Me

Sheets("1. Segment Prioritization").Select
Range("w1").Select

If ActiveCell = "1" Then Sheets("Seg (1)").Select Range("E23").Select
If ActiveCell = "2" Then Sheets("Seg (2)").Select Range("E23").Select
If ActiveCell = "3" Then Sheets("Seg (3)").Select Range("E23").Select
If ActiveCell = "4" Then Sheets("Seg (4)").Select Range("E23").Select
If ActiveCell = "5" Then Sheets("Seg (5)").Select Range("E23").Select
If ActiveCell = "6" Then Sheets("Seg (6)").Select Range("E23").Select

End Sub

Sub LoadLB4()
'****LOADS Segment names into ListBox 4
Dim NoDupes As New Collection
Dim i As Integer, j As Integer
Dim Swap1, Swap2, Item

' The segments are in A301:A358
Sheets("1. Segment Prioritization").Select
Set AllCells = Range("v11:v25")

' The next statement ignores the error caused
' by attempting to add a duplicate key to the collection.
' The duplicate is not added - which is just what we want!
On Error Resume Next
For Each Cell In AllCells
NoDupes.Add Cell.Value, CStr(Cell.Value)
' Note: the 2nd argument (key) for the Add method must be a string
Next Cell

' Resume normal error handling
On Error GoTo 0

' Sort the collection (optional)
For i = 1 To NoDupes.Count - 1
For j = i + 1 To NoDupes.Count
If NoDupes(i) NoDupes(j) Then
Swap1 = NoDupes(i)
Swap2 = NoDupes(j)
NoDupes.Add Swap1, befo=j
NoDupes.Add Swap2, befo=i
NoDupes.Remove i + 1
NoDupes.Remove j + 1
End If
Next j
Next i

' Add the sorted, non-duplicated items to a ListBox
For Each Item In NoDupes
MENU.ListBox4.AddItem Item
Next Item

'set focus to listbox2
'Menu.ListBox4.SetFocus


--
MJM


"moon" wrote:


"MikeM" schreef in bericht
...
I have a User Form and inside is a ListBox that shows a list of
Different
Worksheets within the Workbook. I asked a collegue for help to write
the
Code that when you click on the WorkSheet name in the workbook, it
would
open
that workbook.

I copied his code into the ListBox from his workbook to mine into the
Same
ListBox name. It does not work in mine but works fine in his!

I checked to make sure the Properties are similar between the 2 which
they
are. What am I missing?
--
MJM



Better paste some code snippets next time, else how can we know what
you're
missing?
Two subs below, one fills the listbox, the other one handles the
onclick-event.



'populate listbox with sheets
'when opening the form...
Private Sub UserForm_Activate()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
For Each ws In wb.Sheets
ListBox1.AddItem ws.Name
Next
Set ws = Nothing
Set wb = Nothing
End Sub

'after clicking the listbox
'activate selected sheet and
'close the form
Private Sub ListBox1_Click()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Sheets(ListBox1.Text)
ws.Activate
Unload Me
Set ws = Nothing
Set wb = Nothing
End Sub





 
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
text boxes on forms steve Excel Programming 1 September 12th 06 02:55 PM
User Forms - Combo Boxes Michael Excel Dude Excel Discussion (Misc queries) 2 September 3rd 06 10:41 PM
Boxes in Forms PaulW Excel Discussion (Misc queries) 0 March 10th 06 03:18 PM
forms / text boxes JT Excel Programming 1 January 23rd 06 08:07 PM
List Boxes New User LostNFound New Users to Excel 1 January 19th 06 01:18 AM


All times are GMT +1. The time now is 06:44 PM.

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"