Solved

vba multi-select list box

Posted on 2014-04-11
4
758 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

821 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