Employee Roster Separation

Pestomania

Active Member
Joined
May 30, 2018
Messages
332
Office Version
  1. 365
Platform
  1. Windows
Hi there,

I am trying to set up the macros that save off an excel sheet based on Mgr1 Name.

My thoughts:

1. A Macro that moves all employees that report to a specific Mgr to a new excel sheet.
2. Rename the sheet to that Mgr name.
3. Format it to look nice (I can do the formatting code)
4. Print to PDF
5. Save the PDF with the Mgr Name in a certain file.

BCM Copy of Organziational Structure (for upload).xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAK
1NameOrg Codeuser.mobileU.S. PersonCompanyDepartmentSiteIDorganizationalPerson.homePhoneEmailMgr1 NameMgr1 IDMgr1 EmailMgr2 NameMgr2 IDMgr2 EmailMgr3 NameMgr3 IDMgr3 EmailMgr4 NameMgr4 IDMgr4 EmailMgr5 NameMgr 5 IDMgr5 EmailMgr6 NameMgr6 IDMgr6 EmailMgr7 NameMgr7 IDMgr7 EmailMgr8 NameMgr8 IDMgr8 EmailMgr9organizationalPerson.mobileorganizationalPerson.physicalDeliveryOfficeName
2Employee 1Manager 5Manager 4
3Employee 2Manager 4Manager 7
4Employee 3Manager 15Manager 6
5Employee 4Manager 6Manager 2
6Employee 5Manager 6Manager 1
7Employee 6Manager 15Manager 4
8Employee 7Manager 12Manager 2
9Employee 8Manager 2Manager 8
10Employee 9Manager 7Manager 2
11Employee 10Manager 7Manager 14
12Employee 11Manager 4Manager 8
13Employee 12Manager 5Manager 2
14Employee 13Manager 14Manager 13
15Employee 14Manager 6Manager 3
16Employee 15Manager 15Manager 13
17Employee 16Manager 13Manager 9
18Employee 17Manager 15Manager 9
19Employee 18Manager 12Manager 13
20Employee 19Manager 15Manager 6
21Employee 20Manager 1Manager 3
22Employee 21Manager 1Manager 6
23Employee 22Manager 12Manager 10
24Employee 23Manager 8Manager 11
25Employee 24Manager 2Manager 9
26Employee 25Manager 13Manager 13
27Employee 26Manager 13Manager 1
28Employee 27Manager 6Manager 6
29Employee 28Manager 9Manager 6
30Employee 29Manager 2Manager 2
31Employee 30Manager 2Manager 4
32Employee 31Manager 11Manager 1
33Employee 32Manager 10Manager 11
34Employee 33Manager 15Manager 5
35Employee 34Manager 8Manager 11
36Employee 35Manager 14Manager 11
37Employee 36Manager 11Manager 3
38Employee 37Manager 6Manager 15
39Employee 38Manager 15Manager 4
40Employee 39Manager 3Manager 10
41Employee 40Manager 5Manager 7
42Employee 41Manager 4Manager 9
43Employee 42Manager 15Manager 6
44Employee 43Manager 8Manager 5
45Employee 44Manager 13Manager 14
46Employee 45Manager 14Manager 14
47Employee 46Manager 7Manager 7
48Employee 47Manager 10Manager 11
49Employee 48Manager 13Manager 10
50Employee 49Manager 7Manager 12
51Employee 50Manager 3Manager 5
52Employee 51Manager 9Manager 2
53Employee 52Manager 10Manager 12
54Employee 53Manager 8Manager 9
55Employee 54Manager 4Manager 4
56Employee 55Manager 12Manager 7
57Employee 56Manager 2Manager 13
58Employee 57Manager 10Manager 4
59Employee 58Manager 8Manager 5
60Employee 59Manager 4Manager 8
61Employee 60Manager 9Manager 3
62Employee 61Manager 4Manager 2
63Employee 62Manager 14Manager 9
64Employee 63Manager 7Manager 5
user
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi Pestomania,

A couple of questions if I may...
  1. Is it just the list of employee names that you need from the above example?
  2. You say 'move' so they will no longer be in the original list? Or do you need them just 'copied' across to the PDF?
  3. Also how would you like to have people select the managers name?
    • Dropdown selection on a sheet - trigger macro with a button click
    • Custom userform - in which case are you able to create a userform?
