防止在vba中运行自动打开和工作簿打开事件

时间:2015-08-10 13:48:32

标签: vba excel-vba events excel

我想在我的vba代码中打开一个XLSM文件,但是当我这样做时,所有的VBA脚本或事件都会打开。所以我只想打开,取2个单元格并关闭文件。有人能帮助我吗?

Sub meta1()
    Dim xl As New Excel.Application
    Dim xlw As Excel.Workbook
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
        Data1 = InputBox("Escreve a data do nome do arquivo da seguinte forma: 2015-07-26")
        var1 = "blablabla" + Data1 + ".xlsm"
        var2 = "Blablabla" + Data1 + ".xlsm"

        Set xlw = xl.Workbooks.Open(var1)
        xlw.Sheets("APOIO").Select

        cont1 = Workbooks(var2).Worksheets("APOIO").Cells(61, Columns.Count).End(xlToLeft).Column
        cont2 = Workbooks(var2).Worksheets("APOIO").Cells(92, Columns.Count).End(xlToLeft).Column
        cont3 = Workbooks(var2).Worksheets("APOIO").Cells(26, Columns.Count).End(xlToLeft).Column
        xlw.Close False
        Set xlw = Nothing
        Set xl = Nothing
        cmgeral1 = Cells(61, cont1)
        conversão = Cells(92, cont2)
        derivacao = Cells(26, cont3)

1 个答案:

答案 0 :(得分:0)

测试:

Option Explicit

Sub preventEvents()
    Application.EnableEvents = False
    Workbooks.Open "FileName.xlsm"
    Application.EnableEvents = True
End Sub