+ Reply to Thread
Results 1 to 5 of 5

VBA Range Recalculate Locks spreadsheet

Hybrid View

  1. #1
    Registered User
    Join Date
    11-30-2004
    Posts
    1

    VBA Range Recalculate Locks spreadsheet

    Excel 2003 is driving me nuts
    (Toshiba M4 XP SP3, 1GB 2*750MHz)
    I have a medium size spreadsheet. The basic structure is
    A Main page of 100 rows and 200 cols using data from 8 other pages
    Each row in the main page is independent (Multiple copies of the same row)

    I have a VBA subroutine that tries to find the value for a particular cell that gives the smallest value in another cell by iterating through a number of possible values (See below)

    I have a couple of ‘challenges’
    1) I have tried Range(“rng”).Calculate and Range(rng).calculate and both produce 1004 errors so I have resorted to the more cumbersome Range(Cells( etc . . . . which works OK. Why does Range(“rng”).Calculate produce 1004 error?
    2) Recalculation Issues
    i) If I do a full calculate each time through the loop its unacceptably slow (1.5 secs / time through the OQ<MaxOQ loop)
    ii) If I switch off set manual recalculation before the loop its much quicker (approx .5 secs/time through the OQ<MaxOQ loop)
    iii) If I change from Calculate to Range.calculate it’s a decent speed (<.05 sec/loop) but then excel locks up on the final statement that sets recalculation back to automatic (and does a recalculate). Locks up means drives the processor flat out for more than 5 mintes
    So what is causing this Excel lock up?
    If I have not previously done a Range.calculate, Calculate works fine so why does it get stuck?

    Thanks for any suggestions

    The Routine (Lines in capitals are where I have summarised sections of the code.
    '    Application.Calculation = xlCalculationManual
        For Each rng In Selection.Rows
            lRowSelected = rng.Row
            If lRowSelected >= MinRow And lRowSelected <= MaxRow Then
                INITIALISE VARAIBLES
    
               Do While OQ < Maxoq
                  Cells(lRowSelected, ColOQ).Value = OQ
    ' Range("rng").Calculate 'recalculate values in this row
    'Range(rng).Calculate
    'Range(Cells(lRowSelected, MinCOl), Cells(lRowSelected, MaxCol)).Calculate
                  Calculate
    
                   STORE IT IF IT’S THE BEST SO FAR
                   DISPLAY PROGRESS VALUES IN A CELL
                Loop
            Else
               MsgBox "Please select a valid row before calculating the best OQ"
            End If
            Cells(lRowSelected, ColOQ) = BestOQ 'store answer
        Next rng
       Application.Calculation = xlCalculationAutomatic
    '   Calculate
    End Sub
    Last edited by royUK; 08-11-2008 at 04:09 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Welcome to the Forum. Please read the Forum Rules & use Code Tags in future. It would help if we could see how you have declared your variables.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    03-03-2005
    Posts
    41
    Whoops - sorry. Thought it looked a bit messy. Didn't know about code tag.
    Full code follows.
    Thoughts appreciated.

    Sub MinCost()
    
    ColOQ = 24      'Average Order
    ColMult = 33    'Shipping Outer contains
    ColMin = 25     'Min OQ
    ColCost = 55    'Fully Loaded Cost
    ColMax = 21     'Seasons sales
    MaxIter = 5     ' Maximum Iterations
    MinRow = 9      'First row that data in which data can be entered
    MaxRow = 109    'Last row in which data can be entered
    MinCOl = 3      'Min col for recalculate
    MaxCol = 200    'maxCol for recalculate
    ' Parameters are Columns for Order Size, Multiple, Cell to minimise
        Dim rng As Range
        Dim lRowSelected As Long
    '    Application.Calculation = xlCalculationManual
        For Each rng In Selection.Rows
            lRowSelected = rng.Row
            If lRowSelected >= MinRow And lRowSelected <= MaxRow Then
               Multoq = Cells(lRowSelected, ColMult)
               Minoq = Cells(lRowSelected, ColMin)
               Maxoq = Cells(lRowSelected, ColMax)
               Incoq = Max(Multoq, Int((Maxoq - Minoq) / MaxIter))
               Incoq = Multoq * Int(Incoq / Multoq + 0.99999)
               BestCost = 10 ^ 9
               BestOQ = Minoq
               OQ = Max(Incoq, Minoq)  'initial Order Quantity
               Do While OQ < Maxoq
                  Cells(lRowSelected, ColOQ).Value = OQ
    'Range("rng").Calculate 'recalculate values in this row
    'Range(rng).Calculate
    'Range(Cells(lRowSelected, MinCOl), Cells(lRowSelected, MaxCol)).Calculate
         Calculate
                  If Cells(lRowSelected, ColCost) < BestCost Then
                     BestCost = Cells(lRowSelected, ColCost)
                     BestOQ = OQ
                  End If
                  OQ = OQ + Incoq
                  Cells(5, 3).Value = OQ
                  Cells(4, 3).Value = BestCost
               Loop
            Else
               MsgBox "Please select a valid row before calculating the best OQ"
            End If
            Cells(lRowSelected, ColOQ) = BestOQ 'store answer
        Next rng
       Application.Calculation = xlCalculationAutomatic
    '   Calculate
    End Sub

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I think that you are looking for
    Rows(rng.Row).Calculate
    Last edited by royUK; 08-11-2008 at 08:12 AM.

  5. #5
    Registered User
    Join Date
    03-03-2005
    Posts
    41
    Thanks - that works just fine. However, I don't really understand why Range(rng).Calculate does not work?

    However the real problem is No 2 in the original post. Why does excel "lock up" after the loop when recalculation is set back to automatic?
    Any suggestions on this one?
    TFAI

+ 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