Consulting

Results 1 to 8 of 8

Thread: code not calculating storage charge correctly

  1. #1

    code not calculating storage charge correctly

    Hello I use the below code to calculate storage charge's after 7 days. It is not calculating correctly After the first month, it would need to be another $40.00 or 0.04 per pound. after 30 calendar days. C4 is the Pickup Date, and A12 is the delivery date, H10 is the Weight in Pounds. Please help me figure this out, I have played around with the code enough.
    Private Sub Worksheet_Calculate()
       Dim myDays       As Long
       Dim myRate       As Double
       Application.EnableEvents = False
       myDays = DateDiff("d", Range("C4").Value, Range("A12").Value)
       If myDays >= 7 Then
          myRate = Range("H10") * 0.04
          If myRate < 40# Then myRate = 40#
       End If
       Range("J25").Value = myRate
       Application.EnableEvents = True
    
    
       'MsgBox myDays
    End Sub

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You have no option for MyDays < 7
    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'

  3. #3
    Quote Originally Posted by mdmackillop View Post
    You have no option for MyDays < 7
    That's where i'm getting confused. I tried wording that under the "If my days >=7 then" line

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Something like
    If myDays >= 7 Then
            myRate = Range("H10") * 0.04
            If myRate < 40# Then myRate = 40#
        Else
            myRate = Range("H10") * 0.1
            If myRate < 50# Then myRate = 50#
        End If
    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
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    BTW
    If you disable events and the code crashes, events will be left disabled which could have consequences elsewhere. You can guard against that as follows
    On Error GoTo Exits
        Application.EnableEvents = False
    
    'Do stuff here
        
    Exits:
        Application.EnableEvents = True
    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'

  6. #6
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    I think all you need to do is change this line:

    [vba]Range("J25").Value = myrate
    [/vba]
    to

    [vba]Range("J25").Value = myrate + myrate * Application.WorksheetFunction.RoundDown(mydays / 30, 0)
    [/vba]

  7. #7
    Quote Originally Posted by offthelip View Post
    I think all you need to do is change this line:

    [vba]Range("J25").Value = myrate
    [/vba]
    to

    That did it, thank you - I appreciate it.

    [vba]Range("J25").Value = myrate + myrate * Application.WorksheetFunction.RoundDown(mydays / 30, 0)
    [/vba]

  8. #8
    Thanks for the help Md - I appreciate it.

Posting Permissions

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