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

End Sub

Open in new window

Gary CroxfordOperations Support AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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:

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

Set ExcelObject = Nothing

So the final few lines become...

' ---

    Set ExcelObject = Nothing

End Sub
' ----

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 ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
at the end of ur sub if the task is completed, u may add

set ExcelObject = nothing

and then try again and see if prob resolved?
Gary CroxfordOperations Support AnalystAuthor Commented:
Works perfectly, thank you
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.