Results 1 to 6 of 6

Thread: (SOLVED) Can't Close Excel Aplication (Range Issue)

Hybrid View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Tijuana,Mexico
    Posts
    109

    Resolved (SOLVED) Can't Close Excel Aplication (Range Issue)

    Hi... I have been looking around for this problem, I already found where my problem is but I don't know how to solved.

    I'm sure that my problem has to be with Range (I got this from a VBA but I don't know how to translate it to VB).

    I'm trying to copy some information from Excel to a Flexgrid, but I have to sort this information before... after that I'm closing Excel but that's not happening, in the task manager I can see the process still running and closing only when I close my program.

    vb Code:
    1. Public oApp As Excel.Application
    2. Public oWB As Excel.Workbook
    3. Public osH As Excel.Worksheet
    4. Public oRng As Excel.Range
    5.  
    6.    Set oApp = New Excel.Application
    7.    oApp.Visible = False
    8.    Set oWB = oApp.Workbooks.Open(Archivo)
    9.    Set osH = oWB.Sheets(oWB.Sheets.Count)
    10.    Set oRng = osH.Range("A1")
    11. '*** Now I don't know how to continue...
    12.    oApp.ActiveWorkbook.ActiveSheet.Range(oApp.Selection, oApp.Selection.End(xlDown)).Select
    13.    oApp.Range(oApp.Selection, oApp.Selection.End(xlDown)).Select
    14.    CalcRows = oApp.Selection.Rows.Count
    15.    Clipboard.Clear
    16.    oApp.ActiveWorkbook.ActiveSheet.Range("A1").Select
    17.    oApp.ActiveWorkbook.ActiveSheet.Range(oApp.Selection, oApp.Selection.End(xlDown)).Select
    18.    oApp.ActiveWorkbook.ActiveSheet.Range(oApp.Selection, oApp.Selection.End(xlToRight)).Select
    19.    oApp.Selection.Sort Key1:=Range(Chr(Asc("A") + RosGL - 1) & "2"), Order1:=xlAscending, _
    20.         Key2:=Range(Chr(Asc("A") + RosCoAst - 1) & "2"), Order2:=xlAscending, _
    21.         Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    22.         DataOption1:=xlSortTextAsNumbers, DataOption2:=xlSortTextAsNumbers
    23.    oApp.Selection.Copy
    24.    FG2.Clear
    25.    FG2.Rows = CalcRows
    26.    FG2.Cols = RosCols
    27.    FG2.Redraw = False
    28.    FG2.Row = 0
    29.    FG2.Col = 0
    30.    FG2.ColSel = FG2.Cols - 1
    31.    FG2.RowSel = FG2.Rows - 1
    32.    FG2.Clip = Replace(Clipboard.GetText, vbNewLine, vbCr)
    33.    FG2.Col = 1
    34.    oApp.DisplayAlerts = False
    35.    oWB.Close SaveChanges:=False    '(App.Path + "\Test1.xls")          '"D:\Test.xls"
    36.    Set oWB = Nothing
    37.    LogError = "Archivo cerrado"
    38.    oApp.Quit
    39.    Set oApp = Nothing
    Last edited by G-Hawk; Feb 27th, 2007 at 05:49 PM. Reason: Change VB tags

  2. #2
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Can't Close Excel Aplication (Range Issue)

    Get rid of those ActiveWorkbooks and ActiveSheets - they only apply when you specifically want to use the active book/sheet. Replace them with specific directions as to what object you are using, eg. oWB to use the workbook, osH to use that specific sheet, oWB.worksheets(1) etc.

    Also, get rid of the Selections and refer directly to the range. Why select a range and then do something to the current selection if you already know what the Range is? Just do it to the Range itself!


    This is the one issue with using VBA macros to record your VBA - Excel interprets your actions using the ActiveBook / Sheet and the Selection object because that is what you are using; however for generic coding purposes it will not do.


    zaza
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Can't Close Excel Aplication (Range Issue)

    Indeed, as an example "oApp.ActiveWorkbook" should be replaced by "oWB", and "oApp.ActiveWorkbook.ActiveSheet" should be replaced by "osH".

    You should remove the Selection too as zaza mentioned.. but I'm not entirely sure how in this case as you are extending the selection.

    The actual problem you have noticed is on the Sort line.. which is caused by the fact you have specified just Range instead of sheetobject.Range


    By the way, VBCode tags have changed now - you need to use this instead: [highlight=vb] code here [/highlight]

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Tijuana,Mexico
    Posts
    109

    Re: Can't Close Excel Aplication (Range Issue)

    Tnxs zaza & si_the_geek for your answers... anyway I'm still having the same problem :

    @zaza:
    How can I get rid off this:

    oApp.ActiveWorkbook.ActiveSheet.Range(oApp.Selection, oApp.Selection.End(xlDown)).Select

    I'm using this because with this statement I would know the number of columns I have to set up the FG. Now, if I don't use select then hou can I sort or copy the information

    @si_the_geek:

    I tried your suggestion but I still can't close Excel, anyway that should have done the job because I use the same code in other function but without the sort statement and I have no problems closing Excel.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Can't Close Excel Aplication (Range Issue)

    I've just had another look, and I think lines 16 to 18 can be replaced by this: osH.UsedRange.Select

    ..and to eliminate the Select/Selection, merge it with line 19 like this (also includes 2 corrections for the Range issue):
    vb Code:
    1. osH.UsedRange.Sort Key1:=osH.Range(Chr(Asc("A") + RosGL - 1) & "2"), Order1:=xlAscending, _
    2.         Key2:=osH.Range(Chr(Asc("A") + RosCoAst - 1) & "2"), Order2:=xlAscending, _
    3.         Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    4.         DataOption1:=xlSortTextAsNumbers, DataOption2:=xlSortTextAsNumbers

    If that doesn't solve it, show us your current code with the corrections mentioned above (using oWB etc).

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Tijuana,Mexico
    Posts
    109

    Re: Can't Close Excel Aplication (Range Issue)

    si_the_geek... thanks I already got the idea.. and now is working perfectly

    Regards!

Posting Permissions

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



Click Here to Expand Forum to Full Width