+ Reply to Thread
Results 1 to 9 of 9

Excel 2010 VBA Macro Stops Running After Application.Wait

  1. #1
    Registered User
    Join Date
    03-30-2014
    Location
    Glendale, Arizona
    MS-Off Ver
    Excel 2013
    Posts
    25

    Question Excel 2010 VBA Macro Stops Running After Application.Wait

    Hi All,

    I am real new at VBA Macros. Tons of experience in the old Lotus 123 Macros but that was another day and time. My VBA Macro abiltiy is pretty much limited to those that I can record, or find answers for on the web. I am trying to write a macro that I can use to sum columns (diferrent 1's at different times) that have contiguously filled cells above, the number of rows of which may vary. After this section of the Macro is complete I want to continue on with more Macro steps that I have already written. The summations may take place in the same columns at different rows but the data will always be sepearted by blank cells. I am able to accomplish some of this with the following subroutine.

    Sub SumCol()

    Range("C6:D6").Cells.ClearContents 'This clears the summation formula from prior attempts.
    Range("C3").Select 'This selects the top cell to be included in the sum.
    Selection.End(xlDown).Select 'This selects C3:C5 (in this case) as the range to be summed.
    ActiveCell.Offset(1, 0).Range("A1").Activate 'This makes the cell immediately below the data (C6) active for the sum formula..
    Application.CommandBars.ExecuteMso ("AutoSum") 'This enters the formula =sum(C3:C5) in cell C6 (Which is what I want - but it doesn't close off the formula).
    Application.SendKeys "{return}" 'The sum formula remained open and all processing stopped until I input this row and the next.
    Application.Wait Now + TimeValue("00:00:10") 'Once this row was entered the sum formula closed & I got the right answer, but all further processing stops.

    Range("D6").Select 'Without the row above this would become the active cell and =sum(C3:C5) would show up here at D6.
    'With the Application.Wait row above all processing of this subroutine halts, never to restart.

    ActiveCell.Value = 15 'The rest is just junk code to see if I could make this puppy continue working.
    Application.SendKeys "{return}"
    Application.Wait Now + TimeValue("0:00:10")

    End Sub


    SendKeys might be the problem as the Application.Wait doesn't slow the process down even for a second in this Macro. When I put the Application.Wait line in its own subroutine and run that by itself, it pauses Excel for whatever time I assign to it. I am a might bewildered over all this and would really appreciate your help. Thanks!

    DMumme

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Excel 2010 VBA Macro Stops Running After Application.Wait

    Hi and welcome to the forum.

    When posting your code to a thread, surround it with CODE tags. See my signature block below.


    Try something like this. It uses the .Formula property of the Range object (in this case cell C6) to apply the SUM function

    Please Login or Register  to view this content.
    There are several methods to determine the last used cell in a column. This link may help.
    Find last row, column or last cell
    Last edited by AlphaFrog; 03-30-2014 at 09:05 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    03-30-2014
    Location
    Glendale, Arizona
    MS-Off Ver
    Excel 2013
    Posts
    25

    Re: Excel 2010 VBA Macro Stops Running After Application.Wait

    AlphaFrog,

    Thanks for the quick response. I will apply [CODE] tags in the future.

    The problem I have with the response given is that it assumes the cells "C3:C5" as a given. The cells will be different each time the Macro is run. Different columns, different rows, different number of columns and rows. I have no problem identifying where I want the sum to be or the data I need summed. It doesn't fit the mold of being from the last used cell in a column to a given row above. It is always changing. That is why I was glad to find the "AutoSum" feature. Problem was getting the feature out of "edit" mode ie hitting return. I thought that might be what "SendKeys" could be used for. Any other ideas or am I just not getting it?

    DMumme

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Excel 2010 VBA Macro Stops Running After Application.Wait

    Quote Originally Posted by DMumme View Post
    I have no problem identifying where I want the sum to be or the data I need summed.

    The AutoSum and SendKeys is not the way to do it. Trust me.

    If you can identify where to put the formula and what you want summed, you can dynamically create the SUM formula in VBA. The formula is just a text string. It doesn't have to be hard coded as C3:C5. But I can't give you specifics without knowing your specifics.

    As an example: this method will put a SUM formula in the ActiveCell. It will Sum from row 3 of the active cell column to the row just above the ActiveCell. It doesn't matter what column or row the active cell is in. This is similar to the AutoSum feature.

    Please Login or Register  to view this content.
    Range.FormulaR1C1 Property

    Excel – R1C1 Reference Style

  5. #5
    Registered User
    Join Date
    03-30-2014
    Location
    Glendale, Arizona
    MS-Off Ver
    Excel 2013
    Posts
    25

    Re: Excel 2010 VBA Macro Stops Running After Application.Wait

    Once again, thanks for your attention. This answer won't work because it assumes that the Macro knows the number of rows to be summed (in your example - 3). It does not. The answer may be 3 as in your example this time but next time it may be 50. I simply want to sum the numbers from [the cell above the active cell] to [the top cell above the active cell before a blank cell is encountered]. This will change all the time. It may be higher or lower in the same column as the last time the Macro was run. It may be in a different column than the last time the macro was run. It is very dynamic. It will not necessarily be all the rows in the column.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    In the code posted the range to sum is not 3 rows.

    It's from row 3 of the column the formula is in to the row above the row the formula is in.

    So, for example, if you put the formula in A50 the range to be summed would be A3:A49.
    Last edited by Norie; 04-06-2014 at 12:41 AM.
    If posting code please use code tags, see here.

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Excel 2010 VBA Macro Stops Running After Application.Wait

    Quote Originally Posted by DMumme View Post
    Once again, thanks for your attention. This answer won't work because it assumes that the Macro knows the number of rows to be summed (in your example - 3). It does not. The answer may be row 3 as in your example this time but next time it may be 50. I simply want to sum the numbers from [the cell above the active cell] to [the top cell above the active cell before a blank cell is encountered]. This will change all the time. It may be higher or lower in the same column as the last time the Macro was run. It may be in a different column than the last time the macro was run. It is very dynamic. It will not necessarily be all the rows in the column.
    I thought I explained it pretty well. 3 is the start row. It sums from row 3 to the row just above the Active cell. That one line of code figures it all out. The two links explain how it works.

    Quote Originally Posted by AlphaFrog
    ...this method will put a SUM formula in the ActiveCell. It will Sum from row 3 of the active cell column to the row just above the ActiveCell. It doesn't matter what column or row the active cell is in. This is similar to the AutoSum feature.
    Just try it then comment if it doesn't work.

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

    Re: Excel 2010 VBA Macro Stops Running After Application.Wait

    Perhaps you want something like:
    Please Login or Register  to view this content.
    Remember what the dormouse said
    Feed your head

  9. #9
    Registered User
    Join Date
    03-30-2014
    Location
    Glendale, Arizona
    MS-Off Ver
    Excel 2013
    Posts
    25

    Re: Excel 2010 VBA Macro Stops Running After Application.Wait

    I added a reply weeks ago but I don't see it so I will try again.

    romperstomper: This pretty much works. I edited your formula to be:
    Please Login or Register  to view this content.
    and this works like magic every time.
    Thanks!
    [SOLVED]

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] 2010 Excel stops working when running macros
    By Yamaha Rider in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-23-2013, 02:40 PM
  2. [SOLVED] Macro to wait 5 seconds before running next line
    By Jiptastic in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-27-2013, 11:22 AM
  3. Application.Wait Now + TimeValue("00:00:10"), but it stops my workbook also.
    By esbenhaugaard in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-20-2012, 04:50 PM
  4. Excel VBA Macro Running DOS Application !!
    By monir in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-25-2006, 11:00 PM
  5. Excel VBA Macro stops running when another program is activated
    By Brody in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-23-2006, 02:45 PM

Tags for this Thread

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