Unanswered: VBA Function to Count ListBox Items Selected
I am trying to write a function to count items selected in a list box on a form, and pass the form name (if necessary) and list box name to the function. This function will allow me to get the counts for multiple list boxes in my Excel 2003 application. Without this function, I will need to code for each list box where a selected-item-count is done. Not a big deal with only two list boxes--I'm just trying to see if a single function can do it all.
Here is my code that does not work.
Function Selected_Count(frmName As String, lstBoxName As String) As Integer
Dim sCount As Integer, i As Integer
Dim ctl As Control
Set ctl = frmName.Controls(lstBoxName)
For i = 0 To .ListCount - 1
If .Selected(intIndex) Then sCount = sCount + 1
Selected_Count = sCount
Last edited by JerryDal; 09-08-11 at 17:36.
Here is the solution I worked out to pass a control name to a function in Excel 2003 VBA. A demo of this is attached.
Function Any_Selected(ListBoxName) As String
'loops through a list box and returns # of items selected
Dim i As Integer, selCount As Integer
For i = 0 To Controls(ListBoxName).ListCount - 1
If Controls(ListBoxName).Selected(i) = True Then selCount = selCount + 1
Any_Selected = CStr(selCount)