+ Reply to Thread
Results 1 to 4 of 4

Combobox Selection To Insert Data

Hybrid View

Mooseman60 Combobox Selection To Insert... 12-04-2010, 06:15 AM
JBeaucaire Re: Combobox Selection To... 12-04-2010, 10:06 AM
Mooseman60 Re: Combobox Selection To... 12-04-2010, 10:46 AM
JBeaucaire Re: Combobox Selection To... 12-04-2010, 12:38 PM
  1. #1
    Forum Contributor
    Join Date
    09-26-2010
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    182

    Exclamation Combobox Selection To Insert Data

    Hi

    I am having a mental blank I can't remember the code to do the following

    I want to be able to add items to worksheets based on the worksheet selection in a Combobox

    I dont want the Category name selected in the Combobox to be saved to the worksheet it is only used to select which worksheet the other data is to be saved to.

    Any help would be appreciated

    Private Sub cmdAdd_Click()
    Dim iRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets(",")
    
    'find first empty row in database
    iRow = ws.Cells(Rows.Count, 1) _
      .End(xlUp).Offset(1, 0).Row
      
    'check for a Category
    If Trim(Me.cboCategory.Value) = "" Then
      Me.cboCategory.SetFocus
      MsgBox "Please Select a Category"
      Exit Sub
    End If
    
    'check for a Product
    If Trim(Me.txtProduct.Value) = "" Then
      Me.cboCategory.SetFocus
      MsgBox "Please Enter a Product Description"
      Exit Sub
    End If
    
    'copy the data to the database
    ws.Cells(iRow, 1).Value = Me.txtProduct.Value
    ws.Cells(iRow, 2).Value = Me.cboUnit.Value
    ws.Cells(iRow, 3).Value = Me.txtPrice.Value
    ws.Cells(iRow, 4).Value = Me.txtLabour.Value
    
    
    
    'clear the data
    Me.cboCategory.Value = ""
    Me.txtProduct.Value = ""
    Me.cboUnit.Value = ""
    Me.txtPrice.Value = ""
    Me.txtLabour.Value = ""
    
    Me.cboCategory.SetFocus
    
    End Sub
    Last edited by Mooseman60; 12-05-2010 at 09:11 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Combobox Selection To Insert Data

    Like so, perhaps:
    Option Explicit
    
    Private Sub cmdAdd_Click()
    Dim iRow As Long
    Dim ws As Worksheet
      
    'check for a Category
    If Trim(Me.cboCategory.Value) = "" Then
      Me.cboCategory.SetFocus
      MsgBox "Please Select a Category"
      Exit Sub
    Else
    'Set correct worksheet
      Set ws = Sheets(Trim(Me.oboCategory.Value))
    'find first empty row in database
      iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    End If
    
    'check for a Product
    If Trim(Me.txtProduct.Value) = "" Then
      Me.txtProduct.SetFocus
      MsgBox "Please Enter a Product Description"
      Exit Sub
    End If
    
    'copy the data to the database
      ws.Cells(iRow, 1).Value = Me.txtProduct.Value
      ws.Cells(iRow, 2).Value = Me.cboUnit.Value
      ws.Cells(iRow, 3).Value = Me.txtPrice.Value
      ws.Cells(iRow, 4).Value = Me.txtLabour.Value
    
    'clear the data
      Me.cboCategory.Value = ""
      Me.txtProduct.Value = ""
      Me.cboUnit.Value = ""
      Me.txtPrice.Value = ""
      Me.txtLabour.Value = ""
    
      Me.cboCategory.SetFocus
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    09-26-2010
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    182

    Re: Combobox Selection To Insert Data

    Jerry

    Excellent worked a treat thank you so much

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Combobox Selection To Insert Data

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ 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