我编写了一个宏,用于从一个工作簿中获取选定的工作表,并将它们复制到另一个工作簿,并以新名称保存。我需要重复运行相同的查询,直到创建大约 6 个单独的文件。每个单独的宏都有效,我可以一次调用它们,但它们不会按顺序运行。我相信我知道问题在于我编写的代码不会引用回源工作簿,而且我不知道如何编写代码来做到这一点。
附加的代码是我正在使用的,它可能看起来有点草率——我把几个不同宏的部分放在一起来让它工作。Gqp Master 是创建所有其他工作簿的主工作簿的名称。
Sub Snuth()
'This will prevent the alet from popping up when overwriting graphs, etc
Application.DisplayAlerts = False
Dim FName As String
Dim FPath As String
Dim NewBook As Workbook
Dim strFileName As String
Dim WS As Worksheet
Dim WBk As Workbook
Set WBk = ("Gap Master")
For Each WS In Worksheets
WS.Visible = True
Next
For Each WS In Worksheets
If WS.Range("C4") <> "Snuth, John" Then
WS.Visible = False
End If
If WS.Range("C4") = "Snuth, John" Then
WS.Visible = True
End If
Next WS
FPath = "C:\Users\mmarshall\Documents\GAP\GAP Development"
FName = "Snuth GAP " & Format(Date, "yyyy-mm-dd") & ".xlsx"
Set NewBook = Workbooks.Add
ThisWorkbook.Sheets.Copy Before:=NewBook.Sheets(1)
Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
ActiveWindow.SelectedSheets.Delete
If Dir(FPath & "\" & FName) <> "" Then
MsgBox "File " & FPath & "\" & FName & " already exists"
Else
NewBook.SaveAs Filename:=FPath & "\" & FName
End If
Application.DisplayAlerts = True
End Sub