+ Reply to Thread
Results 1 to 27 of 27

Need a macro to make workbooks based on employee names

Hybrid View

  1. #1
    Registered User
    Join Date
    05-20-2012
    Location
    OMAN
    MS-Off Ver
    Excel 2019
    Posts
    80

    Need a macro to make workbooks based on employee names

    Hi Friends I am attaching an Excel with attendance record of employees...I would like to have a macro to create workbooks based on employee names..ie for each employee there should be a workbook which contains his/her attendance data...Also please advice if I can have an outlook macro to mail these workbooks to employees without me having to manually do the same...
    Attached Files Attached Files

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Need a macro to make workbooks based on employee names

    Try this code - change the bold area to match your system -
    Sub copy_data()
    Dim shno As Long
    Dim lrow As Long
    Dim i As Long
    Dim empfile As String
    Dim wk As Workbook
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    ThisWorkbook.Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Temp"
    
    With ThisWorkbook.Worksheets("Temp")
        ThisWorkbook.Worksheets(1).Columns(2).Copy .Range("A1")
        .Columns("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
        lrow = .Range("A" & .Rows.Count).End(xlUp).Row
        For i = 2 To lrow
            empfile = "B:\Test\" & .Range("A" & i).Value & ".xlsx"
            ThisWorkbook.Worksheets(1).Rows(1).AutoFilter field:=2, Criteria1:=.Range("A" & i).Value
            Set wk = Workbooks.Add
            wk.SaveAs Filename:=empfile, FileFormat:=51
            ThisWorkbook.Worksheets(1).Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy
            wk.Worksheets(1).Range("A1").PasteSpecial (xlPasteValuesAndNumberFormats)
            Application.CutCopyMode = False
            wk.Worksheets(1).Rows(1).Font.Bold = True
            wk.Worksheets(1).Cells.EntireColumn.AutoFit
            wk.Save
            wk.Close
        Next i
    End With
    
    ThisWorkbook.Worksheets(1).Rows(1).AutoFilter
    ThisWorkbook.Worksheets("Temp").Delete
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
    End Sub
    Copy the Excel VBA code
    Select the workbook in which you want to store the Excel VBA code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    Choose Insert | Module
    Where the cursor is flashing, choose Edit | Paste

    To run the Excel VBA code:
    Choose View | Macros
    Select a macro in the list, and click the Run button
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    05-20-2012
    Location
    OMAN
    MS-Off Ver
    Excel 2019
    Posts
    80

    Re: Need a macro to make workbooks based on employee names

    Hi Arlette your code is perfect...I cant give you rep because my last rep was to you..but soon will give you one...btw can u also advice me regarding the macro for outlook ??

  4. #4
    Registered User
    Join Date
    05-20-2012
    Location
    OMAN
    MS-Off Ver
    Excel 2019
    Posts
    80

    Re: Need a macro to make workbooks based on employee names

    Hi Friends Arlette has given a perfect solution for the first part of my request...can anybody advice me regarding the outlook macro part ? If its impossible then I can close the thread as SOLVED...

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Need a macro to make workbooks based on employee names

    Hi renjithvakkayil

    Where will the Email Address be stored?
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  6. #6
    Registered User
    Join Date
    05-20-2012
    Location
    OMAN
    MS-Off Ver
    Excel 2019
    Posts
    80

    Re: Need a macro to make workbooks based on employee names

    Hi Jaslake in the code provided by Arlette I am modifying the folder where I want to save the newly created sheets...

    empfile = "B:\Test\" & .Range("A" & i).Value & ".xlsx" (I will make the change as D:\EMPLOYEEATTENDANCE) in the same folder I can have an excel workbook with Column A containing Employee names and B containing the corresponding email IDs...will that be okey??

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Need a macro to make workbooks based on employee names

    Hi renjithvakkayil

    Post a sample...I'll look at it.
    in the same folder I can have an excel workbook with Column A containing Employee names and B containing the corresponding email IDs

  8. #8
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Need a macro to make workbooks based on employee names

    Thank you Renjith. Am glad it worked for you. John should be able to help you. I have not done coding with outlook...still have to learn that.

  9. #9
    Registered User
    Join Date
    05-20-2012
    Location
    OMAN
    MS-Off Ver
    Excel 2019
    Posts
    80

    Re: Need a macro to make workbooks based on employee names

    Hi Jaslake when I run the macro the folder specified will be populated with worksheets named as per employee names..ie each name in the worksheet attached in my first post will have a corresponding excel sheet with only his/her data..

    In that folder I can save the attached excel..the macro should create mail according to the email ID and attach the corresponding excel and send it...
    Attached Files Attached Files

  10. #10
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Need a macro to make workbooks based on employee names

    Hi renjithvakkayil

    What do you currently send the Employee? Their Workbook as an attachment? Their Worksheet as an attachment? Their Worksheet as a PDF? Their Worksheet in the Body of the Email? Need to know what approach you wish to use.

  11. #11
    Registered User
    Join Date
    05-20-2012
    Location
    OMAN
    MS-Off Ver
    Excel 2019
    Posts
    80

    Re: Need a macro to make workbooks based on employee names

    We are not currently sending them anything as its time consuming..but now after the macros we are planning to send them the sheet as an attachment(xls or slsx) with subject attendance record.

  12. #12
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Need a macro to make workbooks based on employee names

    Hi renjithvakkayil

    Do all your Employees have Excel (2007 or later if .xlsx)? Will they be able to open an Excel File?
    Would not a .PDF attachment be better?
    Alternately, the Worksheet could be embedded in the Body of the Email such that there would be no need for Excel or Acrobat.
    You tell me.

  13. #13
    Registered User
    Join Date
    05-20-2012
    Location
    OMAN
    MS-Off Ver
    Excel 2019
    Posts
    80

    Re: Need a macro to make workbooks based on employee names

    Hi Jaslake that's a good point..few have 2007 and others 2010 and some dont have Acrobat..so I think the last method you mentioned will be okey for all...

  14. #14
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Need a macro to make workbooks based on employee names

    Hi renjithvakkayil

    Let me play with it a bit...

  15. #15
    Registered User
    Join Date
    05-20-2012
    Location
    OMAN
    MS-Off Ver
    Excel 2019
    Posts
    80

    Re: Need a macro to make workbooks based on employee names

    Thanks John!!

  16. #16
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Need a macro to make workbooks based on employee names

    Hi renjithvakkayil

    The attached File has Code to do this
    an outlook macro to mail these workbooks to employees without me having to manually do the same
    and this
    the Worksheet could be embedded in the Body of the Email
    I modified arlu's Code only very slightly.

    There are certain things you need to do. In Module Public_Variables you need to change these two items
    'This is where the Employee Files are stored
    Public Const myPath As String = "E:\Test\"
    
     'This is where the Employee Email Addresses are stored
     'This File should be stored in in the same Folder as the Employee Files
     'such that in this example EBook.xls will be located in E:\Test\
    Public Const EBook As String = "EBook.xls"
    EBook.xls needs to be maintained...you need to do this manually...if an Employee has not an Email Address in this file, you'll get a warning message but the Employee will not get an Email. Be diligent in maintaining this file.

    In MailBooks Module I'd STRONGLY urge you to set the Code to display in the Macro Sub Mail_Sheet_Outlook_Body() as you're testing...do that here
    With OutMail
                    .to = EmpCel.Offset(0, 1).Value
                    .CC = ""
                    .BCC = ""
                    .Subject = "Attendance Record"
                    .HTMLBody = RangetoHTML(rng)
                    .Send   'or use .Display '<---- to send
                    '            .Display    '<---- to display
                End With
    The Book is attached...let me know of issues.
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    05-20-2012
    Location
    OMAN
    MS-Off Ver
    Excel 2019
    Posts
    80

    Re: Need a macro to make workbooks based on employee names

    Hi John today I cannot access the office mail..as its our weekly OFF day(Being in Middle East we have FRIDAY holiday)...will check the code tomorrow and let you know how it went..Thanks a lot for helping me out...

  18. #18
    Registered User
    Join Date
    05-20-2012
    Location
    OMAN
    MS-Off Ver
    Excel 2019
    Posts
    80

    Re: Need a macro to make workbooks based on employee names

    Hi John the macro is running but n o mails are going out...I saved the Ebook with only my name in it...is that the problem ? Should i put mail IDs to all the files? I mean if a file doesnt have an ID only that mail wont go right ? Rest will go...
    Last edited by renjithvakkayil; 01-18-2013 at 04:28 AM.

  19. #19
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Need a macro to make workbooks based on employee names

    Hi renjithvakkayil

    I should have attached the EBook File I'm using for testing. Make certain your file has the same structure, including the Headers.
    Attached Files Attached Files

+ 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