3

嗨,我在 Excel 表上使用 VBScript 编写了一些代码,如下所示。现在,每当脚本完成处理时,它都会提示用户使用它Save。但我不想要这个,而是希望它在没有提示的情况下自动保存。

代码

    Option Explicit

    Dim objExcel1,strPathExcel1,objSheet1,objSheet5

    Set objExcel1 = CreateObject("Excel.Application")'Object for Condition Dump
    strPathExcel1 = "D:\VA\GE_Wing_To_Wing_Report.xlsx"
    objExcel1.Workbooks.Open strPathExcel1
    Set objSheet1 = objExcel1.ActiveWorkbook.Worksheets(1)
    Set objSheet5 = objExcel1.ActiveWorkbook.Worksheets(5)

    '=====================================================================================
    'Here Bad sheet will be copied by the data from First sheet master data sheet
    '=====================================================================================
       ParentPIDFromMasterSheet objSheet1,objSheet5

    '=====================================================================================
    'Here Bad sheet will be copied by the data from First sheet master data sheet
    '=====================================================================================
       BadDataSelectionDel objSheet5

    '=======================
    objExcel1.ActiveWorkbook.SaveAs strPathExcel1
    objExcel1.Workbooks.close
    objExcel1.Application.Quit
    '======================
4

2 回答 2

4

未经测试(试试这个)

您需要设置工作簿,然后在保存后将其关闭。此外,在使用后在代码末尾清理对象也是一个好习惯。:)

Option Explicit

Dim objExcel1, objWB, strPathExcel1, objSheet1, objSheet5

Set objExcel1 = CreateObject("Excel.Application") 'Object for Condition Dump
strPathExcel1 = "D:\VA\GE_Wing_To_Wing_Report.xlsx"
Set objWB = objExcel1.Workbooks.Open(strPathExcel1)
Set objSheet1 = objWB.Worksheets(1)
Set objSheet5 = objWB.Worksheets(5)

'=====================================================================================
'Here Bad sheet will be copied by the data from First sheet master data sheet
'=====================================================================================
   ParentPIDFromMasterSheet objSheet1, objSheet5

'=====================================================================================
'Here Bad sheet will be copied by the data from First sheet master data sheet
'=====================================================================================
   BadDataSelectionDel objSheet5

'=======================
objWB.Save
objWB.Close
objExcel1.Quit

'~~> Cleanup
Set objSheet1 = Nothing
Set objSheet5 = Nothing
Set objWB = Nothing
Set objExcel1 = Nothing
'======================
于 2012-12-26T07:14:01.433 回答
0

您只需要在保存文件之前添加这行代码:

objExcel1.Application.DisplayAlerts = False 'Prevents prompts from appearing

我试过了,它奏效了。

于 2020-03-22T13:03:56.297 回答