Consulting

Results 1 to 7 of 7

Thread: Application.Calculation

  1. #1

    Application.Calculation

    Hi All,

    What is the basic difference between xlCalculationManual and xlCalculationAutomatic? For my PERSONAL.XLS workbook, the Calculation setting is Automatic. I am trying to understand a macro for a workbook with several worksheets. In this macro , while operating on the data of a particular worksheet, the setting is changed to xlCalculationManual and after the calculation is over the setting is changed to xlCalculationAutomatic.
    I am looking at the following macro :

    Sub Delete_Data()
    StopCharacter = ""
    ObjectColumn = "D"
    DeleteCriteria = SelectedAccount
    RowCounter = 2

    Application.Calculation = xlManual

    Do While Range(ObjectColumn & RowCounter).Value <> StopCharacter
    If Range(ObjectColumn & RowCounter).Value = DeleteCriteria Then
    Rows(RowCounter).Delete
    Else
    RowCounter = RowCounter + 1
    End If
    Loop
    Application.Calculation = xlAutomatic
    End Sub

    Thanks in advance

    - Krishna

  2. #2
    VBAX Newbie
    Joined
    Apr 2008
    Posts
    3
    Location
    Hi Krishna,

    This relates to the Calculation tab in Tools > Options. Running the Application.Calculation = xlManual switches from the default Automatic setting. Essentially it's used to control when Excel recalculates formulae after a cell is changed.

    Hope that helps.

  3. #3
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    The reason it is in the code is that each time a row is deleted Excel might try to recalculate the worksheet. If you turn calculation off at the beginning and then turn it back on at the end, Excel will only recalculate the final results and that should speed up the processing.
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You should always build in an error handler to reset to automatic, as this change affects all workbooks
    [VBA]
    Sub Delete_Data()
    On Error GoTo Reset
    Application.Calculation = xlManual
    'Do Your Stuff
    Reset:
    Application.Calculation = xlAutomatic
    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'

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It's purely about speed. If you set to manual, anything you do in VBA will not cause a recalculation of all formulae. For example, I was handed a workbook recently which had a pivot table, and some code went through and selected every broker from the list and printed it. As handed to me, this took over an hour. I set calculation to manual, and just prior to printing each broker's pivot view I reclaculated just that sheet (there were many others with formulae on), and I reduced the print time down to less than 4 minutes.
    ____________________________________________
    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

  6. #6
    Thanks all of you for the explanation. This forum is awesome; what one can learn from this forum is beyond the scope of any tutorial or book.

    - Krishna

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by krishnak
    Thanks all of you for the explanation. This forum is awesome; what one can learn from this forum is beyond the scope of any tutorial or book.

    - Krishna
    That ius because we can tailor the info to YOUR particular problem. Books cannot only really give guidance, the actual problems out there are just too many and too varied. Having said that, your question is one that could (should?) be very easily covered in a tutorial or a book.
    ____________________________________________
    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

Posting Permissions

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