+ Reply to Thread
Results 1 to 3 of 3

Number of Selected Items in Listbox Determine Size of Table

Hybrid View

Dferrier Number of Selected Items in... 05-16-2016, 07:08 AM
humdingaling Re: Number of Selected Items... 05-16-2016, 10:44 PM
Dferrier Re: Number of Selected Items... 05-17-2016, 03:56 AM
  1. #1
    Registered User
    Join Date
    04-07-2015
    Location
    Burnley, UK
    MS-Off Ver
    2013
    Posts
    19

    Number of Selected Items in Listbox Determine Size of Table

    Hello All Out There,

    I have found myself at the all too familiar point of getting to last part of a VBA project and getting an issue that just wont go away.

    What I want to do is have the user select some items in a List Box and then the code re-sizes a table to reflect the number selected. So that a nice chart can be created showing that data.

    This is the offending code:

    Dim i3 As Integer
    Dim iD As Integer
    
    i3 = Worksheets("Data").Range("K40").Row
    
                'iD is not picking up number of selected items, so iD = 7
    
    Dim iL As Integer
    Dim selCount As Integer
    Dim iLC As Integer
    
    iLC = Worksheets("Data").Range("O41").Row
    
       Do Until Worksheets("Data").Range("$O$" + iLC).Value = "" Or iLC = 200 'Error 13 Type mismatch error
       iLC = iLC + 1
       Loop
        
     iD = iLC - 1
        
    MsgBox (iD) ' A temporary check to see if its picking up the right number
    
                'Error 1004: 'Range' of Object '_Global' Failed
    
    Worksheets("Data").ListObjects("Table27").Resize Range("$O$40:$P$" & (i3 + iD))
    I have searched left, right and centre, and high and low for a solution, but the ones I've tried just haven't quite done it.

    The deadline for when this system is to be launched is looming.

    See attached for the workbook if it helps.

    Any Help will be greatly thanked.

    Regards
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Number of Selected Items in Listbox Determine Size of Table

    try picking up the ID earlier in when you are checking the listbox

    If bSelected = True Then
            With Worksheets("DATA").Range("Table27") 'Transfer to range
                '.Cells.Clear 'Clear transfer range
                For lItem = 0 To lRows
                    If XListBox.selected(lItem) = True Then 'Row selected
                      'Increment variable for row transfer range
                      lTransferRow = lTransferRow + 1
                      iD = iD + 1
                        'Loop through columns of selected row
                        For lColLoop = 0 To lCols
                           'Transfer selected row to relevant row of transfer range
                           .Cells(lTransferRow, lColLoop + 1) = XListBox.List(lItem, lColLoop)
                            'Uncheck selected row
                            XListBox.selected(lItem) = False
                        Next lColLoop
                    End If
                Next
            End With
             Unload Me
        Else ' NO listbox row chosen
             GoTo ErrorHandler
        End If
    then use the full range

    Worksheets("Data").ListObjects("Table27").Resize Worksheets("data").Range("$O$40:$P$" & (i3 + iD))
    ps remember to declare ID at the start before it is required
    Last edited by humdingaling; 05-16-2016 at 10:49 PM. Reason: Bold area of interest
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Registered User
    Join Date
    04-07-2015
    Location
    Burnley, UK
    MS-Off Ver
    2013
    Posts
    19

    Re: Number of Selected Items in Listbox Determine Size of Table

    Hello Humdingaling,

    Your solution worked beautifully. Thank you.

    Take some rep.

    Regards

+ 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. VBA - Store selected items in listbox
    By Nisseluren in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-08-2015, 05:57 PM
  2. [SOLVED] Add Selected Items From One ListBox to Another ListBox on UserForm
    By EnigmaMatter in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-27-2014, 06:53 PM
  3. 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
  4. [SOLVED] Listbox size shrinks when items are loaded
    By efernandes67 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-30-2013, 10:37 AM
  5. Transfer random listbox items to new listbox and then loop through selected items
    By narrowgate88 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-31-2013, 05:58 PM
  6. ComboBox to determine selected items
    By qiqinuinaifen128 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-02-2009, 06:27 AM
  7. [SOLVED] Determine size of listbox
    By Jasper in forum Excel General
    Replies: 3
    Last Post: 04-18-2006, 05:40 PM
  8. [SOLVED] How to use selected items from Listbox
    By gerard.kompier@shell.com in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-07-2006, 11:10 AM

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