Hello, I am looking for some code that will save a tab of one file as multiple files in a specific location. I have also posted this question here and I have basically half of a solution but am having trouble getting them to understand what I am after. I have a sample file with two tabs. Sheet1 contains a listing of numbers, names and departments as well as a key off to the side in columns L and M. They key lists all the departments there could possibly be(column L) and a brief description of them in column M. Sorry I can't usexl2bb to do this but company download policy is preventing it so I am using a screenshot. See pic below:
Sheet2 basically serves as a template that would be printed. It has a drop-down menu from data validation in cell B8 which is taken from the Key in sheet1. Here is a pic of Sheet2
B7 just has a simple lookup for the dept description base don what is chosen in B8. Changing selections in B8 will also change names that are populated in row 11 and onwards:
What I am looking to do is have a macro that will select each of the choices in the menu in B8 of this sheet and then save a copy of sheet2 as a specific name in a specific location. The location can simply be a folder called sample on the desktop(I will alter the path to what I truly need but for simplicity sake this can be used) The file name will be specific to what is in cells B7 and B8. So if we look at the second screenshot where I have "Test1" selected, the macro would save Sheet2 as a new file in that sample folder called "Test1 - 1st Shift" ("B8 - B7"). It would then select all the other departments in that list one by one saving each one as it's own file in the sample folder. So in my sample sheet, I have a total of 4 possible departments(There are more on the actual sheet but just keeping it simpler here) That will mean that 4 different files of sheet 2 get created in the folder and each one specific to the selection made in B8. The code I was given to help that is doing half of what I need and is as follows if it will help as a guiding point:
This code is functioning correct in that it is saving correct amount of files to the specified location. It however is saving the wrong tab. It is saving Sheet1 not sheet2 which is what I have tried explaining. I am not sure how VBA handles selecting menu choices from a data validation menu, but if it is easier for coding, the selections could simply be copied from Sheet1 in column L and then pasted into B8 of Sheet2 and then have Sheet2 saved to the folder. I hope this made sense. Please let me know if this is doable and if any questions.
Sheet2 basically serves as a template that would be printed. It has a drop-down menu from data validation in cell B8 which is taken from the Key in sheet1. Here is a pic of Sheet2
B7 just has a simple lookup for the dept description base don what is chosen in B8. Changing selections in B8 will also change names that are populated in row 11 and onwards:
What I am looking to do is have a macro that will select each of the choices in the menu in B8 of this sheet and then save a copy of sheet2 as a specific name in a specific location. The location can simply be a folder called sample on the desktop(I will alter the path to what I truly need but for simplicity sake this can be used) The file name will be specific to what is in cells B7 and B8. So if we look at the second screenshot where I have "Test1" selected, the macro would save Sheet2 as a new file in that sample folder called "Test1 - 1st Shift" ("B8 - B7"). It would then select all the other departments in that list one by one saving each one as it's own file in the sample folder. So in my sample sheet, I have a total of 4 possible departments(There are more on the actual sheet but just keeping it simpler here) That will mean that 4 different files of sheet 2 get created in the folder and each one specific to the selection made in B8. The code I was given to help that is doing half of what I need and is as follows if it will help as a guiding point:
VBA Code:
Option Explicit
Sub PitchNinja_v2()
Dim i As Long, j As Long, jj As Long
Dim wsSource As Worksheet: Set wsSource = ThisWorkbook.Worksheets("Sheet1")
Dim FolderPath As String: FolderPath = "D:\Sample\"
Application.ScreenUpdating = False
If Len(Dir(FolderPath, vbDirectory)) = 0 Then
MkDir FolderPath
End If
For i = 2 To wsSource.Cells(wsSource.Rows.Count, 3).End(xlUp).Row
Dim iDept As String: iDept = wsSource.Cells(i, 3).Value
Dim NewWorkbook As Workbook: Set NewWorkbook = Workbooks.Add
Dim NewBookRow As Long: NewBookRow = 1
wsSource.Cells(1, 1).Resize(, 2).Copy NewWorkbook.Worksheets(1).Cells(NewBookRow, 1)
For j = 2 To wsSource.Cells(wsSource.Rows.Count, 1).End(xlUp).Row
If wsSource.Cells(j, 3).Value = iDept Then
For jj = 2 To wsSource.Cells(wsSource.Rows.Count, 13).End(xlUp).Row
Dim iRng As Range: Set iRng = wsSource.Columns("L:L").Find(What:=iDept, LookIn:=xlValues, LookAt:=xlWhole)
If Not iRng Is Nothing Then
Dim iKey As String: iKey = iRng.Offset(0, 1).Value
End If
Next jj
wsSource.Cells(j, 1).Resize(, 2).Copy NewWorkbook.Worksheets(1).Cells(NewBookRow + 1, 1)
NewWorkbook.Worksheets(1).Columns("A:B").AutoFit
NewBookRow = NewBookRow + 1
End If
Next j
Dim FileName As String: FileName = FolderPath & iDept & " - " & iKey & ".xlsx"
Application.DisplayAlerts = False
NewWorkbook.SaveAs FileName, FileFormat:=51
Application.DisplayAlerts = True
NewWorkbook.Close SaveChanges:=False
Next i
Application.ScreenUpdating = True
MsgBox "Done!", vbInformation
End Sub
This code is functioning correct in that it is saving correct amount of files to the specified location. It however is saving the wrong tab. It is saving Sheet1 not sheet2 which is what I have tried explaining. I am not sure how VBA handles selecting menu choices from a data validation menu, but if it is easier for coding, the selections could simply be copied from Sheet1 in column L and then pasted into B8 of Sheet2 and then have Sheet2 saved to the folder. I hope this made sense. Please let me know if this is doable and if any questions.