+ Reply to Thread
Results 1 to 5 of 5

control worksheet with combobox value

Hybrid View

  1. #1
    Registered User
    Join Date
    01-05-2007
    Posts
    13

    control worksheet with combobox value

    Hello guys,
    Please I am lost. I have searched the forum to see if I can find a previous thread but could not.
    My problem: Iam trying to develope a time sheet to enable us clock in/out from a computer. I have a user form that holds a combo box and the combobox is populated with my staff names. What I want is when a staff selects his name from the combobox, it shoud activate a worksheet with the staff's name. this worksheet becomes the active worksheet to capture the staff time inputs. I have bellow the codes I am using.
    Please help.

    Private Sub CmbName_Change()
    Dim ws As Worksheet
    Select Case CmbName.Value
    Case "kelly"
    Set ws = Worksheets("kelly")
    End Select
    End Sub
    Private Sub CmdAdd_Click()
    Dim lRow As Long
    Dim intTimein As Integer
    Dim intTimeout As Integer
    Dim lTot As Long
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    'check for empty combobox
    If Trim(Me.CmbName.Value) = "" Then
     Me.CmbName.SetFocus
      MsgBox "Please enter Name"
      Exit Sub
    End If
    
    'find first empty row in database
    lRow = ws.Cells(Rows.Count, 1) _
     .End(xlUp).Offset(1, 0).Row
    'lName = Me.CmbName.ListIndex
    
    'copy the data to active worksheet
    With ws
      .Cells(lRow, 1).Value = Me.CmbName.Value
      .Cells(lRow, 2).Value = Me.TxtdATE.Value
      .Cells(lRow, 3).Value = Me.TxtTimeIN.Value
      .Cells(lRow, 4).Value = Me.TxtTimeOut.Value
      .Cells(lRow, 5).Value = Me.TxtLunchOut.Value
      .Cells(lRow, 6).Value = Me.TxtLunchIN.Value
      .Cells(lRow, 7).Value = FormatDateTime(TimeValue("00:00:01AM") + TimeValue("23:59:59PM") + TimeValue(Me.TxtTimeOut.Value) - TimeValue(Me.TxtTimeIN.Value), vbShortTime)
       End If
      End With
    
    'clear the data
    Me.CmbName.Value = ""
    Me.TxtdATE.Value = ""
    Me.TxtTimeIN.Value = Format(Time, "h:m")
    Me.TxtTimeOut.Value = Format(Time, "h:m")
    Me.TxtLunchIN.Value = Format(Time, "h:m")
    Me.TxtLunchOut.Value = Format(Time, "h:m")
    Me.CmbName.SetFocus
    
    End Sub
    Private Sub Cmdclose_Click()
    Unload Me
    End Sub
    Private Sub UserForm_Initialize()
    
    Dim cName As Range
    Dim cDays As Range
    Dim ws As Worksheet
    Set ws = Worksheets("lookupList")
    
    'populate name combo box
    For Each cName In ws.Range("Namelist")
       With Me.CmbName
       .AddItem cName.Value
        .List(.ListCount - 1, 1) = cName.Offset(0, 1).Value
      End With
    Next cName
    
    Me.TxtTimeIN.Value = Format(Time, "h:m")
    Me.TxtTimeOut.Value = Format(Time, "h:m")
    Me.TxtLunchIN.Value = Format(Time, "h:m")
    Me.TxtLunchOut.Value = Format(Time, "h:m")
    Me.CmbName.SetFocus
    
    End Sub
    Last edited by VBA Noob; 08-31-2008 at 08:14 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I suggest you read the Fotum Rules & use the Edit button to add Code Tags before you run out of time for editing your post.
    Hope that helps.

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

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    01-05-2007
    Posts
    13
    Private Sub CmbName_Change()
    Dim ws As Worksheet
    Select Case CmbName.Value
    Case "kelly"
    Set ws = Worksheets("kelly")
    End Select
    End Sub
    
    Private Sub CmdAdd_Click()
    Dim lRow As Long
    Dim intTimein As Integer
    Dim intTimeout As Integer
    Dim lTot As Long
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    'check for empty combobox
    If Trim(Me.CmbName.Value) = "" Then
    Me.CmbName.SetFocus
    MsgBox "Please enter Name"
    Exit Sub
    End If
    
    'find first empty row in database
    lRow = ws.Cells(Rows.Count, 1) _
    .End(xlUp).Offset(1, 0).Row
    'lName = Me.CmbName.ListIndex
    
    'copy the data to active worksheet
    With ws
    .Cells(lRow, 1).Value = Me.CmbName.Value
    .Cells(lRow, 2).Value = Me.TxtdATE.Value
    .Cells(lRow, 3).Value = Me.TxtTimeIN.Value
    .Cells(lRow, 4).Value = Me.TxtTimeOut.Value
    .Cells(lRow, 5).Value = Me.TxtLunchOut.Value
    .Cells(lRow, 6).Value = Me.TxtLunchIN.Value
    .Cells(lRow, 7).Value = FormatDateTime(TimeValue("00:00:01AM") + TimeValue("23:59:59PM") + TimeValue(Me.TxtTimeOut.Value) - TimeValue(Me.TxtTimeIN.Value), vbShortTime)
    End If
    End With
    
    'clear the data
    Me.CmbName.Value = ""
    Me.TxtdATE.Value = ""
    Me.TxtTimeIN.Value = Format(Time, "h:m")
    Me.TxtTimeOut.Value = Format(Time, "h:m")
    Me.TxtLunchIN.Value = Format(Time, "h:m")
    Me.TxtLunchOut.Value = Format(Time, "h:m")
    Me.CmbName.SetFocus
    
    End Sub
    
    
    
    Private Sub Cmdclose_Click()
    Unload Me
    End Sub
    
    
    Private Sub UserForm_Initialize()
    
    Dim cName As Range
    Dim cDays As Range
    Dim ws As Worksheet
    Set ws = Worksheets("lookupList")
    
    'populate name combo box
    For Each cName In ws.Range("Namelist")
    With Me.CmbName
    .AddItem cName.Value
    .List(.ListCount - 1, 1) = cName.Offset(0, 1).Value
    End With
    Next cName
    
    Me.TxtTimeIN.Value = Format(Time, "h:m")
    Me.TxtTimeOut.Value = Format(Time, "h:m")
    Me.TxtLunchIN.Value = Format(Time, "h:m")
    Me.TxtLunchOut.Value = Format(Time, "h:m")
    Me.CmbName.SetFocus
    
    End Sub

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    It looks like you setting the variable es but not then using the With statement

    With ws
    'etc
    End With

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

    Option Explicit
    Dim ws         As Worksheet
    
    Private Sub CmbName_Change()
        Set ws = Worksheets(Me.CmbName.Value)
    End Sub
    
    Private Sub CmdAdd_Click()
        Dim lRow   As Long
        'you don't use these variables
        '    Dim intTimein As Integer
        '    Dim intTimeout As Integer
        '    Dim lTot   As Long
    
        'check for empty combobox
        If Trim(Me.CmbName.Value) = "" Then
            Me.CmbName.SetFocus
            MsgBox "Please enter Name"
            Exit Sub
        End If
    
        'find first empty row in database
        lRow = ws.Cells(Rows.Count, 1) _
               .End(xlUp).Offset(1, 0).Row
        'lName = Me.CmbName.ListIndex
    
        'copy the data to active worksheet
        With ws
            .Cells(lRow, 1).Value = Me.CmbName.Value
            .Cells(lRow, 2).Value = Me.TxtdATE.Value
            .Cells(lRow, 3).Value = Me.TxtTimeIN.Value
            .Cells(lRow, 4).Value = Me.TxtTimeOut.Value
            .Cells(lRow, 5).Value = Me.TxtLunchOut.Value
            .Cells(lRow, 6).Value = Me.TxtLunchIN.Value
            .Cells(lRow, 7).Value = FormatDateTime(TimeValue("00:00:01AM") + TimeValue("23:59:59PM") + TimeValue(Me.TxtTimeOut.Value) - TimeValue(Me.TxtTimeIN.Value), vbShortTime)
        End If
    End With
    
    'clear the data
    With Me
        .CmbName.Value = ""
        .TxtdATE.Value = ""
        .TxtTimeIN.Value = Format(Time, "h:m")
        .TxtTimeOut.Value = Format(Time, "h:m")
        .TxtLunchIN.Value = Format(Time, "h:m")
        .TxtLunchOut.Value = Format(Time, "h:m")
        .CmbName.SetFocus
    End With
    End Sub
    
    
    
    Private Sub Cmdclose_Click()
        Unload Me
    End Sub
    
    
    Private Sub UserForm_Initialize()
        Set ws = Worksheets("lookupList")
        'populate name combo box
        Me.CmbName.List = ws.Range("Namelist").Value
        With Me
            .TxtTimeIN.Value = Format(Time, "h:m")
            .TxtTimeOut.Value = Format(Time, "h:m")
            .TxtLunchIN.Value = Format(Time, "h:m")
            .TxtLunchOut.Value = Format(Time, "h:m")
            .CmbName.SetFocus
        End With
    End Sub

+ 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. Inventory worksheet
    By tourcat in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-03-2008, 07:25 PM
  2. Worksheet call outs
    By amcall in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-24-2008, 01:51 PM
  3. Replies: 3
    Last Post: 08-17-2007, 02:51 PM
  4. Embedding Control In Worksheet
    By Dan_Dollar in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-31-2007, 04:06 PM
  5. combobox selection adding new row to worksheet
    By buckchow in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-18-2007, 05:55 AM

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