使用VBA复制工作簿和过滤



我正在尝试保存工作簿的副本,然后在新保存的工作簿副本中,过滤一些项目,关闭它,然后使用原始工作簿(用于一系列项目)重复该过程。

目前,我得到一个错误("编译错误:命名参数未找到"在这里,最重要的是我怀疑我的方法是否是最有效的方法。

想知道这个文档有什么问题以及如何改进它?

宏:

Sub CECL_Scrub()
'
' CECL_Scrub Macro
'
Application.ScreenUpdating = False
Application.Calculation = xlManual

Dim cl As Range, rng As Range
'In Input sheet, select country list for filter
With ThisWorkbook
Set rng = .Sheets("Input").Range("A1:A2")
End With

'Create folders for countries
Dim i As Integer
For i = 13 To 14
' Checks if folder already exists
If Len(Dir("C:UsersmeMacroTestOutput" & Range("A" & i), vbDirectory)) = 0 Then
MkDir "C:UsersmeMacroTestOutput" & Range("A" & i)
End If
Next i
'Define variables and save as new workbook
For Each cl In rng
Dim wb As Workbook
Set wb = ThisWorkbook
wb.Activate

' Save separate file in location of choice

Dim Path1 As String
Dim Path2 As String
Dim Path3 As String
Dim myfilename As String

Path1 = "C:UsersmeMacroTestOutput"
Path2 = cl.Value
Path3 = " - Test"

myfilename = Path1 & Path2 & "" & Path2 & Path3

ActiveWorkbook.SaveCopyAs Filename:=myfilename, FileFormat:=xlOpenXMLWorkbook

Dim newWorkbook As Workbook
Set newWorkbook = Workbooks.Open(myfilename)

'Filter of first sheet
Worksheets("Summary").Activate
ActiveSheet.Range("$A$6:$W27").AutoFilter Field:=1, Criteria1:=cl.Value

Next cl

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

编辑:

以上代码根据问题

的答案进行了更正

为什么这个代码不能工作是因为语法不正确(文件名中使用了错误的斜杠)。此代码已在上面进行了编辑和更正。应该是有价值的任何人正在寻找这个在未来

相关内容

  • 没有找到相关文章

最新更新