Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

vba multi-select list box

Posted on 2014-04-11
4
Medium Priority
?
784 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 36

Accepted Solution

by:
Kimputer earned 2000 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 36

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

610 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