打开新文件并退出而不保存

时间:2018-02-20 13:18:50

标签: excel vba excel-vba

我有这个子程序,我想在不保存AAAAMMGG_A.xls的情况下退出

由于

Sub A_STEP3()
On Error GoTo Err_A_STEP3

    Dim rng As Range
    Dim rng2 As Range
    Dim c As Range
    Dim d As Range
    Dim righedacopiare As Integer

   'open the file AAAAMMGG_A.xls di Flutitoli e copio le informazioni


    Workbooks.Open (MyPath2 & nomefile2(8))
    Range("A2:W2").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy

    ActiveWorkbook.Close SaveChanges:=False

    Windows("modello.xlsm").Activate
    Sheets("A").Select
    Range("C3").Select
    ActiveSheet.Paste

    righedacopiare = 1000

  ' formatto le stringhe in numero
    Set rng = Worksheets("A").Range("U3:V" & righedacopiare)

    For Each c In rng
        c.Value = c.Value * 1
        c.HorizontalAlignment = xlRight
    Next

    Set rng2 = Worksheets("A").Range("C3:F" & righedacopiare)

    For Each d In rng2
        d.Value = "'" & d.Value
    Next

Exit_A_STEP3:
    Exit Sub

Err_A_STEP3:
    MsgBox "A_STEP3 -> " & Err.Number & ": " & Err.Description
    ERROREBLOCCO = True
    Resume Exit_A_STEP3

End Sub

1 个答案:

答案 0 :(得分:1)

首先,确保您在变量中捕获新工作簿,以便指定要关闭的工作簿:

Dim wb_A As Workbook
Set wb_A = Workbooks.Open MyPath2 & nomefile2(8)
'Do stuff here
wb_A.Close SaveChanges:=False

其次,如果您不希望保存,则可以以只读模式打开工作簿:

Dim wb_A As Workbook
Set wb_A = Workbooks.Open MyPath2 & nomefile2(8), ReadOnly:=True
'Do stuff here
wb_A.Close SaveChanges:=False

使用ActiveWorkbook很麻烦 - 请尽量使用完整的对象路径(例如Worksheet_Name.Range(..)而不是Range(..)ActiveSheet.Range(..),以避免意外活动