Speed up looping?

alee001

Board Regular
Joined
Sep 9, 2014
Messages
154
Office Version
  1. 2010
Platform
  1. Windows
I tried to use method A instead of method B to speed up the loop process in function, but in the massive fx calculation did not significantly accelerate, do you know why?
VBA Code:
Dim lot_now As Long

Method A:
   Dim lot_chk As Long
   lot_chk = 0
rtn_chk:
   If lot_now > 10 Then
     step = 10
   Else
     step = 2
   End If
   If amt - lot_now * rate < min_cost And lot_now > 0 Then
     If lot_now = lot_chk Then
       lot_now = lot_now - 1
       GoTo fx_exit  'exit loop
     End If
     lot_now = lot_now - step
     GoTo rtn_chk
   Else
     lot_now = lot_now + 1
     lot_chk = lot_now
     GoTo rtn_chk
   End If

Method B:
Do While amt - lot_now * rate < min_cost And lot_now > 0
   lot_now = lot_now - 1
Loop
 
What I meant was to modify your code like so:
VBA Code:
    Dim bkg
    Dim bkg_min
    Dim lvy
    Dim stp
    Dim tra
    Dim qty
    Dim lv
    Dim stm_min
    Dim stm_max
    Dim cty
    
    bkg = Range("bkg").Value2
    bkg_min = Range("bkg_min").Value2
    lvy = Range("lvy").Value2
    stp = Range("stp").Value2
    tra = Range("tra").Value2
    qty = Range("qty").Value2
    lv = Range("lv").Value2
    stm_min = Range("stm_min").Value2
    stm_max = Range("stm_max").Value2
    cty = Range("cty").Value2

    Do While amt - c0 - c1 - c2 _
       - Application.Max(amt * bkg, bkg_min) _
       - amt * (lvy + stp + tra) _
       - lot_now * qty * prt - Application.Max(lot_now * qty * prt * bkg, bkg_min) _
       - lot_now * qty * prt * (lv + stp + tra) _
       - Application.Min(Application.Max(lot_now * qty * prt * stm, stm_min), stm_max) < cty * 12 And lot_now > 0
        lot_now = lot_now - 1
    Loop
Of course how much this improves speed highly depends on the number of times the loop needs to be run.
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top