运行时错误'424'所需对象[Macros excel]

问题描述 投票:0回答:1

我正在编写代码,以将工作表的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

我对宏非常陌生。

excel vba excel-vba macros
1个回答
0
投票

我的假设:

  1. [我假设P2的单元格Sheet1具有类似xyz.xlsx的值。
  2. 我假设您正在运行代码的工作簿已保存(至少一次以获取FPath的正确值。
  3. 我假设您有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
© www.soinside.com 2019 - 2024. All rights reserved.