+ Reply to Thread
Results 1 to 3 of 3

problem vba code for multiselect listbox (userform)

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-07-2015
    Location
    cyprus
    MS-Off Ver
    Microsoft 365
    Posts
    183

    problem vba code for multiselect listbox (userform)

    in the attached file, I drafted a userform "PANDL" (which is actually the copy of the page 2 of the userform "cargo2016").

    In this userform I would have like to be able to select several ref and to populate the userform with infos for each of them..

    But my knowledge in VBA is null and I don't know how to say for each selected item in the listbox "REF", get datas.....

    I tried to put a "ok" command to validate the selection in REF listbox with following code...

    Private Sub cmdOkay_Click()
    Dim i As Long, msg As String, Check As String
    
    'Generate a list of the selected items
    
        For i = 0 To Me.REF.ListCount - 1
            If Me.REF.Selected(i) Then
            GetData
            End If
        Next i
    
    
    If msg = vbNullString Then
        'If nothing was selected, tell user and let them try again
        MsgBox "Nothing was selected!  Please make a selection!"
        Exit Sub
    Else
        'Ask the user if they are happy with their selection(s)
        Check = MsgBox("You selected:" & vbNewLine & msg & vbNewLine & _
            "Are you happy with your selections?", _
            vbYesNo + vbInformation, "Please confirm")
    End If
    
    If Check = vbYes Then
        GetData
    Else
        'User wants to try again, so clear listbox selections and
        'return user to the userform
        For i = 0 To REF.ListCount - 1
            REF.Selected(i) = False
        Next
    End If
         
    End Sub
    but obviously the ref.value remain "null".....

    can someone give me some tips to search further?
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: problem vba code for multiselect listbox (userform)

    Quote Originally Posted by mariec_06 View Post
    In this userform I would have like to be able to select several ref and to populate the userform with infos for each of them..
    The PANDL form can only display the info for one REF at a time. Why have a multi-select list?


    I don't know how to say for each selected item in the listbox "REF", get datas.....

    but obviously the ref.value remain "null".....

    can someone give me some tips to search further?

    Use the index number i to get the selected RefNumber from the list and pass it to the GetData procedure.

        For i = 0 To Me.REF.ListCount - 1
            If Me.REF.Selected(i) Then
            GetData Me.REF.List(i)
            End If
        Next i

    Then use the passed RefNumber to search

    Sub GetData(RefNumber As String)
    
    Dim irow As Long
    Dim irow1 As Long
    Dim irow2 As Long
    Dim IROW3 As Long
    Dim IROW4 As Long
    
    Dim WSH As Worksheet
    Dim WSH1 As Worksheet
    Dim wsh2 As Worksheet
    Dim WSH3 As Worksheet
    Dim wsh4 As Worksheet
    
    
    Set WSH = ThisWorkbook.Worksheets("RECAP")
    Set WSH1 = ThisWorkbook.Worksheets("PRICING FINAL")
    Set wsh2 = ThisWorkbook.Worksheets("PANDL")
    Set WSH3 = ThisWorkbook.Worksheets("DEM CALC")
    Set wsh4 = ThisWorkbook.Worksheets("pricing provisional")
    
    irow = WSH.Cells.Find(What:=RefNumber, LookIn:=xlValues, SearchOrder:=xlRows, SearchDirection:=xlPrevious).row
    irow1 = WSH1.Cells.Find(What:=RefNumber, LookIn:=xlValues, SearchOrder:=xlRows, SearchDirection:=xlPrevious).row
    irow2 = wsh2.Cells.Find(What:=RefNumber, LookIn:=xlValues, SearchOrder:=xlRows, SearchDirection:=xlPrevious).row
    IROW3 = WSH3.Cells.Find(What:=RefNumber, LookIn:=xlValues, SearchOrder:=xlRows, SearchDirection:=xlPrevious).row
    IROW4 = wsh4.Cells.Find(What:=RefNumber, LookIn:=xlValues, SearchOrder:=xlRows, SearchDirection:=xlPrevious).row
    There are many other problems with the GetData code, but this is how to pass each selected RefNumber in the Ref.List to GetData.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    10-07-2015
    Location
    cyprus
    MS-Off Ver
    Microsoft 365
    Posts
    183

    Re: problem vba code for multiselect listbox (userform)

    The userform "cargo2016" works perfectly for one ref, but I would like for example to print all the pandl forms of all the ref of January for ex or for all the ref from 2016CT003 to 2016CT006 that's why I was thinking multiselect will be appropriate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. UserForm: Enable Textbox When "Other" Selected in MultiSelect Listbox (Excel 2010)
    By excelforumkeys in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-02-2014, 06:40 PM
  2. Listbox multiselect- VBA code error
    By sassy1978 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-08-2013, 07:22 PM
  3. [SOLVED] MultiSelect Listbox in Userform to copy data to worksheet
    By aarodn in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-06-2013, 03:52 PM
  4. Copy Selected items from multicolumn, multiselect listbox to another listbox
    By Willigb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-19-2013, 11:27 AM
  5. [SOLVED] listindex=0 when first item selected in a multiselect listbox on a userform?
    By mcdermott2 in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 04-17-2013, 12:38 PM
  6. Unable to Detect When UserForm MultiSelect ListBox Selection Changes
    By ShortSword in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-07-2012, 10:49 PM
  7. How to adapt this listbox code when multiselect is enabled?
    By Kimberley in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 12-02-2011, 02:00 PM
  8. userform multiselect listbox problem
    By apndas in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-24-2006, 11:12 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1