RincewindWIZZ
Board Regular
- Joined
- Feb 22, 2005
- Messages
- 81
I'm having a problem with a VBA subroutine in Excel 2003 SP3 running on a Toshiba Tecra M4 XP SP3, 1GB 2*750MHz)
The basic spreadsheet structure is a Main page of 100 rows and 200 cols using data from 8 other pages mostly by lookup, offset, match and index. The first 30 rows are user entered data, the remainder are all the calculations. Each row in the main page is independent of every other row (they are multiple copies of the same formulae)
There are quite a lot of validation, data lists and conditional formatting in the input cells
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) Range.Calculate.
I have tried Range(“rng”).Calculate and Range(rng).calculate (where rng is declared as a range) and both produce 1004 errors so I have resorted to the more cumbersome Range(Cells(a,b),cells(c,d)) 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 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 minutes without ending.
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 after a Range.Calculate especially when all the cells in the range have no dpendents outside the range
Thanks for any suggestions
The basic spreadsheet structure is a Main page of 100 rows and 200 cols using data from 8 other pages mostly by lookup, offset, match and index. The first 30 rows are user entered data, the remainder are all the calculations. Each row in the main page is independent of every other row (they are multiple copies of the same formulae)
There are quite a lot of validation, data lists and conditional formatting in the input cells
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) Range.Calculate.
I have tried Range(“rng”).Calculate and Range(rng).calculate (where rng is declared as a range) and both produce 1004 errors so I have resorted to the more cumbersome Range(Cells(a,b),cells(c,d)) 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 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 minutes without ending.
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 after a Range.Calculate especially when all the cells in the range have no dpendents outside the range
Thanks for any suggestions
Code:
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 'loop though possible values
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 'Is this best so far?
BestCost = Cells(lRowSelected, ColCost)
BestOQ = OQ
End If
OQ = OQ + Incoq ' increment and try again
Cells(5, 3).Value = OQ 'put current answers in cell to see progress
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