我认为你正在寻找类似于下面的东西(改编自copying-dynamic-rows-into-new-workbook-and-save-it)。
Option Explicit
Sub grabber()
Dim thisWb As Workbook: Set thisWb = ThisWorkbook
Dim thisWs As Worksheet: Set thisWs = thisWb.Worksheets("dd") 'replace with relevant name
Dim newBook As Workbook
Dim newws As Worksheet
Dim pathToNewWb As String
Dim uKeys
Dim currentPath, columnWithKey, numCols, numRows, uKey, dataStartRow, columnKeyName
'nobody likes flickering screens
Application.ScreenUpdating = False
'remove any filter applied to the data
thisWs.AutoFilterMode = False
'get the path of the workbook folder
currentPath = Application.ThisWorkbook.Path
'Set the stage
'###Hardcode###
columnKeyName = "Facility" 'name of the column with the facility values
dataStartRow = 4 'this is a pure guess, correct as relevenat. Use the header row index
pathToNewWb = currentPath & "/Business Plans.xlsx" ' where to put the new excel, if you want a saveas prompt you should google "Application.FileDialog(msoFileDialogSaveAs)"
uKeys = Range("Facilities").Value
'###Hardcode End###
columnWithKey = thisWs.Range(dataStartRow & ":" & dataStartRow).Find(what:=columnKeyName, LookIn:=xlValues).Column
numCols = thisWs.UsedRange.Columns.Count
'extract the index of the last used row in the worksheet
numRows = thisWs.UsedRange.Rows.Count
'create the new workbook
Set newBook = Workbooks.Add
'loop the facilities, and do the work
For Each uKey In uKeys
'Filter the keys column for a unique key
thisWs.Range(thisWs.Cells(dataStartRow, 1), thisWs.Cells(numRows, numCols)).AutoFilter field:=columnWithKey, Criteria1:=uKey
'copy the sheet
thisWs.UsedRange.Copy
'Create a new ws for the facility, and paste as values
Set newws = newBook.Worksheets.Add
With newws
.Name = uKey 'I assume the name of the facility is the relevant sheet name
.Range("A1").PasteSpecial xlPasteValues
End With
'remove autofilter (paranoid parrot)
thisWs.AutoFilterMode = False
Next uKey
'save the new workbook
newBook.SaveAs pathToNewWb
newBook.Close
End Sub
编辑:
由于我还没有看到你的数据,如果它需要一些修改,我不会感到惊讶。
首先,我尝试对包含数据(### Hardcode ###位)的工作表“dd”的范围进行“构建”,定义输出的路径,并确定可以过滤的列对应于命名范围“设施”的值。
我检索命名范围“Facilities”(进入uKeys)的值,并创建输出工作簿(newBook)。然后我们遍历for循环中uKeys的每个值(uKey)。在循环中,我为uKey应用了一个自动过滤器。过滤后,在newBook中创建工作表(newWs),并将过滤的工作表“dd”复制粘贴到newWs中。然后关闭自动过滤器,工作表“dd”返回到未过滤状态。
最后,我们将newBook保存到所需位置,然后关闭它。
如果我们能够获得一些您正在尝试完成的示例数据,那对我们能够帮助您是非常有帮助的。此外,还有几行代码可以通过移动它们轻松删除,在编写代码时尽量不要重复。 – Histerical
我有点困惑。您想为组合框中的每个选定项目创建新的工作簿/工作表,还是以PDF格式导出? – NuWin
@NuWin无我想要的是我的下拉菜单中的每个选择我想为它创建一个选项卡,但我希望所有数据都是值。当前每个下拉触发查找公式用于数据操作。我最好希望在新工作簿中新创建的选项卡,但同样的工作簿也可以。 – user3666237