I'm happy to help as I've done a similar exercise to this before where I work.
 
Upvote 0
Hi Pestomania,

A couple of questions if I may...
  1. Is it just the list of employee names that you need from the above example?
  2. You say 'move' so they will no longer be in the original list? Or do you need them just 'copied' across to the PDF?
  3. Also how would you like to have people select the managers name?
    • Dropdown selection on a sheet - trigger macro with a button click
    • Custom userform - in which case are you able to create a userform?
I'm happy to help as I've done a similar exercise to this before where I work.
Hi @sxhall, I would "copy" them so that I don't have to regularly orginial file.

I want to save each of the managers lists off as a PDF with their name and the managers can find their own documents. I may consider doing an email in time, which I think I can do myself.

Here was my thought on process, but I am not sophisticated enough:

1. Copy the entire row over to a new excel sheet
2. Name the document after the Manager
3. Reformat it (I'll do this). I only need the first 12 columns for the pdf, but I'll automate it to hide everything else.
4. Save the file as a PDF with the managers name.
5. Once created, I may have it email out to the manager.
 
Upvote 0
OK,

Will add the manager selection as a dropdown box which will need to be on a seperate sheet, will need to activate the macro via a button click.
 
Upvote 0
Hello everyone,
here is my attempt, I'll briefly explain how it works. Remember to adapt the sheet name.

  1. A collection of all values in column K is created in memory.
  2. A loop is executed, filtering the sheet by individual values (manager's name).
  3. The save path (same as the Excel file, customizable if necessary) and the PDF file name are assigned.
  4. A filter is applied to column K, and for each unique value, only the visible cells are copied.
  5. A sheet with the manager's name is added, the data is pasted, and it is then exported as a PDF (adjusting the content to fit on one page).
  6. At the end, the filter is removed.
In your request, you mentioned manual operations to be performed afterward, but if you use VBA to automate the procedures, you can handle everything automatically. If the sheet containing the data has proper formatting, it will be retained when copying the data to a new sheet (choosing the columns to copy). If the goal is to create a PDF to send via email, you likely don't need a separate sheet; you can use the same macro to create the PDF and send it via Outlook.
VBA Code:
Sub Pestomania()

'https://www.mrexcel.com/board/threads/employee-roster-separation.1265153/

    Dim SourceWs         As Worksheet
    Dim cel         As Range, myRng As Range
    Dim Itm         As Variant
    Dim LastRow     As Long
    Dim collMgr   As New Collection
    Dim MyFile As String
    
    Set SourceWs = ThisWorkbook.Sheets("Sheet1") '<<============ ADAPT Sheet name
        
    On Error Resume Next
    '1)
    For Each cel In SourceWs.Range("K2:K" & Range("K" & Rows.Count).End(xlUp).Row)
        collMgr.Add cel.Value, CStr(cel.Value)
    Next
    On Error GoTo 0
    
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    '2)
    For Each Itm In collMgr
    
    '3)
    MyFile = ThisWorkbook.Path & Application.PathSeparator & Itm & ".PDF"
    
    '4)
        
    SourceWs.Range("A1:L" & Range("A" & Rows.Count).End(xlUp).Row).AutoFilter Field:=11, Criteria1:=Itm
    
    LastRow = SourceWs.Cells(1, 1).SpecialCells(xlCellTypeVisible).End(xlDown).Row
    
    SourceWs.Range("A1:L" & LastRow).SpecialCells(xlCellTypeVisible).Copy
    
    '5)
    With ThisWorkbook
        .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = Itm
        With ActiveSheet
            .Range("A1").PasteSpecial Paste:=xlPasteFormats
            .Range("A1").PasteSpecial Paste:=xlPasteValues
            With ActiveSheet.PageSetup
                .Zoom = False
                .Orientation = xlPortrait
                .FitToPagesWide = 1
                .FitToPagesTall = 1
            End With
            .ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=MyFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
        End With
        
    End With
    
    Next
    '6)
    On Error Resume Next
    SourceWs.ShowAllData
    On Error GoTo 0
    
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    
End Sub
 
Upvote 0
Solution
Hello everyone,
here is my attempt, I'll briefly explain how it works. Remember to adapt the sheet name.

  1. A collection of all values in column K is created in memory.
  2. A loop is executed, filtering the sheet by individual values (manager's name).
  3. The save path (same as the Excel file, customizable if necessary) and the PDF file name are assigned.
  4. A filter is applied to column K, and for each unique value, only the visible cells are copied.
  5. A sheet with the manager's name is added, the data is pasted, and it is then exported as a PDF (adjusting the content to fit on one page).
  6. At the end, the filter is removed.
In your request, you mentioned manual operations to be performed afterward, but if you use VBA to automate the procedures, you can handle everything automatically. If the sheet containing the data has proper formatting, it will be retained when copying the data to a new sheet (choosing the columns to copy). If the goal is to create a PDF to send via email, you likely don't need a separate sheet; you can use the same macro to create the PDF and send it via Outlook.
VBA Code:
Sub Pestomania()

'https://www.mrexcel.com/board/threads/employee-roster-separation.1265153/

    Dim SourceWs         As Worksheet
    Dim cel         As Range, myRng As Range
    Dim Itm         As Variant
    Dim LastRow     As Long
    Dim collMgr   As New Collection
    Dim MyFile As String
   
    Set SourceWs = ThisWorkbook.Sheets("Sheet1") '<<============ ADAPT Sheet name
       
    On Error Resume Next
    '1)
    For Each cel In SourceWs.Range("K2:K" & Range("K" & Rows.Count).End(xlUp).Row)
        collMgr.Add cel.Value, CStr(cel.Value)
    Next
    On Error GoTo 0
   
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
   
    '2)
    For Each Itm In collMgr
   
    '3)
    MyFile = ThisWorkbook.Path & Application.PathSeparator & Itm & ".PDF"
   
    '4)
       
    SourceWs.Range("A1:L" & Range("A" & Rows.Count).End(xlUp).Row).AutoFilter Field:=11, Criteria1:=Itm
   
    LastRow = SourceWs.Cells(1, 1).SpecialCells(xlCellTypeVisible).End(xlDown).Row
   
    SourceWs.Range("A1:L" & LastRow).SpecialCells(xlCellTypeVisible).Copy
   
    '5)
    With ThisWorkbook
        .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = Itm
        With ActiveSheet
            .Range("A1").PasteSpecial Paste:=xlPasteFormats
            .Range("A1").PasteSpecial Paste:=xlPasteValues
            With ActiveSheet.PageSetup
                .Zoom = False
                .Orientation = xlPortrait
                .FitToPagesWide = 1
                .FitToPagesTall = 1
            End With
            .ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=MyFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
        End With
       
    End With
   
    Next
    '6)
    On Error Resume Next
    SourceWs.ShowAllData
    On Error GoTo 0
   
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
   
End Sub
Hello everyone,
here is my attempt, I'll briefly explain how it works. Remember to adapt the sheet name.

  1. A collection of all values in column K is created in memory.
  2. A loop is executed, filtering the sheet by individual values (manager's name).
  3. The save path (same as the Excel file, customizable if necessary) and the PDF file name are assigned.
  4. A filter is applied to column K, and for each unique value, only the visible cells are copied.
  5. A sheet with the manager's name is added, the data is pasted, and it is then exported as a PDF (adjusting the content to fit on one page).
  6. At the end, the filter is removed.
In your request, you mentioned manual operations to be performed afterward, but if you use VBA to automate the procedures, you can handle everything automatically. If the sheet containing the data has proper formatting, it will be retained when copying the data to a new sheet (choosing the columns to copy). If the goal is to create a PDF to send via email, you likely don't need a separate sheet; you can use the same macro to create the PDF and send it via Outlook.
VBA Code:
Sub Pestomania()

'https://www.mrexcel.com/board/threads/employee-roster-separation.1265153/

    Dim SourceWs         As Worksheet
    Dim cel         As Range, myRng As Range
    Dim Itm         As Variant
    Dim LastRow     As Long
    Dim collMgr   As New Collection
    Dim MyFile As String
   
    Set SourceWs = ThisWorkbook.Sheets("Sheet1") '<<============ ADAPT Sheet name
       
    On Error Resume Next
    '1)
    For Each cel In SourceWs.Range("K2:K" & Range("K" & Rows.Count).End(xlUp).Row)
        collMgr.Add cel.Value, CStr(cel.Value)
    Next
    On Error GoTo 0
   
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
   
    '2)
    For Each Itm In collMgr
   
    '3)
    MyFile = ThisWorkbook.Path & Application.PathSeparator & Itm & ".PDF"
   
    '4)
       
    SourceWs.Range("A1:L" & Range("A" & Rows.Count).End(xlUp).Row).AutoFilter Field:=11, Criteria1:=Itm
   
    LastRow = SourceWs.Cells(1, 1).SpecialCells(xlCellTypeVisible).End(xlDown).Row
   
    SourceWs.Range("A1:L" & LastRow).SpecialCells(xlCellTypeVisible).Copy
   
    '5)
    With ThisWorkbook
        .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = Itm
        With ActiveSheet
            .Range("A1").PasteSpecial Paste:=xlPasteFormats
            .Range("A1").PasteSpecial Paste:=xlPasteValues
            With ActiveSheet.PageSetup
                .Zoom = False
                .Orientation = xlPortrait
                .FitToPagesWide = 1
                .FitToPagesTall = 1
            End With
            .ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=MyFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
        End With
       
    End With
   
    Next
    '6)
    On Error Resume Next
    SourceWs.ShowAllData
    On Error GoTo 0
   
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
   
End Sub
This did the trick!

It wasn't so much that I would do manual work as I would add in the code to do the formatting (I am working on that now). After the full dataset was complete, it was over 650 different managers so it took almost an hour to complete the full parsing, but oh well.

Thank you very much!
 
Upvote 0
@Sequoyah

Is there a way to remove any invalid characters for file names and anythign that has any text between parantheses mark.

I have a lot of names with "(i)" and "(EXT)" at the end of their name, I just want to remove that text completely.

And can there be a "popup" that uses "Unique Values of Column G" and allows you to select what sites you want to process under? So if I only want one of 20 sites, it will only search for those that meet the criteria of that one site?

Thank you

VBA Code:
Sub Pestomania()

'https://www.mrexcel.com/board/threads/employee-roster-separation.1265153/

    Dim SourceWs         As Worksheet
    Dim cel         As Range, myRng As Range
    Dim Itm         As Variant
    Dim LastRow     As Long
    Dim collMgr   As New Collection
    Dim MyFile As String
    
    Set SourceWs = ThisWorkbook.Sheets("Employee_Roster") '<<============ ADAPT Sheet name
        
    On Error Resume Next
    '1)
    For Each cel In SourceWs.Range("K2:K" & Range("K" & Rows.Count).End(xlUp).Row)
        collMgr.Add cel.Value, CStr(cel.Value)
    Next
    On Error GoTo 0
    
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    '2)
    For Each Itm In collMgr
    
    '3)
    MyFile = ThisWorkbook.Path & Application.PathSeparator & Itm & ".PDF"
    
    '4)
        
    SourceWs.Range("A1:L" & Range("A" & Rows.Count).End(xlUp).Row).AutoFilter Field:=11, Criteria1:=Itm
    
    LastRow = SourceWs.Cells(1, 1).SpecialCells(xlCellTypeVisible).End(xlDown).Row
    
    SourceWs.Range("A1:L" & LastRow).SpecialCells(xlCellTypeVisible).Copy
    
    '5)
    With ThisWorkbook
        .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = Itm
        With ActiveSheet
            .Range("A1").PasteSpecial Paste:=xlPasteFormats
            .Range("A1").PasteSpecial Paste:=xlPasteValues
            With ActiveSheet.PageSetup
                .Zoom = False
                .Orientation = xlPortrait
                .FitToPagesWide = 1
                .FitToPagesTall = 1
            End With
            .ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=MyFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
        End With
        
    End With
    
    Next
    '6)
    On Error Resume Next
    SourceWs.ShowAllData
    On Error GoTo 0
    
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    
End Sub
 
Upvote 0
@Sequoyah

Is there a way to remove any invalid characters for file names and anythign that has any text between parantheses mark.

I have a lot of names with "(i)" and "(EXT)" at the end of their name, I just want to remove that text completely.

And can there be a "popup" that uses "Unique Values of Column G" and allows you to select what sites you want to process under? So if I only want one of 20 sites, it will only search for those that meet the criteria of that one site?

Thank you

VBA Code:
Sub Pestomania()

'https://www.mrexcel.com/board/threads/employee-roster-separation.1265153/

    Dim SourceWs         As Worksheet
    Dim cel         As Range, myRng As Range
    Dim Itm         As Variant
    Dim LastRow     As Long
    Dim collMgr   As New Collection
    Dim MyFile As String
   
    Set SourceWs = ThisWorkbook.Sheets("Employee_Roster") '<<============ ADAPT Sheet name
       
    On Error Resume Next
    '1)
    For Each cel In SourceWs.Range("K2:K" & Range("K" & Rows.Count).End(xlUp).Row)
        collMgr.Add cel.Value, CStr(cel.Value)
    Next
    On Error GoTo 0
   
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
   
    '2)
    For Each Itm In collMgr
   
    '3)
    MyFile = ThisWorkbook.Path & Application.PathSeparator & Itm & ".PDF"
   
    '4)
       
    SourceWs.Range("A1:L" & Range("A" & Rows.Count).End(xlUp).Row).AutoFilter Field:=11, Criteria1:=Itm
   
    LastRow = SourceWs.Cells(1, 1).SpecialCells(xlCellTypeVisible).End(xlDown).Row
   
    SourceWs.Range("A1:L" & LastRow).SpecialCells(xlCellTypeVisible).Copy
   
    '5)
    With ThisWorkbook
        .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = Itm
        With ActiveSheet
            .Range("A1").PasteSpecial Paste:=xlPasteFormats
            .Range("A1").PasteSpecial Paste:=xlPasteValues
            With ActiveSheet.PageSetup
                .Zoom = False
                .Orientation = xlPortrait
                .FitToPagesWide = 1
                .FitToPagesTall = 1
            End With
            .ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=MyFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
        End With
       
    End With
   
    Next
    '6)
    On Error Resume Next
    SourceWs.ShowAllData
    On Error GoTo 0
   
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
   
End Sub
To add to this @Sequoyah, is there a way to limit the sheet name to the maximum number of allowed characters for a sheet name (the longest name I have is 50 characters long).
 
Upvote 0
I think I fixed the errors of the limit levels, but not the invalid characters for a name.

Is tehre a way to do this to make it by "site" & "-" & "Bldg"?

In the case where "Bldg" is blank, just put it by site name?

I tried but keep getting a key element of the collection error.

VBA Code:
Sub File_by_Building()

'https://www.mrexcel.com/board/threads/employee-roster-separation.1265153/

    Dim SourceWs         As Worksheet
    Dim cel         As Range, myRng As Range
    Dim Bldg         As Variant
    Dim Site        As Variant
    Dim LastRow     As Long
    Dim CollBldg   As New Collection
    Dim collSite    As New Collection
    Dim MyFile As String
    
    Set SourceWs = ThisWorkbook.Sheets("Employee_Roster") '<<============ ADAPT Sheet name
        
    On Error Resume Next
    '1)
    ActiveSheet.ListObjects("user").Range.AutoFilter Field:=9
    ActiveSheet.ShowAllData
    SourceWs.Range("A1:J" & Range("A" & Rows.Count).End(xlUp).Row).Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlYes
    
    For Each cel In SourceWs.Range("H2:H" & Range("H" & Rows.Count).End(xlUp).Row)
        CollBldg.Add cel.Value, CStr(cel.Value)
    Next
    On Error GoTo 0
    
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    '2)
    For Each Bldg In CollBldg

    For Each cel In SourceWs.Range("G2:G" & Range("G" & Rows.Count).End(xlUp).Row)
        collSite.Add cel.Value, CStr(cel.Value)
    Next
    On Error GoTo 0
    
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    '3)
    For Each Site In CollBldg
    
    '4)
    MyFile = ThisWorkbook.Path & Application.PathSeparator & Site & "-" & Bldg & ".PDF"
    
    '5)
    
    SourceWs.Range("A1:J" & Range("A" & Rows.Count).End(xlUp).Row).AutoFilter Field:=9, Criteria1:=Bldg
    
    LastRow = SourceWs.Cells(1, 1).SpecialCells(xlCellTypeVisible).End(xlDown).Row
    
    SourceWs.Range("A1:J" & LastRow).SpecialCells(xlCellTypeVisible).Copy
    
    '6)
    With ThisWorkbook
        .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = Left(Bldg, 31)
        With ActiveSheet
            .Range("A1").PasteSpecial Paste:=xlPasteFormats
            .Range("A1").PasteSpecial Paste:=xlPasteValues
            With ActiveSheet.PageSetup
                .Zoom = False
                .Orientation = xlLandscape
                .FitToPagesWide = 1
                .FitToPagesTall = 1
            End With
            With ActiveSheet
                    Columns("A:A").Select
                    Selection.ColumnWidth = 7.14
                    Columns("B:B").Select
                    Selection.ColumnWidth = 53.43
                    Columns("C:C").Select
                    Selection.ColumnWidth = 5.86
                    Columns("D:D").Select
                    Selection.ColumnWidth = 6.14
                    Columns("E:E").Select
                    Selection.ColumnWidth = 16.86
                    Columns("F:F").Select
                    Selection.ColumnWidth = 19.71
                    Columns("G:G").Select
                    Selection.ColumnWidth = 8.43
                    Columns("H:H").Select
                    Selection.ColumnWidth = 15.43
                    Columns("I:I").Select
                    Selection.ColumnWidth = 44
                    Columns("J:J").ColumnWidth = 43
                    Columns("C:H").Select
                    Selection.VerticalAlignment = xlCenter
                End With
                    With Selection
                        .WrapText = False
                        .Orientation = 0
                        .AddIndent = False
                        .ShrinkToFit = True
                        .ReadingOrder = xlContext
                        .MergeCells = False
                    End With
            .ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=MyFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
        End With
        
    End With
    
    Next
    Next
    '7)
    On Error Resume Next
    SourceWs.ShowAllData
    On Error GoTo 0
    
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    
End Sub
 
Upvote 0
Hi @Pestomania,
For the issue of illegal characters and parentheses as per your request in post #7, try the following macro modified with the addition of a function that replaces illegal characters and the content inside parentheses. For all other requests, I would kindly ask you to open a new thread
VBA Code:
Option Explicit

Sub Pestomania2()
'https://www.mrexcel.com/board/threads/employee-roster-separation.1265153/

    Dim SourceWs         As Worksheet
    Dim cel         As Range, myRng As Range
    Dim Itm         As Variant
    Dim LastRow     As Long
    Dim collMgr   As New Collection
    Dim MyFile As String
   
    Set SourceWs = ThisWorkbook.Sheets("Sheet1") '<<============ ADAPT Sheet name
       
    On Error Resume Next
    '1)
    For Each cel In SourceWs.Range("K2:K" & Range("K" & Rows.Count).End(xlUp).Row)
        collMgr.Add cel.Value, CStr(cel.Value)
               
    Next
    On Error GoTo 0
   
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    '2)
     
    For Each Itm In collMgr
    '4)
       
    SourceWs.Range("A1:L" & Range("A" & Rows.Count).End(xlUp).Row).AutoFilter Field:=11, Criteria1:=Itm
       
    LastRow = SourceWs.Cells(1, 1).SpecialCells(xlCellTypeVisible).End(xlDown).Row
   
     '3)
    'MyFile = ThisWorkbook.Path & Application.PathSeparator & Itm & ".PDF"
    MyFile = ThisWorkbook.Path & Application.PathSeparator & CleanFileName(Itm) & ".PDF"
   
    SourceWs.Range("A1:L" & LastRow).SpecialCells(xlCellTypeVisible).Copy
   
    '5)
    With ThisWorkbook
        .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = Itm
        With ActiveSheet
            .Range("A1").PasteSpecial Paste:=xlPasteFormats
            .Range("A1").PasteSpecial Paste:=xlPasteValues
            With ActiveSheet.PageSetup
                .Zoom = False
                .Orientation = xlPortrait
                .FitToPagesWide = 1
                .FitToPagesTall = 1
            End With
            .ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=MyFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
        End With
       
    End With
       
    Next
   
    '6)
    On Error Resume Next
    SourceWs.ShowAllData
    On Error GoTo 0
   
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
   
End Sub

Function CleanFileName(Itm As Variant) As String
 
    Dim InvalidChars As String
    InvalidChars = "\/:*?""<>|"
   
    Dim i As Integer
    For i = 1 To Len(InvalidChars)
        Itm = Replace(Itm, Mid(InvalidChars, i, 1), "")
    Next i

       Do While InStr(Itm, "(") > 0 And InStr(Itm, ")") > 0
        Itm = Left(Itm, InStr(Itm, "(") - 1) & Mid(Itm, InStr(Itm, ")") + 1)
    Loop

        Itm = Trim(Itm)
   
    CleanFileName = Itm
End Function
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top