+ Reply to Thread
Results 1 to 3 of 3

Listbox selection relative to last row

Hybrid View

  1. #1
    Registered User
    Join Date
    04-24-2013
    Location
    Bristol
    MS-Off Ver
    Excel 2010
    Posts
    32

    Listbox selection relative to last row

    Hi there

    I'm trying to post values from a multiselect listbox into columns 17 to 19, whilst also ensuring they are posted to the last row.

    The bit in red I just cant work out

    Any help appreciated

    Thanks


    Private Sub CmdCrea_Click()
    Dim i As Integer, j As Integer
    Dim ws As Worksheet
    Dim lItem As Long
    Set ws = Worksheets("Data")
    ws.Activate
    With ws
    'position cursor in the correct cell A2.
    Range("A2").Select
    i = 1#
    'check to see the next available blank row start at cell A2...
    Do Until ActiveCell.Value = Empty
     ActiveCell.Offset(1, 0).Select 'move down 1 row
     i = i + 1 'keep a count of the ID for later use
    Loop
    
    'Populate the new data values into the worksheet.
    ActiveCell.Value = i  'Next ID number
    ActiveCell.Offset(0, 1).Value = Me.TxtURN.Text
    ActiveCell.Offset(0, 2).Value = Me.TxtScop.Text
    ActiveCell.Offset(0, 3).Value = Me.TxtArea.Text
    ActiveCell.Offset(0, 4).Value = Me.TxtStra.Text
    ActiveCell.Offset(0, 5).Value = Me.TxtRequ.Text
    ActiveCell.Offset(0, 6).Value = Me.TxtRefi.Text
    ActiveCell.Offset(0, 7).Value = Format(Now(), "MM/DD/YYYY")
    
    For lItem = 0 To LstSyst.ListCount - 1
    
    If Me.LstSyst.Selected(lItem) = True Then
    
    Worksheets("Data Refined").Range("A:A").End(xlUp)(2, 17) = LstSyst.List(lItem)
    Worksheets("Data Refined").Range("A:A").End(xlUp)(2, 18) = LstSyst.List(lItem)
    Worksheets("Data Refined").Range("A:A").End(xlUp)(2, 19) = LstSyst.List(lItem)
    
    Me.LstSyst.Selected(lItem) = False
    
    End If
    
    
    Next

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Listbox selection relative to last row

    Hi Biffer,

    I hope I interpreted your requirements correctly:
    a. Find the Last Row in Sheet 'Data Refined'
    b. Use the row after the Last Row
    c. Put up to 3 items from the ListBox in the row after the Last Row starting at column 17 (= Q).

    Please let me know if this is not what you want.

    Try the following code. I removed the items that you weren't having problems with for ease of debugging. It is easier for me to debug when I add extra variables such as bItemBeenSelected.

    Private Sub CmdCrea_Click()
    
      Dim ws As Worksheet
      Dim i As Integer
      Dim iCount As Long
      Dim iColumn As Long
      Dim iLastRow As Long
      Dim iRow As Long
      Dim j As Integer
      
      Dim lItem As Long
    
      Dim bItemBeenSelected As Boolean
      
      'Get the Last Row Used in Sheet 'Data Refined'
      'If there is no data, do not write in a header row
      'NOTE: A runtime error will occur if the sheet is empty
      iLastRow = Worksheets("Data Refined").Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
      If iLastRow < 2 Then
        iLastRow = 2
      End If
      
      iCount = 0
      iColumn = 16             'Initialize the Column to ONE before the first column
      iRow = iLastRow + 1      'Set the row number to the row after the last row used
    
      'Traverse the 'ListBox'
      'Put up to 3 values in the row after the last row starting at Column '17' (17 = Q)
      For lItem = 0 To LstSyst.ListCount - 1
    
        'Get the True/False Value for the Current Item Being Selected
        bItemBeenSelected = Me.LstSyst.Selected(lItem)
        
        If bItemBeenSelected = True Then
        
          'Increment the counter
          'Increment the column number
          iCount = iCount + 1
          iColumn = iColumn + 1
    
         'Put the data on Sheet 'Data Refined'
          Worksheets("Data Refined").Cells(iRow, iColumn) = LstSyst.List(lItem)
    
          'Exit if 3 columns have been filled
          If iCount >= 3 Then     '>= is a defensive programming techinque
            Exit For
          End If
          
        End If
    
      Next lItem
    
    End Sub
    The following may help you now and in the future:
    Debugger Secrets:
    a. Press 'F8' to single step (goes into subroutines and functions).
    b. Press SHIFT 'F8' to single step OVER subroutines and functions.
    c. Press CTRL 'F8' to stop at the line where the cursor is.
    d. 'Left Click' the margin to the left of a line to set (or clear) a BREAKPOINT.
    e. Press CTRL 'G' to open the IMMEDIATE WINDOW. 'debug.print' statements send their
    output to the IMMEDIATE WINDOW.
    f. Select View > Locals to see all variables while debugging.
    g. To automatically set a BREAKPOINT at a certain location put in the line:
    'Debug.Assert False'
    h. To conditionally set a BREAKPOINT at a certain location put in lines similar to:
    if i >= 20 and xTV20 > 99.56 then
    Debug.Assert False
    endif
    i. A variable value will be displayed by putting the cursor over the variable name.

    Lewis

  3. #3
    Registered User
    Join Date
    04-24-2013
    Location
    Bristol
    MS-Off Ver
    Excel 2010
    Posts
    32

    Re: Listbox selection relative to last row

    Apologies for the late response...this works great, thanks LJ

+ 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. [SOLVED] Listbox Selection/Highlight is lost after changing Listbox BackColor
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-02-2014, 10:43 PM
  2. [SOLVED] Indexing proper range in listbox depending on selection in previous listbox
    By bloodmeat in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-02-2013, 02:31 PM
  3. Replies: 12
    Last Post: 08-28-2012, 07:09 AM
  4. Multi select Listbox Items selection based on other Listbox item selection.
    By srinivassathi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-20-2011, 05:53 AM
  5. Replies: 0
    Last Post: 11-30-2005, 12:50 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