CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    May 2009
    Boise Idaho

    [RESOLVED] VB6: Excel won't close after sorting

    STARTED NEW THREAD************************

    I am having an issue where Excel doesn't close when it should. Here is a very simplified version of my code. If I remove the code identified as "Problem Area" then Excel will close as expected at the end of this procedure. If the problem area code is included then Excel will not close. Somehow it seems to be related to the Range still being active which keeps Excel open. Any ideas?
    Dim oExcel As Excel.Application
    Dim oWbk As Excel.Workbook
    Dim oSht As Excel.Worksheet
    Dim oRng As Excel.Range
    Set oExcel = New Excel.Application
    uploadpath = "C:\Excel File.xls"
    Set oWbk = oExcel.Workbooks.Open(uploadpath)
    Set oRng = oWbk.Worksheets(1).Range("A8").CurrentRegion
    K1Rng = "A8"
    K2Rng = "E8"
    '*******************************PROBLEM AREA 
    Selection.Sort Key1:=Range(K1Rng), Order1:=xlAscending, Key2:=Range(K2Rng) _
    , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
    False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
    '*******************************END OF PROBLEM AREA
    Set oRng = Nothing
    Set oSht = Nothing
    oWbk.Close SaveChanges:=False
    Set oWbk = Nothing
    Set oExcel = Nothing
    Note: This code works. It opens Excel, loads the spreadsheet, performs the correct sorting on columns A and E. The only thing it doesnt do is close Excel when it is done. If the lines marked above as "Problem Area" are removed, then Excel does close properly. Something in those lines keeps Excel open.
    Last edited by Shuja Ali; August 28th, 2009 at 03:32 PM. Reason: code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.