Consulting

Results 1 to 13 of 13

Thread: How do I put Msgbox in between formulas in VBA?

  1. #1

    How do I put Msgbox in between formulas in VBA?

    I have a simple formula in VBA

    1. How do I put Msgbox, asking "Start in Last Row?" If Yes Start in the Last row, If No start in Row 4.

    And How do I insert the formula that will start the calculation in the last row if yes, and formula that will start the calculation in row 4?

    2. How do I put Msgbox, asking "Continue Calculation?" If Yes continue with next function, If no end run.


    [vba]Option Explicit
    Sub Calculate()
    Range("D37").Formula = "=A3+B3+C3"
    'How do I put Msgbox in this line, asking "Continue Calculation?" If Yes continue with next function, If no end run.
    Range("D712").Formula = "=A7+B7+C7"
    Range("D1318").Formula = "=A13+B13+C13"
    Range("D1923").Formula = "=A19+B19+C19"
    End Sub
    [/vba]

    Thanks for your time! I really appreciate all the help I get form this site!

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    The things you ask are very simple to do, but you need to explain yourself better, in your code you want the formula to be put in to D37, you the overwrite D7, so how do you want to place the formula? should it be in column D last unused cell then down for 20 rows?
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Actually, my purpose is just to have a pause in every 500 rows.

    I have like 20,000 cells to calculate, and it takes 45 minutes to calculate all the cells. So, What I want is to pause in every 500 rows, so I have an option to stop or continue.

    Then, when I stopped or click "No" in my MsgBox, lets say in row 500, and decided to the VBA again I want to have an option to choose if I want to start over again for my calculation or start in the row where I stopped my first calculation.

    Thank you very much for your time!

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Do you mean your code takes 45 minutes to run or the sheet takes 45 minutes to calculate?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    The sheet and yes the code also.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    A code break is simple enough, just add a counter

    [VBA]
    Sub TestBreak()
    Dim i As Long
    For i = 1 To Rows.Count
    Cells(i, 1) = i
    If i Mod 500 = 0 Then
    If MsgBox("Continue?", vbYesNo) = vbNo Then Exit Sub
    End If
    Next
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Thank you mdmckillop!

    How about if I click "No" and stopped. But, then I decided to continue calculation? Will it start in the beginning again or will it start in the last row where I stopeed my calculation the first time?

    If no, how will I go about it?

    Thank you again!

  8. #8
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Not tested but how about something like this:
    (using mdmckillops' suggestion)
    [vba]Public ans As Long
    Sub TestBreak()
    Dim i As Long
    If MsgBox("Calculate from start?", vbYesNo, "Continue iteration") = vbYes Then
    ans = 1
    End If
    For i = ans To Rows.Count
    Cells(i, 1) = i
    If i Mod 500 = 0 Then
    If MsgBox("Continue?", vbYesNo) = vbNo Then
    ans = i
    Exit Sub
    end if
    End If
    Next
    End Sub[/vba]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  9. #9
    Ok I'll try this code.

    Thanks so much to both of you!!!

  10. #10
    I'm sorry to bother you agian, but I added the formulas below and I just can't get it right.

    I already tried inserting the formula in different line but I just couldn't get it.

    Attached is my sample file. Thanks again!

    [VBA]Option Explicit
    Public ans As Long
    Sub TestBreak()
    Dim i As Long
    If MsgBox("Calculate from start?", vbYesNo, "Continue iteration") = vbYes Then
    ans = 1
    End If

    Range("C3:C10").Formula = "=A3+B3"

    For i = ans To Rows.Count
    Cells(i, 1) = i
    If i Mod 500 = 0 Then
    If MsgBox("Continue?", vbYesNo) = vbNo Then
    ans = i
    Exit Sub
    End If
    End If

    Next

    Range("C11:C18").Formula = "=A11+B11"
    For i = ans To Rows.Count
    Cells(i, 1) = i
    If i Mod 500 = 0 Then
    If MsgBox("Continue?", vbYesNo) = vbNo Then
    ans = i
    Exit Sub
    End If
    End If

    Next
    Range("C19:C24").Formula = "=A11+B11"
    End Sub
    [/VBA]

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If it takes 45 minutes to calculate, shouldn't we be addressing that, not adding breaks?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Quote Originally Posted by xld
    If it takes 45 minutes to calculate, shouldn't we be addressing that, not adding breaks?
    naturally you're right Bob, the worksheet/workbook must be full of volatile formulae, i guess we need a sample workbook with actual formulae and to understand the process from the OP i.e does he calculate one sheet or all? is there a need for all.....etc
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  13. #13
    Thanks you for finding time to solve my concern!

    Attached is my sample file with actual formula.

    i deleted 90% of data so file will be small enough to attach. But this is the actual file that I'm trying to work on.

    The formula should calculate all the way down to the last row.

    It also has a VBA for the dates.

    Thank you so much!

Posting Permissions

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