Solved

vba multi-select list box

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

689 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