3

Hi I wrote some code using VBScript on Excel sheet as below. Now everytime when the Script is done its processing it is prompting the user to Save it.But I don't want this,rather I want it to save it automatically without prompt.

CODE

    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
    '======================
1

2 Answers 2

4

UNTESTED (Try this)

You need to set your workbook and then close it after saving it. Also it is a good practice to clean up your objects at the end of your code after use. :)

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
'======================
Sign up to request clarification or add additional context in comments.

3 Comments

hi Siddharth, why is it good practice to close yoru objects? what are the implications? :) #generalcuriosity
Very Good Question :) If you do a google search you will get your answer. This has been widely covered
To save with a specific filename, you can use objWB.SaveAs "C:\path\to\file\filename.xlsx" (See Open an Excel file and save as .XLS)
0

You only need to add this line of code before saving your file :

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

I tried it and it worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.