+ Reply to Thread
Results 1 to 7 of 7

Send list box value to first unused cell in column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-07-2006
    Location
    San Jose, CA
    MS-Off Ver
    2007, 2016
    Posts
    115

    Send list box value to first unused cell in column

    I want to send the current ListBox value to a list in column U via a Command Button click. The following code works somewhat except that it starts the list after the used cells in the "middle" of column U, rows 36 to 3000 instead at row 2, the first unused row.

    Can anyone suggest how to modify the code place list box values starting in the first unused cell in column?

    
    Private Sub CommandButton1_Click()
    Dim lngLastRow As Long
    Dim lngCol As Long
    Dim lngIndex As Long
    
    lngLastRow = Range("U" & Rows.Count).End(xlUp).Row + 1
    lngCol = 21
    Cells(lngLastRow, lngCol) = ListBox1.Value
    
    End Sub
    Part II if I am allowed to request at the same time, is to simultaneously place the value in cell P32 in a list in column V?

    Can the above code be modified to place both the current list box value in the first unused cell in a list in column U, while at the same time placing the current value in cell P32 in a list starting at first unused cell in a list in column V.

    Any help is greatly appreciated.

    Thanks,

    Art

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,949
    I tested the posted code; it works as expected. So I'm not sure what you mean by "first unused cell". If you have a column with some groups of used cells with blank cells in between, then that code won't work; you'll have to check each cell in the entire column for blanks. It would help if you posted a copy of your workbook.
    Ben Van Johnson

  3. #3
    Forum Contributor
    Join Date
    01-07-2006
    Location
    San Jose, CA
    MS-Off Ver
    2007, 2016
    Posts
    115
    Ben,

    Thanks for your response. What is happening is that the listbox value is placed in cell U3001 starting the list at that cell instead of cell U2, under the column heading in U1. U2 is the first unused cell in column U. Cells U2:U35 are blank while there numerical values in cells U36:U3000.

    Can the code that I posted be modified to place the listbox list value to begin in cell U2?

    Thanks,

    Art

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,949
    Try the following test code:

    Option Explicit
    Sub test()
         Dim ListData As Range, C As Range
         Dim LastV As Long
         
         Set ListData = Sheets("sheet1").Range("u2:u35")
         
         'find the last row in column V
         LastV = Sheets("Sheet1").Cells(Rows.Count, "V").End(xlUp).Row
         
         'loop through cells R2 - R35 to find the first blank and copy listbox value
         For Each C In ListData
              If C = "" And C.Row < 36 Then
                   C.Value = ListBox1.Text
                   
                   'copy P32 to next cell in col V
                   Range("V" & LastV).Value = Range("P32").Value
                   Exit Sub
              End If
         Next C
    End Sub
    Last edited by protonLeah; 08-18-2008 at 12:03 AM.

  5. #5
    Forum Contributor
    Join Date
    01-07-2006
    Location
    San Jose, CA
    MS-Off Ver
    2007, 2016
    Posts
    115
    I tried putting your code in as follows:

    
    Private Sub CommandButton1_Click()
         Dim ListData As Range, C As Range
         Dim LastV As Long
         
         Set ListData = Sheets("Stock Data").Range("u2:u35")
         
         'find the last row in column V
         LastV = Sheets("Sheet1").Cells(Rows.Count, "V").End(xlUp).Row
         
         'loop through cells R2 - R35 to find the first blank and copy listbox value
         For Each C In ListData
              If C = "" And C.Row < 36 Then
                   C.Value = ListBoxStocks.Text
                   
                   'copy P32 to next cell in col V
                   Range("V" & LastV).Value = Range("P32").Value
                   Exit Sub
              End If
         Next C
    When I execute the code, I receive an error 9, "subscript out of range" message. Am I doing something wrong?

    Any suggestions?

    Art

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Try

    Option Explicit
    
    Private Sub CommandButton1_Click()
        Dim lngLastRow As Long
        Dim lngCol As Long
        Dim lngIndex As Long
        lngCol = 21
        lngLastRow = Range(35, lngCol).End(xlUp).Row + 1
        If lngLastRow < 35 Then
            Cells(lngLastRow, lngCol) = ListBox1.Value
        Else: MsgBox "No empty row available", vbCritical, "Error"
        End If
    End Sub
    Last edited by royUK; 08-18-2008 at 05:02 AM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  7. #7
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,949
    What are your sheet names? You may need to replace the 'sheet1' in:
    'find the last row in column V
    LastV = Sheets("Sheet1").Cells(Rows.Count, "V").End(xlUp).Row
    with the actual sheet/tab name.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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