我正在编写代码,以将工作表的sheet8复制到工作表sheet1的单元格P2中的名称为xyz.xlsx的代码中,并将其粘贴到另一个文件夹folderX中。到目前为止,我还没有编写粘贴代码。
代码段:
Sub file_name()
returnVal = Sheet1.Cells(2, 16).Value
Dim FPath As String
FPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheet8.Copy
Application.ActiveWorkbook.SaveAs FileName:=returnVal & “.xlsx”
Application.ActiveWorkbook.Close False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
我对宏非常陌生。
我的假设:
P2
的单元格Sheet1
具有类似xyz.xlsx
的值。FPath
的正确值。Sheet8
代码:
这是您要尝试的吗?
Option Explicit
Sub file_name()
Dim ScrnUpdating As Boolean
Dim DispAlerts As Boolean
On Error GoTo Whoa
With Application
'~~> Store existing settings before changing
ScrnUpdating = .ScreenUpdating
DispAlerts = .DisplayAlerts
.ScreenUpdating = False
.DisplayAlerts = False
End With
Dim returnVal As String
returnVal = Sheet1.Cells(2, 16).Value
Dim FPath As String
FPath = ThisWorkbook.Path
Sheet8.Copy
ActiveWorkbook.SaveAs Filename:=FPath & "\" & returnVal, FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close False
LetsContinue:
With Application
.ScreenUpdating = ScrnUpdating
.DisplayAlerts = DispAlerts
End With
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub