Solved

vba multi-select list box

Posted on 2014-04-11
4
746 Views
Last Modified: 2014-04-15
Hi EE,

I have a problem with a form that contains a list box and some option boxes.

In my code, I am trying to compare two values:

Column C in my worksheet
Column(0) of my listbox

When the form opens, I select one or multiple rows in my listbox. Then, I select a code to apply. After doing so, I click the process button and for each of the rows selected in my listbox, I need to update the respective rows in my worksheet (column I) with the code that was selected in my form.

In the code I have, it works for one, but not for any more and I suspect it is an issue with the way I have written it.

Private Sub cmdProcess_Click()
    
Dim wkb As String, i As Integer, response As Integer, msg As String, title As String, style As String, _
    intcounter As Integer, oCtrl As Control, k As String

'check for list box selection
If Me.lstTrans = vbNullString Then
    'if nothing was selected, tell user and let them try again ->>
     Exit Sub
Else
    'only controls
    For Each oCtrl In frm_ledger_coding.fra1.Controls
        'only option buttons
        If TypeName(oCtrl) = "OptionButton" Then
            'which one is checked?
            If oCtrl.Value = True Then
                'what's the tag?
                k = oCtrl.Tag
                Exit For
            End If
        End If
     Next
    'specifying cunter to start from row 2
    intcounter = 2
    If k = "" Then Exit Sub Else
    'specifying list selection counts
        For i = 0 To lstTrans.ListCount - 1
            If lstTrans.Selected(i) Then
                With ActiveWorkbook.Worksheets("Import")
                    'Range H being the amount which always have a value
                    While .Range("H" & intcounter).Value <> ""
                        'compare the key in column c with the list value in column 0 (bound column)
                        If .Range("C" & intcounter).Value = lstTrans.List(i) Then
                            Range("I" & intcounter).Value = k
                        End If
                    intcounter = intcounter + 1
                    Wend
                End With
            End If
        Next i
End If

End Sub

Open in new window


I have enclosed a working file with test data to help.

Can someone help me get this working?

TA
Test.xlsm
0
Comment
Question by:discogs
  • 2
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Kimputer earned 500 total points
ID: 39995448
Me again :)

Private Sub cmdProcess_Click()
    
Dim wkb As String, i As Integer, response As Integer, msg As String, title As String, style As String, _
    intcounter As Integer, oCtrl As Control, k As String

'check for list box selection
If Me.lstTrans = vbNullString Then
    'if nothing was selected, tell user and let them try again ->>
     Exit Sub
Else
    'only controls
    For Each oCtrl In frm_ledger_coding.fra1.Controls
        'only option buttons
        If TypeName(oCtrl) = "OptionButton" Then
            'which one is checked?
            If oCtrl.Value = True Then
                'what's the tag?
                k = oCtrl.Tag
                Exit For
            End If
        End If
     Next
    'specifying cunter to start from row 2
    intcounter = 2
    If k = "" Then Exit Sub Else
    'specifying list selection counts
        For i = 0 To lstTrans.ListCount - 1 Step 1
            If lstTrans.Selected(i) Then
                intcounter = 1
                With ActiveWorkbook.Worksheets("Import")
                    'Range H being the amount which always have a value
                    While .Range("H" & intcounter).Value <> ""
                        'compare the key in column c with the list value in column 0 (bound column)
                        If .Range("C" & intcounter).Value = lstTrans.List(i) Then
                            Range("I" & intcounter).Value = k
                        End If
                    intcounter = intcounter + 1
                    Wend
                End With
            End If
        Next i
End If

End Sub

Open in new window

0
 

Author Comment

by:discogs
ID: 39995523
Hi there,

Thanks for sorting this. I thought I had it sorted but was badly mistaken.

Just for clarity, in line 24, we specify intcounter as 2, then in line 29 we do it again. Can you explain why it is like this?
0
 
LVL 35

Expert Comment

by:Kimputer
ID: 39995892
It's the main reason your first script LOOPED properly, but returned only one result. Because by the end of line 37 (your old code), the while loop starting at 31 will ALWAYS return nothing and not go through the while loop. The intcounter is stuck at 80, which is the last line of your whole sheet having an empty cell. If you want to loop something inside another loop, you have to reset the conditions just before you enter the loop.
0
 

Author Comment

by:discogs
ID: 40002838
Hi. Thanks for outlining that. I was wondering why that was. Now I understand because it is a loop inside a loop which makes absolute sense. Cheers, :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now