VBA Worksheet Locked for Editing

Thank you for looking at my question,

I am using the code below to insert as column in a worksheet in another location. The insertion works as it should. When I  go to the target worksheet and attempt to open it I am advised that he file is locked for editing.

Please, what do I need to add to the code to leave the file ready for further editing?



Sub btnInventoryComparison_Click()

    Application.DisplayAlerts = False
    On Error Resume Next

    ' Set a string to the path of the Excel File
    'MyExcelFilePath = "C:\PreProject\MyErrorCheck.xlsx"
    strInventoryFile = "C:\WH Inventory\WH Inventory.xlsx"

    ' Open the spreadsheet using the Excel Application Object
    Set ExcelObject = CreateObject("Excel.Application")
    ExcelObject.Workbooks.Open strInventoryFile

    ' Set the Active Worksheet (in this case the first sheet)
    Set objInventorySheet = ExcelObject.ActiveWorkbook.Worksheets("WHInventory1")
    
    ''With objInventorySheet
    ''    .Columns("A:A").Select
    ''End With
    
    With objInventorySheet.Columns("A:A")
        .Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    End With
    
    MsgBox "Column Inserted", vbOKOnly
    
    ExcelObject.ActiveWorkbook.Save
    ExcelObject.ActiveWorkbook.Close

End Sub

Open in new window

Gary CroxfordOperations Support AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

[ fanpages ]IT Services ConsultantCommented:
Hi,

I would suggest adding the following between lines 28 & 30...

ExcelObject.Quit
Set ExcelObject = Nothing

So the final few lines become...

' ---
    ExcelObject.ActiveWorkbook.Save
    ExcelObject.ActiveWorkbook.Close

    ExcelObject.Quit
    Set ExcelObject = Nothing

End Sub
' ----
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ryan ChongCommented:
at the end of ur sub if the task is completed, u may add

ExcelObject.quit
set ExcelObject = nothing

and then try again and see if prob resolved?
0
Gary CroxfordOperations Support AnalystAuthor Commented:
Works perfectly, thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.