+ Reply to Thread
Results 1 to 16 of 16

Application.WorksheetFunction stops working if the Sub is called

  1. #1
    Registered User
    Join Date
    02-01-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    12

    Application.WorksheetFunction stops working if the Sub is called

    Hi All,

    I'm new to VBA and am sure there is a good reason for this that has completely escaped me.

    I have the following sub which is one of the earliest I wrote:

    Please Login or Register  to view this content.
    If I run this as a standalone Macro it works just fine, if instead I call the function as shown below I get a "Run-time Error '1004': Unable to get the Log10 property of the WorksheetFunction class.

    Please Login or Register  to view this content.
    I wold be extremely grateful if someone could explain why this occurs and how I may fix the problem.

    Cheers!
    Last edited by emmalg; 02-03-2011 at 09:29 AM. Reason: solved

  2. #2
    Registered User
    Join Date
    02-01-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Application.WorksheetFunction stops working if the Sub is called

    In fact, I've just realised it doesn't even execute the calls to ReformatDate, ReformatTime, ReformatXName at all either! :-)

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Application.WorksheetFunction stops working if the Sub is called

    Hi emmalg,
    Is there a chance that your early routines erase or overwrite the Sheet2!(counter,22) cells with non numeric data?
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Application.WorksheetFunction stops working if the Sub is called

    Hi,

    This looks like a case for Option Explicit to solve. See
    http://www.cpearson.com/excel/declaringvariables.aspx

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Application.WorksheetFunction stops working if the Sub is called

    Sounds like you have a problem in CreateQT.

    Another way to do AddK:
    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    02-01-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Application.WorksheetFunction stops working if the Sub is called

    Hi All,

    I have Option Explicit in use as recommended by Marvin P in an earlier thread. The code will compile without errors and if do F5 and select each of the sub routines I have called in turn (CreateQT, ReformatDate, ReformatTime, ReformatXName, AddK and Stats) everything works perfectly, I was just getting a bit sick of having to run so many separate marcos when I wanted to check the end to end process.

    The eventual aim is that everything in the work book will be completed by the user selecting one or two marcos.

    sgh - Thank you, it seems to work (though not with the rest of my code I get the same error)... Looks like you are right there is an earlier error but it isn't in CreateQT.

    I think I have found the issue (or at least where it starts) - when I call ReformatDate from PrepSheet, it isn't inserting the data correctly. Run on it's own it creates a new column before F and it first enters "aquisition date" in the top cell of the new column.... Except this is turning up in column U!

    BUT! If I put a breakpoint in at the lRow=lastRow() line, run PrepSheet and step through from the breakpoint it works perfectly again

    The guilty code:
    Please Login or Register  to view this content.
    etc...

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Application.WorksheetFunction stops working if the Sub is called

    One thing is that you aren't passing a worksheet reference to LastRow, which means it's computing last row for the active sheet.

    The "etc." part is difficult to comment on.

  8. #8
    Registered User
    Join Date
    02-01-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Application.WorksheetFunction stops working if the Sub is called

    So long as I keep the break point in ReformatDate lRow=LastRow() and step through until the column has been inserted ALL the subsequent functions work perfectly, including ReformatDate which adds a new row in exactly the same way.

    Am totally confused!

  9. #9
    Registered User
    Join Date
    02-01-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Application.WorksheetFunction stops working if the Sub is called

    Sorry sgh - the "etc" was supposed to be in the code tag, i left it out as the error is related to the insertion of the column.

    I accept your criticism of the lack of worksheet being passed to Last Row - I've been activating the worksheet before calling it ans I had ommited to here - didn't make any difference when I did though.

    Now off to rework the last col and last row to take a sheet reference as input...

  10. #10
    Registered User
    Join Date
    02-01-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Application.WorksheetFunction stops working if the Sub is called

    Hi All,

    I have found the problem but I can't figure out how to fix it!

    If I watch the spreadsheet while the PrepSheet routine is executing, cell A1 says "ExternalData_43: Getting Data ..." and in cells F1 and column G1 it already says "acquisition_date" and "acquisition_time".

    It appears that the CreateQT sub routine does not complete before the other sub routines have started, therefore everything breaks and the run-time 1004 error message pops up.

    Now that I understand the issue I have a clearer question:

    How can I ensure that my sub routines are run to completion in sequence and not in parallel?

    Thanks for your patience!

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Application.WorksheetFunction stops working if the Sub is called

    There is a Refreshing property that tells you the status of a background query, and a BackgroundQuery argument for SQL queries that will not rqeturn control until the query is complete.

  12. #12
    Registered User
    Join Date
    02-01-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Application.WorksheetFunction stops working if the Sub is called

    Hi sgh,

    So you mean At the end of the query you have to do .Refresh? My query is shown below (I have cut out several lines of the sql query in the extract below just because it is so long), I believed this syntax to be correct, though this was the first macro I ever created and I followed examples posted online. If you have any advice I would approciate it.

    Please Login or Register  to view this content.

  13. #13
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Application.WorksheetFunction stops working if the Sub is called

    I really have none for queries, sorry, but am certain that other people could advise.

  14. #14
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Application.WorksheetFunction stops working if the Sub is called

    Use:
    Please Login or Register  to view this content.
    Remember what the dormouse said
    Feed your head

  15. #15
    Registered User
    Join Date
    02-01-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Application.WorksheetFunction stops working if the Sub is called

    Hi sgh,

    Actually you are a star! What you just said about the BackgroundQuery property was just what I needed, I thought the default was for it to be False but it must be True, by replacing the oQt.Refresh with oQt.Refresh BackgroundQuery:=False absolutely everything works!

    Thank you so much!

  16. #16
    Registered User
    Join Date
    02-01-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Application.WorksheetFunction stops working if the Sub is called

    Thanks romperstomper, I just beat you to it but it was still what I needed! :-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1