关闭工作簿保存更改不起作用

我已经构建了以下代码

Sub Merge_File_based()
' Merge files based on Names

Dim AMITRETURN As String
Dim JPRETURN As String

Dim Folderpath As String
Dim counter1 As Integer
Dim counter2 As Integer
Dim Finalrow As Integer

    Dim wb As Workbook: Set wb = ThisWorkbook

    sh = Sheets("First Step").Name

    Finalrow = Sheets(sh).Range("A1000").End(xlUp).Row

  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationmanual
  Application.DisplayAlerts = False



    Folderpath = Fpath & "\"



    For counter1 = 2 To Finalrow Step 2
        counter2 = counter1 + 1


    AMITRETURN = wb.Sheets(sh).Cells(counter1,1)
    JPRETURN = wb.Sheets(sh).Cells(counter2,1)

    'Ensure Workbook has closed before moving on to next line of code
    DoEvents

    Workbooks.Open Filename:=Folderpath & AMITRETURN,UpdateLinks:=0
    Workbooks.Open Filename:=Folderpath & JPRETURN,UpdateLinks:=0


    Windows(JPRETURN).activate

    Sheets(Array("AMIT Tax Return","AMIT Tax Schedule")).Select
    Sheets("AMIT Tax Schedule").activate
    Workbooks(JPRETURN).Sheets(Array("AMIT Tax Return","AMIT Tax Schedule").Copy after:=Workbooks(AMITRETURN).Sheets("AMIT_form")

    'Ensure Workbook has closed before moving on to next line of code
     DoEvents

    Workbooks(AMITRETURN).Close SaveChanges:=True

    'Ensure Workbook has closed before moving on to next line of code
     DoEvents

    Workbooks(JPRETURN).Close SaveChanges:=False

    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.DisplayAlerts = True

    Next

    MsgBox "Task Complete"

End Sub

但是在循环中运行 Workbooks(AMITRETURN).Close SaveChanges:= True 时,它没有保存工作簿,而是询问了您是否要保存工作簿。如果我回答是,则会出现错误dochshare集成无法正常工作等问题。

所以我要保存工作簿时单击“否”,一旦完成宏循环,就可以保存这些文件而没有任何问题。

此外,当我进入宏以查看错误时,它使我可以保存文件而没有任何问题。这是造成问题的唯一循环,知道我在这里错过了什么吗?

solabc 回答:关闭工作簿保存更改不起作用

我相信这可以解决您的问题,您可以在循环中启用.DisplayAlerts,也不需要.Select.Activate
我不确定是否需要您的DoEvents,但还是将它们放在“正确”的位置

Option Explicit
Sub Merge_File_based()
' Merge files based on Names

Dim AMITRETURN As String
Dim JPRETURN As String

Dim Folderpath As String
Dim counter1 As Long
Dim counter2 As Long
Dim Finalrow As Long
Dim sh as Worksheet

Set sh = ThisWorkbook.Sheets("First Step")

Finalrow = sh.Range(sh.Rows.Count,1).End(xlUp).Row

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False

Folderpath = Fpath & "\"

For counter1 = 2 To Finalrow Step 2
    counter2 = counter1 + 1

    AMITRETURN = sh.Cells(counter1,1)
    JPRETURN = sh.Cells(counter2,1)

    Workbooks.Open Filename:=Folderpath & AMITRETURN,UpdateLinks:=0
    Workbooks.Open Filename:=Folderpath & JPRETURN,UpdateLinks:=0

    Workbooks(JPRETURN).Sheets(Array("AMIT Tax Return","AMIT Tax Schedule")).Copy after:=Workbooks(AMITRETURN).Sheets("AMIT_form")

    Workbooks(AMITRETURN).Close SaveChanges:=True

    'Ensure Workbook has closed before moving on to next line of code
    DoEvents

    Workbooks(JPRETURN).Close SaveChanges:=False

    'Ensure Workbook has closed before moving on to next line of code
    DoEvents

Next

Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True

MsgBox "Task Complete"

End Sub
本文链接:https://www.f2er.com/3098344.html

大家都在问