+ Reply to Thread
Results 1 to 8 of 8

combobox selection adding new row to worksheet

  1. #1
    Registered User
    Join Date
    09-25-2006
    Posts
    12

    combobox selection adding new row to worksheet

    I've been working on this problem and am stumped. When I click on an item in the userform combobox all the data is transferred to the next empty row instead of the existing item row on the worksheet and I end up with duplicates. What I would like to accomplish is if the item already exists in the combobox then add the userform data to the existing row, otherwise if the item does not exist I would like the item and data to be transferred to the next empty row.

  2. #2
    Valued Forum Contributor
    Join Date
    08-26-2006
    Location
    -
    MS-Off Ver
    2010
    Posts
    388
    You need to search for the item on the sheet and if it exists add data to the row where found, otherwise transfer data to the next empty row.
    The Find method is useful for searching a sheet.

    Below is a function FindIt which searches for a string on a worksheet and returns the row number where found. Then it's just a matter of adding your code for adding to itemRow or transferring to the next empty row.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    09-25-2006
    Posts
    12

    combobox selection adding new row to worksheet

    Thanks for the response T-J. I've tried entering your code but am now receiving an error so I've attached my original code. The name of my combobox is txtBatch1 and my named range for the combobox is "Batch" which is located on sheet 2.


    Private Sub CommandButton1_Click()
    Dim iRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("Data")
    txtBatch1.SetFocus
    'find first empty row in database
    iRow = ws.Cells(Rows.Count, 1) _
    .End(xlUp).Offset(1, 0).Row

    'check for a part number
    If Trim(Me.txtBatch1.Value) = "" Then
    Me.txtBatch1.SetFocus
    MsgBox "Please enter a batch number"
    Exit Sub
    End If

    'copy the data to the database
    ws.Cells(iRow, 1).Value = Me.txtBatch1.Value
    ws.Cells(iRow, 2).Value = Me.txtDate1.Value
    ws.Cells(iRow, 3).Value = Me.txtCust1.Value
    ws.Cells(iRow, 4).Value = Me.txtBoard1.Value
    ws.Cells(iRow, 5).Value = Me.txtSerial1.Value
    ws.Cells(iRow, 6).Value = Me.txtQty1.Value
    ws.Cells(iRow, 18).Value = Me.txtStatus1.Value
    ws.Cells(iRow, 17).Value = Me.txtNotes.Value

    'clear the data
    Me.txtBatch1.Value = ""
    Me.txtDate1.Value = ""
    Me.txtCust1.Value = ""
    Me.txtBoard1.Value = ""
    Me.txtSerial1.Value = ""
    Me.txtQty1.Value = ""
    Me.txtStatus1.Value = ""
    Me.txtNotes.Value = ""
    Me.txtBatch1.SetFocus

    End Sub

  4. #4
    Valued Forum Contributor
    Join Date
    08-26-2006
    Location
    -
    MS-Off Ver
    2010
    Posts
    388
    Probably a misunderstanding on my part. Comment out the Private Sub ComboBox1_Click() above and in your Private Sub CommandButton1_Click() code replace this part:

    Please Login or Register  to view this content.
    Last edited by T-J; 01-16-2007 at 07:15 AM.

  5. #5
    Registered User
    Join Date
    09-25-2006
    Posts
    12

    combobox selection adding new row to worksheet

    'FindIt' produces an error

  6. #6
    Valued Forum Contributor
    Join Date
    08-26-2006
    Location
    -
    MS-Off Ver
    2010
    Posts
    388
    It works ok for me. What is the error and line?

  7. #7
    Registered User
    Join Date
    09-25-2006
    Posts
    12

    Combobox

    'FindIt' is highlighted with the following error: Compile error: Sub or Function not defined.

  8. #8
    Valued Forum Contributor
    Join Date
    08-26-2006
    Location
    -
    MS-Off Ver
    2010
    Posts
    388
    It must be the line itemRow = FindIt(txtBatch1.Value) in CommandButton1_Click()
    If you click Help in the error message you get

    A Sub, Function, or Property procedure must be defined to be called. This error has the following causes and solutions:
    The specified procedure isn't visible to the calling procedure.


    Place the FindIt code in the userform code or in a code module and it should work.

+ 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