Solved

vba multi-select list box

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

706 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

16 Experts available now in Live!

Get 1:1 Help Now