+ Reply to Thread
Results 1 to 5 of 5

Thread: Combo box to select column

  1. #1
    Registered User
    Join Date
    07-24-2008
    Location
    UK
    Posts
    9

    Combo box to select column

    Hi all,

    I have the code below that allows me to enter data into 3 columns of the next empty row dependant on what is added into text box's on a userform.

    However, I require a combo box to be added. This combo box will have 4 choices. e.g. word1,word2,word3,word4.

    What I require is if Word 1 is selected then the values in the text box's are added into column 1, If word2 is selcted then the values in the text box's are added to column 5, If word 3, column 9 and if word4 then column 14.

    My VBA knowledge doesnt stretch this far....

    Private Sub cboRegion_Change()
     'Code needs to be entered to allow entry of words e.g Word1, word2,word3.
    End Sub
    
    'This enters the data entered into text box's txtBuyer,txtNumberOfOrders,txtSlippage into the next empty row in column 1
    
    Private Sub cmdAdd_Click()
    Dim iRow As Long
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    
    iRow = ws.Cells(Rows.Count, 1) _
      .End(xlUp).Offset(1, 0).Row
    
    If Trim(Me.txtBuyer.Value) = "" Then
      Me.txtBuyer.SetFocus
      MsgBox "Please enter buyer name"
      Exit Sub
    End If
    
    ws.Cells(iRow, 1).Value = Me.txtBuyer.Value
    ws.Cells(iRow, 2).Value = Me.TxtNumberOfOrders.Value
    ws.Cells(iRow, 3).Value = Me.txtSlippage.Value
    
    
    Me.txtBuyer.Value = ""
    Me.TxtNumberOfOrders.Value = ""
    Me.txtSlippage.Value = ""
    Me.txtBuyer.SetFocus
    End Sub
    'This closes the userform
    Private Sub cmdClose_Click()
      Unload Me
    End Sub

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,788
    Could you perhaps attach a small sample workbook? I take it your use of "word1" etc is merely illustrative?

  3. #3
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639
    Try this

    Populate the Combo

    Private Sub UserForm_Initialize()
    'populate Combo
        Me.cboRegion.List = Array("Word1", "Word2", "Word3", "Word4")
    End Sub

    Add the data to the sheet

    Private Sub cmdAdd_Click()
        Dim C      As Long
        Dim iRow   As Long
        Dim ws     As Worksheet
        Set ws = ActiveSheet
        With Me
            C = .cboRegion.ListIndex + 1
            If C = 0 Then
                MsgBox "Please select a Region", vbCritical, "Input Error"
                .cboRegion.SetFocus
                Exit Sub
            End If
            iRow = ws.Cells(Rows.Count, 1) _
                   .End(xlUp).Offset(1, 0).Row
    
            If Trim(.txtBuyer.Value) = "" Then
                .txtBuyer.SetFocus
                MsgBox "Please enter buyer name"
                Exit Sub
            End If
            With ws
                ws.Cells(iRow, 1).Value = .txtBuyer.Value
                ws.Cells(iRow, 2).Value = .TxtNumberOfOrders.Value
                ws.Cells(iRow, 3).Value = .txtSlippage.Value
                ws.Cells(iRow, C).Value = .cboRegion.Value
    
                .txtBuyer.Value = ""
                .TxtNumberOfOrders.Value = ""
                .txtSlippage.Value = ""
                .txtBuyer.SetFocus
            End With
        End Sub
    I can't see what you mean by adding the TextBoxes to one column. Your code doesnot appear to add the input to one Column.
    Last edited by royUK; 08-13-2008 at 06:14 AM.
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  4. #4
    Registered User
    Join Date
    07-24-2008
    Location
    UK
    Posts
    9
    Hi, I managed to figure that out in the end, using the code below
    Private Sub cboRegion_Change()
     cboRegion.AddItem "NEWT"
     cboRegion.AddItem "NWWT"
     cboRegion.AddItem "CDT"
     cboRegion.AddItem "AMB"
     'Code needs to be entered to allow entry of words e.g Word1, word2,word3.
    End Sub
    
    'This enters the data entered into text box's txtBuyer,txtNumberOfOrders,txtSlippage into the next empty row in column 1
    
    Private Sub cmdAdd_Click()
    Dim iRow As Long
    
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    If cboRegion.Value = "NEWT" Then
    
        iRow = ws.Cells(Rows.Count, 9) _
        .End(xlUp).Offset(1, 0).Row
    
            If Trim(Me.txtBuyer.Value) = "" Then
            Me.txtBuyer.SetFocus
            MsgBox "Please enter buyer name"
            Exit Sub
        End If
    
        ws.Cells(iRow, 9).Value = Me.txtBuyer.Value
        ws.Cells(iRow, 10).Value = Me.TxtNumberOfOrders.Value
        ws.Cells(iRow, 11).Value = Me.txtSlippage.Value
    
    
        Me.txtBuyer.Value = ""
        Me.TxtNumberOfOrders.Value = ""
        Me.txtSlippage.Value = ""
        Me.txtBuyer.SetFocus
    
        Else
            If cboRegion.Value = "NWWT" Then
            iRow = ws.Cells(Rows.Count, 13) _
            .End(xlUp).Offset(1, 0).Row
    
                If Trim(Me.txtBuyer.Value) = "" Then
                Me.txtBuyer.SetFocus
                MsgBox "Please enter buyer name"
                Exit Sub
                End If
    
            ws.Cells(iRow, 13).Value = Me.txtBuyer.Value
            ws.Cells(iRow, 14).Value = Me.TxtNumberOfOrders.Value
            ws.Cells(iRow, 15).Value = Me.txtSlippage.Value
    
    
            Me.txtBuyer.Value = ""
            Me.TxtNumberOfOrders.Value = ""
            Me.txtSlippage.Value = ""
            Me.txtBuyer.SetFocus
    
    
    Else
    If cboRegion.Value = "CDT" Then
        iRow = ws.Cells(Rows.Count, 5) _
        .End(xlUp).Offset(1, 0).Row
    
            If Trim(Me.txtBuyer.Value) = "" Then
            Me.txtBuyer.SetFocus
            MsgBox "Please enter buyer name"
            Exit Sub
            End If
    
    ws.Cells(iRow, 5).Value = Me.txtBuyer.Value
    ws.Cells(iRow, 6).Value = Me.TxtNumberOfOrders.Value
    ws.Cells(iRow, 7).Value = Me.txtSlippage.Value
    
    
    Me.txtBuyer.Value = ""
    Me.TxtNumberOfOrders.Value = ""
    Me.txtSlippage.Value = ""
    Me.txtBuyer.SetFocus
    
    Else
        If cboRegion.Value = "AMB" Then
        iRow = ws.Cells(Rows.Count, 1) _
        .End(xlUp).Offset(1, 0).Row
    
            If Trim(Me.txtBuyer.Value) = "" Then
            Me.txtBuyer.SetFocus
            MsgBox "Please enter buyer name"
            Exit Sub
            End If
    
    ws.Cells(iRow, 1).Value = Me.txtBuyer.Value
    ws.Cells(iRow, 2).Value = Me.TxtNumberOfOrders.Value
    ws.Cells(iRow, 3).Value = Me.txtSlippage.Value
    
    
    Me.txtBuyer.Value = ""
    Me.TxtNumberOfOrders.Value = ""
    Me.txtSlippage.Value = ""
    Me.txtBuyer.SetFocus
    
    End If
    End If
    End If
    End If
    
    
    
    
    End Sub
    Roy, I have added the words to this code in the top few lines ("AMB,NEWT" etc).
    However, i have found that this code works, apart from the fact that it doesnt show the available choices in the dropdown menu, it is blank until a letter is entered. Is it possible to show the available choices in the drop down menu before anything is entered?

    Regards

  5. #5
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639
    I mprovided code for you that loads the ComboBox using the form's initialize event

    Private Sub UserForm_Initialize()
    'populate Combo
        Me.cboRegion.List = Array("Word1", "Word2", "Word3", "Word4")
    End Sub
    And this should enter the data into the correct Columns

    'This enters the data entered into text box's txtBuyer,txtNumberOfOrders,txtSlippage into the next empty row in column 1
    
    Private Sub cmdAdd_Click()
        Dim C      As Long
        Dim iRow   As Long
        Dim ws     As Worksheet
        Set ws = ActiveSheet
        
        With Me
            Select Case .cboRegion.ListIndex
                Case 0: C = 9    '"NEWT
                Case 1: C = 13    '"NWWT"
                Case 2: C = 5    '"CDT"
                Case 3: C = 1    '"AMB"
                Case Else
                    MsgBox "Please select a Region", vbCritical, "Input Error"
                    .cboRegion.SetFocus
                    Exit Sub
            End Select
            iRow = ws.Cells(Rows.Count, 1) _
                   .End(xlUp).Offset(1, 0).Row
    
            If Trim(.txtBuyer.Value) = "" Then
                .txtBuyer.SetFocus
                MsgBox "Please enter buyer name"
                Exit Sub
            End If
            
            With ws
                ws.Cells(iRow, C).Value = .txtBuyer.Value
                ws.Cells(iRow, C).Value = .TxtNumberOfOrders.Value
                ws.Cells(iRow, C).Value = .txtSlippage.Value
    
                .txtBuyer.Value = ""
                .TxtNumberOfOrders.Value = ""
                .txtSlippage.Value = ""
                .txtBuyer.SetFocus
                .cmdAdd.Enabled=False 'disable add button
            End With
        End Sub
    Yopu could make sure that the user can only use the add button if a region is selected by disabling the add buton.

    Private Sub UserForm_Initialize()
    'populate Combo
       with Me
    .cboRegion.List = Array("Word1", "Word2", "Word3", "Word4")
    .cmdAdd.Enabled = False
    End With
    End Sub
    'enable the add button after selection
    Private Sub cboRegion_Change()
     Me.cmdAdd.Enabled=True
    End Sub
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

+ 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.2.0