LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Naming a listbox as a string

And if these are listboxes from the Control toolbox toolbar that are placed on
the worksheet, you can use something like:

Dim OLEObj As OLEObject
Dim iCtr As Long
Dim lCtr As Long

For iCtr = 1 To 12 'how many listboxes?
Set OLEObj = Me.OLEObjects("Listbox" & iCtr)
With OLEObj.Object
For lCtr = 1 To .ListCount - 1
If .Selected(lCtr) Then
'do the work
End If
Next lCtr
End With
Next iCtr

I used the Me keyword. It refers to the object that owns the code. In this
case, I guessed that your code was in the worksheet module, so Me refers to that
worksheet.



wrote:

I need to label a listbox as a string (or whatever works) so I can
access the properties in a loop. Hard to explain but here's basically
what I need done:

Original Code:
Private Sub ListBox1_Change()
ActiveSheet.Range("B2:Z2").Clear
Dim i As Integer, li As Integer
For li = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(li) Then
ActiveSheet.Range("B2").Offset(i, 0) = ListBox1.List(li)
i = i + 1
End If
Next
End Sub

Desired Code:
Private Sub ListBox1_Change()
ActiveSheet.Range("B2:Z2").Clear
Dim i As Integer, li As Integer, listboxname As String
For li = 0 To listboxname.ListCount - 1
If listboxname.Selected(li) Then
ActiveSheet.Range("B2").Offset(i, 0) = listboxname.List(li)
i = i + 1
End If
Next
End Sub

I have a series of list boxes allowing multiple selections that all
have different data. I would like the output of all list boxes to
show across horizontal cells (all items from ListBox1 in their own
cell in row 1, ListBox2 in row 2, etc.). The original code works
fine, but I would like to eventually write a loop such that I can call
the sub procedure and have the lists update the horizontal rows
automatically without cluttering up my VB code and requiring a lot of
repetition.


--

Dave Peterson
 
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 can i no margin string in listbox from left tom taol Excel Programming 0 April 23rd 08 05:58 AM
userform listbox cannot get listbox.value to transfer back to main sub [email protected] Excel Programming 1 May 17th 06 09:44 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
Is refreshing listbox rowsource in listbox click event possible? Jeremy Gollehon[_2_] Excel Programming 4 September 25th 03 06:45 PM


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