Results 1 to 18 of 18

Thread: VB 2 Excel drives me ever crazier [RESOLVED]

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    VB 2 Excel drives me ever crazier [RESOLVED]

    I'm not the first one in this forum who helplessly tries to have Excel close after a VB app has transferred data to it. I've searched the forum and found posts galore on this issue and have finally written a subroutine based on what I've collected. This subroutine leaves Excel in the Task Manager list (=> ctrl/alt/del). However, if the lines

    .Rows("2:2").Select
    Selection.Insert Shift:=xlDown

    are removed, then it works and Excel is correctly unloaded and appears no longer in the task manager list.

    I'm more than puzzled and about to believe that my pc is haunted or jinxed or something. Any help will be appreciated.

    VB Code:
    1. Sub JustDoIt()
    2.  
    3.     Dim xlApp As Excel.Application
    4.     Dim xlBook As Excel.Workbook
    5.     Dim xlWS As Excel.Worksheet
    6.    
    7.     On Error Resume Next
    8.  
    9.     Set xlApp = GetObject(, "Excel.Application")
    10.  
    11.     If Err.Number = 429 Then
    12.         ' Excel is NOT running, so create a new instance
    13.         Set xlApp = CreateObject("Excel.Application")
    14.     End If
    15.    
    16.     Set xlBook = xlApp.Workbooks.Open(App.Path & "\TestFile.xls")
    17.  
    18.     xlApp.Visible = False
    19.     xlApp.UserControl = True
    20.     DoEvents
    21.  
    22.     Set xlWS = xlBook.Worksheets(1)
    23.  
    24.     With xlWS
    25.         .Select
    26.         .Rows("2:2").Select
    27.         Selection.Insert Shift:=xlDown
    28.         For n = 1 To 100
    29.             .Cells(n, 2).Value = n
    30.         Next
    31.     End With
    32.  
    33.     Set xlWS = Nothing
    34.     xlApp.DisplayAlerts = False
    35.     xlBook.Close SaveChanges:=True
    36.     Set xlBook = Nothing
    37.     xlApp.Quit
    38.     Set xlApp = Nothing
    39.  
    40. End Sub
    Last edited by krtxmrtz; May 26th, 2004 at 02:15 AM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    What is Selection? (Selection.Insert Shift:=xlDown)
    If you want to use the Excel Selection object you need to specify that. (ie: xlApp.Selection.Insert Shift:=xlDown)

    Whenever possible you should not use the Selection object as the user can alter it while your program is running - just use whatever objects you have selected instead, eg:
    VB Code:
    1. With xlWS
    2.         .Rows("2:2").Insert Shift:=xlDown
    3.         For n = 1 To 100
    4.             .Cells(n, 2).Value = n
    5.         Next
    6.     End With

  3. #3

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by si_the_geek
    What is Selection? (Selection.Insert Shift:=xlDown)
    If you want to use the Excel Selection object you need to specify that. (ie: xlApp.Selection.Insert Shift:=xlDown)
    The reason for that is I directly pasted some code from an Excel macro.
    This though does not take care of the problem, Excel is still bewitching the computer memory...
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    You cant just paste verbatim, you need to precede Excel specific objects with an appropriate reference (as Selection is a member of Application, it needs an application object, in this case xlApp).


    Other issues that I can see:

    Isn't xlApp.UserControl read only? I thought you couldnt change that, so it would be best to check, and remove it if it is read only.
    (I dont use it in any of my programs, and I dont have this issue).

    You have "On Error Resume Next". You should NOT use this if there are any unknown issues (such as you have), and should avoid it at all other times unless you dont mind if the entire code after it fails. Try to use proper error handling instead.

  5. #5

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by si_the_geek
    Isn't xlApp.UserControl read only? I thought you couldnt change that, so it would be best to check, and remove it if it is read only.
    (I dont use it in any of my programs, and I dont have this issue).

    You have "On Error Resume Next". You should NOT use this if there are any unknown issues (such as you have), and should avoid it at all other times unless you dont mind if the entire code after it fails. Try to use proper error handling instead.
    I don't really know what usercontrol is about. I posted about it yesterday in this link
    I've removed the references to UserControl but this still does not solve the problem.

    The reason for On Error Resume Next is that after

    Set xlApp = GetObject(, "Excel.Application")

    an error (429) was expected if Excel was not running...
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Ok, but it needs to be turned off again after the error, or you get situations like this where you dont know if an error is occuring or not.

    Ideally you should have an error handler, but as you havent the easiest way to return to normal error handling is the "Goto 0", eg:

    VB Code:
    1. On Error Resume Next
    2.  
    3.     Set xlApp = GetObject(, "Excel.Application")
    4.  
    5.     If Err.Number = 429 Then
    6.         ' Excel is NOT running, so create a new instance
    7.         Set xlApp = CreateObject("Excel.Application")
    8.     End If
    9.     On Error Goto 0   'this [b]should[/b] be to an error handler, rather than 0

  7. #7

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by si_the_geek
    Ok, but it needs to be turned off again after the error, or you get situations like this where you dont know if an error is occuring or not.
    [/Highlight]
    You're right, of course. But I'm not going to write the error handling part of the subroutine until I'm sure it works. If I can't solve this issue about Excel not vanishing from memory, then maybe I'll rewrite it afresh saving all data separated by tabs in a text file. It won't be too convenient but this application is for my personal use and I could make do with it for now.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Just use the "On Error Goto 0" as i posted above, it will make the rest of the code run without ignoring errors (I didnt make that very clear before ).

    Hopefully then you will see what is causing the problem.

    I'm not going to write the error handling part of the subroutine until I'm sure it works
    You already have error handling of a sort, it's just the opposite way to what you need for development work. Besides, a proper error handler is much more useful in development than not having one.

  9. #9

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by si_the_geek
    Just use the "On Error Goto 0" as i posted above, it will make the rest of the code run without ignoring errors (I didnt make that very clear before ).

    Hopefully then you will see what is causing the problem.


    You already have error handling of a sort, it's just the opposite way to what you need for development work. Besides, a proper error handler is much more useful in development than not having one.
    I must admit what you say makes a lot of sense. I have followed your advice and have found error 1004 about the "select" property, still caused by the line

    .Rows("2:2").Insert Shift:=xlDown

    I wonder what the correct way is to insert a line at row #2. The idea is that the most recent data are always stored at the top of the sheet.

    Btw, whether or not the code can be fixed, I've already learned something today !
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

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

    Unfortunately error 1004 is a generic one, it has many different messages/causes/solutions. It might help if you tell us the exact message it shows.

    I've just tried it out, and this works for me:

    xlWS.Rows("2:2").Insert Shift:=xlDown
    or:
    xlWS.Rows("2:2").Insert Shift:=-4121 '(xlDown = -4121)

    It might help to run this line of code just before it:
    xlWS.Activate



    (by the way, to insert 100 rows it would be "2:101" instead of "2:2")

  11. #11

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by si_the_geek
    good stuff

    Unfortunately error 1004 is a generic one, it has many different messages/causes/solutions. It might help if you tell us the exact message it shows.

    I've just tried it out, and this works for me:

    xlWS.Rows("2:2").Insert Shift:=xlDown
    or:
    xlWS.Rows("2:2").Insert Shift:=-4121 '(xlDown = -4121)

    It might help to run this line of code just before it:
    xlWS.Activate



    (by the way, to insert 100 rows it would be "2:101" instead of "2:2")
    At the moment I'm siting in front of another computer where VB is not installed so I'll try out your suggestions at a later time. I'll post the exact text later (the error text comes out in Spanish and I'm not sure an approximate translation will be too useful). At any rate the error occurs at the statement I've mentioned, that's for sure. As far as I can recollect it was something like "the select method failed in the range class" or something along these lines.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    I recognise that error

    I've just checked, and it is solved by adding the xlWS.Activate line!

    You might need to activate the workbook too, in which case this would do it:
    VB Code:
    1. ...
    2.    Set xlWS = xlBook.Worksheets(1)
    3.  
    4.    xlBook.Activate
    5.    xlWS.Activate
    6.    With xlWS
    7.         .Rows("2:2").Insert Shift:=xlDown
    8. ...

  13. #13

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by si_the_geek
    I recognise that error

    I've just checked, and it is solved by adding the xlWS.Activate line!
    Now this is becoming ever uncannier.

    First, I think the error kept coming out because of a stupid mistake I'd made:

    I had wrongly written the line
    .Rows("2:2").Insert Shift:=xlDown
    as:
    .Rows("2:2").Select.Insert Shift:=xlDown

    As soon as I corrected this no error was produced when running the code.

    YET Excel wouldn't go away from task manager!!!

    Thereafter I added the
    xlBook.Activate
    xlWS.Activate
    lines but things were pretty much the same...

    I think I'm going to leave this for tomorrow and maybe I'll have to check all the subroutine adding lines at a time.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  14. #14

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    I may have finally found the source of trouble.

    I had code like this:
    VB Code:
    1. With xlws
    2.         .Rows("2:2").Insert Shift:=xlDown
    3.         .Cells(2, 1).Value = Text1.Text
    4.         .Cells(2, 2).Value = Text2.Text
    5.         .Cells(2, 3).Value = Text3.Text
    6.         ...
    7. End With
    There were so many lines that a typo had gone unnoticed:

    Cells(2,34).Value = Combo4.List(Combo4.ListIndex)

    without the dot at the beginning...

    After I placed that dot Excel was definitely killed at the end of the subroutine. Now, I've tested this under XP at home, but I was having the trouble with 98 at the office. I'll test it tomorrow.

    Strangely enough no error was reported when running the code.
    .
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  15. #15

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Thumbs up

    It works under Win98 as well.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Ah, I didnt realise there was more code! It's an easy mistake, and strangely it doesnt always give an error (even though it should).

    I'm glad you got it working

  17. #17

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by si_the_geek
    I'm glad you got it working
    I hope this may help other people who run into a similar headache.
    Thanks B2U !
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  18. #18

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by si_the_geek
    I'm glad you got it working
    I hope this may help other people who run into a similar headache.
    Thanks B2U !
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

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