+ Reply to Thread
Results 1 to 9 of 9

Need VBA to update new emp through userform to sheet and table both

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-21-2011
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2007,2010,2016
    Posts
    695

    Need VBA to update new emp through userform to sheet and table both

    Dear Experts

    I have rawdata emp info where stored emp info

    i have userform to fill details once fill details i submit i want update details into emp info as well simultaneously include in table under corresponding team

    please find the attachment
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,508

    Re: Need VBA to update new emp through userform to sheet and table both

    You mention "userform" but if you aleady have one, why is it not in your w/book?

    A non_VBA example (but not what you want I suspect!)

    in C3

    Formula: copy to clipboard
    =IFERROR(VLOOKUP(INDEX(TableX!$C$3:$I$18,MATCH(Data!$B3,TableX!$B$3:$B$18,0),MATCH(Data!C$2,TableX!$C$2:$I$2,0)),Shift_Times,2,0),VLOOKUP(INDEX(TableX!$C$3:$I$18,MATCH(Data!$B3,TableX!$B$3:$B$18,0)+1,MATCH(Data!C$2,TableX!$C$2:$I$2,0)),Short_Term_Leave,2,0))


    with named ranges for shifts / leave and new layout in "Tablex": no colour coding for "Leave" types

    Having spare rows in Excel tables is pointless and can make solutions more complex.

    And there is no obvious reason to separate out Shifts /Leave on separate rows .
    Attached Files Attached Files
    Last edited by JohnTopley; 07-04-2023 at 12:24 PM.
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  3. #3
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,508

    Re: Need VBA to update new emp through userform to sheet and table both

    Option Explicit
    Sub Fill_Form()
    Dim a, rng As Range, scode As String, empNo As Integer, shift As String
    Dim i As Long, j As Long, lr As Long
    
    Application.ScreenUpdating = False
    
    With Sheets("Data")
    
        lr = .Cells(Rows.Count, "A").End(xlUp).Row
        Set rng = .Range("C2:I2")
        .Range("C3:K" & lr + 1).Clear
        
        For i = 3 To lr
            
            For j = 3 To 8
                empNo = Application.Match(.Cells(i, "B"), Range("Tablex").Columns(2), 0)
                scode = Application.Index(Range("Tablex"), empNo, j)
                If scode <> "" Then
                     shift = Application.VLookup(scode, Range("Shift_Times"), 2, 0)
                     .Cells(i, "J") = .Cells(i, "J") + 1
                     .Cells(lr + 1, "J") = .Cells(lr + 1, "J") + 1
                Else
                    empNo = empNo + 1
                    scode = Application.Index(Range("Tablex"), empNo, j)
                    shift = Application.VLookup(scode, Range("Short_term_leave"), 2, 0)
                    .Cells(i, j).Interior.ColorIndex = Application.VLookup(scode, Range("Short_term_leave"), 3, 0)
                    .Cells(i, "K") = .Cells(i, "K") + 1
                    .Cells(lr + 1, "K") = .Cells(lr + 1, "K") + 1
                End If
                .Cells(i, j) = shift
            Next j
            
        Next i
        
        With .Range("A1:K" & lr + 1)
            .Borders.Weight = 2
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
        End With
        
    End With
    
    Application.ScreenUpdating = True
    
    End Sub
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    07-21-2011
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2007,2010,2016
    Posts
    695

    Re: Need VBA to update new emp through userform to sheet and table both

    Thanks expert!! Its solved i have one small requirement

    if i extent the table for month year, i want code run till date where it end then fill total of workingdays & absent

    is it possible add on it

    thanks
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,508

    Re: Need VBA to update new emp through userform to sheet and table both

    Yes it could be changed but NOT with the current "TABLE format which is why I changed it in my solution : your layout is "pretty" but not "practical",

    Input errors: there are entries in both employee lines for a given date so you need to address this. The easiest way is to have a SINGLE line per employee with a drop-down which has both Shifts+Leave in the list.

    And what about the "Totals" which were in your original post? Better to be positioned in columns C:D followed by the dates in E onwards..

    I await your response to these points before I do further work.
    Last edited by JohnTopley; 07-05-2023 at 03:18 AM.

  6. #6
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,508

    Re: Need VBA to update new emp through userform to sheet and table both

    Option Explicit
    Sub Fill_Form()
    
    Dim a, rng As Range, tabRng As Range, scode As String, EmpNo_Row As Integer, description As String
    Dim i As Long, j As Long, lr As Long, lc As Integer
    
    Application.ScreenUpdating = False
    
    Set tabRng = Sheets("Table").Range("$A$1:$AG$200")  ' Table for upto 31 days
    
    With Sheets("Data")
    
        lr = .Cells(Rows.Count, "A").End(xlUp).Row
        lc = .Cells(2, Columns.Count).End(xlToLeft).Column
        Set rng = .Range(.Cells(3, 2), .Cells(lr, lc))
        .Range(.Cells(3, 3), .Cells(lr + 1, lc)).Clear                                       ' Clear output in "Data"
        
        For i = 3 To lr                                                                      ' Loop through EMPLOYEES
            
            For j = 3 To lc - 2                                                              ' Loop through month days
              
                EmpNo_Row = Application.Match(.Cells(i, "B"), Range("Employee_Name"), 0)     ' Get row for this Employee
                scode = Application.Index(tabRng, EmpNo_Row, j)                              ' Get SHIFT Code
                description = ""
                
                If scode <> "" Then
                     description = Application.VLookup(scode, Range("Shift_Times"), 2, 0)          ' Get SHIFT description
                     .Cells(i, "AH") = .Cells(i, "AH") + 1                                   ' Count of EMPLOYEE Working days
                     .Cells(lr + 1, "AH") = .Cells(lr + 1, "AH") + 1                         ' Count of TOTAL Working days
                Else
                    EmpNo_Row = EmpNo_Row + 1                                                ' Get second row of this Employee
                    scode = Application.Index(tabRng, EmpNo_Row, j)                          ' get LEAVE code
                    If scode <> "" Then
                        description = Application.VLookup(scode, Range("Short_term_leave"), 2, 0)  'get LEAVE description
                        .Cells(i, j).Interior.ColorIndex = Application.VLookup(scode, Range("Short_term_leave"), 3, 0) ' Highlight Cell
                        .Cells(i, "AI") = .Cells(i, "AI") + 1                                ' Count of Employee LEAVE days
                        .Cells(lr + 1, "AI") = .Cells(lr + 1, "AI") + 1                      ' Count of TOTAL Leave days
                    End If
                End If
                
                .Cells(i, j) = description                                                         'Shift/Leave descriptioon
            Next j
            
        Next i
        
        With .Range("A1:AI" & lr + 1)
            .Borders.Weight = 2
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
        End With
        
    End With
    
    Application.ScreenUpdating = True
    
    End Sub
    Attached Files Attached Files
    Last edited by JohnTopley; 07-05-2023 at 07:44 AM.

  7. #7
    Forum Contributor
    Join Date
    07-21-2011
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2007,2010,2016
    Posts
    695

    Re: Need VBA to update new emp through userform to sheet and table both

    Hi sir i have facing small issue, i just extent table sheet and renamed got error,

    can u make so simple fill your formula in vba code across row 2:2 till Total working days met , and column 2:2 till meet blank cell

    Formula: copy to clipboard
    ActiveCell.FormulaR1C1 = _
    "=IFERROR(VLOOKUP(INDEX(TableX!R3C3:R18C9,MATCH(Data!RC2,TableX!R3C2:R18C2,0),MATCH(Data!R2C,TableX!R2C3:R2C9,0)),Shift_Times,2,0),VLOOKUP(@INDEX(TableX!R3C3:R18C9,MATCH(Data!RC2,TableX!R3C2:R18C2,0)+1,MATCH(Data!R2C,TableX!R2C3:R2C9,0)),Short_Term_Leave,2,0))"


    find the
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,508

    Re: Need VBA to update new emp through userform to sheet and table both

    I wasted my time on a VBA solution so no more from me!!
    Last edited by JohnTopley; 07-05-2023 at 09:33 AM.

  9. #9
    Forum Contributor
    Join Date
    07-21-2011
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2007,2010,2016
    Posts
    695

    Re: Need VBA to update new emp through userform to sheet and table both

    hi sir , sorry for annoying you, thanks for your code its solved i just need it for third option.

+ 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. Update excel table from userform
    By Khanzaki in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-29-2022, 01:15 PM
  2. Help me regarding userform combo box update from Range table
    By mebro in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-10-2018, 12:34 AM
  3. [SOLVED] Update Existing Table with UserForm Values
    By TFiske in forum Excel Programming / VBA / Macros
    Replies: 25
    Last Post: 03-23-2018, 08:29 PM
  4. [SOLVED] Userform to Update Table
    By adamheon in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-17-2016, 12:15 PM
  5. Replies: 4
    Last Post: 08-15-2014, 04:02 PM
  6. update value and edit items already update on sheet by userform
    By tjxc32m in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 11-08-2013, 10:13 AM
  7. Update table from a userform with controls
    By mohanvp in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-26-2011, 03:56 AM

Tags for this Thread

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