我有一个Excel宏,它复制特定工作表中的所有信息并将其复制到新工作簿中。代码如下:
Option Explicit
Sub TwoSheetsAndYourOut()
Dim NewName As String
Dim nm As Name
Dim ws As Worksheet
If MsgBox("Copy specific sheets to a new workbook" & vbCr & _
"New sheets will be pasted as values, named ranges removed" _
, vbYesNo, "New Copy") = vbNo Then Exit Sub
With Application
.ScreenUpdating = False
On Error GoTo ErrCatcher
Sheets("Input").Copy
On Error GoTo 0
For Each ws In ActiveWorkbook.Worksheets
ws.Cells.Copy
ws.[A1].PasteSpecial Paste:=xlValues
ws.Cells.Hyperlinks.Delete
Application.CutCopyMode = False
Cells(1, 1).Select
ws.Activate
Next ws
Cells(1, 1).Select
For Each nm In ActiveWorkbook.Names
nm.Delete
Next nm
NewName = InputBox("Please specify the name of your new workbook", "New Copy", "input")
Dim sPath As String
sPath = ThisWorkbook.Path
ActiveWorkbook.SaveCopyAs sPath & NewName + ".xls"
ActiveWorkbook.Close SaveChanges:=False
.ScreenUpdating = True
End With
Exit Sub
ErrCatcher:
MsgBox "Specified sheets do not exist within this workbook"
End Sub
但是,它不会将新的Excel文件保存在正确的目录中。原始Excel文件(包含宏的文件)位于以下目录中(在Mac上):
/应用/ WORDNET /项目
但是,每次运行宏时,它都会将新的Excel文件保存在WORDNET文件夹中,而不是PROJECTS文件夹中。
如何修改代码以便将其保存在正确的位置?为什么它不与原始Excel文件保存在同一目录中?
答案 0 :(得分:1)
sPath = ThisWorkbook.Path
sPath
是最后没有分隔符的路径(至少在Windows上),因此您必须在脚本中添加一个分隔符。在您的情况下,文件将保存到/Applications/WORDNET
,名称为"PROJECTS" & NewName
Unix的:
ActiveWorkbook.SaveCopyAs sPath & "/" & NewName + ".xls"
视窗:
ActiveWorkbook.SaveCopyAs sPath & "\" & NewName + ".xls"