+ Reply to Thread
Results 1 to 5 of 5

vba script for % price change over the period

Hybrid View

  1. #1
    Registered User
    Join Date
    08-10-2016
    Location
    Ireland
    MS-Off Ver
    2013
    Posts
    67

    Re: vba script for % price change over the period

    Quote Originally Posted by AlphaFrog View Post
    Sub DataTest_V4()
        Dim vT, v, vv, vMin, vMax
        Dim i As Long, d As Object
        Dim t As Double
        
        t = Timer
        vT = Range("Transactions!A1").CurrentRegion.Value2
        Set d = CreateObject("Scripting.Dictionary")
        
        With Worksheets("Output")
            .Cells(1, 1).CurrentRegion.Offset(1).Clear
            
            'Item and Description Transactions
            For i = 2 To UBound(vT)
                d.Item(vT(i, 1)) = vT(i, 2)
            Next i
            .Cells(2, 1).Resize(d.Count) = Application.Transpose(d.keys)
            .Cells(2, 2).Resize(d.Count) = Application.Transpose(d.items)
            d.RemoveAll
            
            'Quantity Transactions
            For i = 2 To UBound(vT)
                d.Item(vT(i, 1)) = d.Item(vT(i, 1)) + vT(i, 4)
            Next i
            .Cells(2, 3).Resize(d.Count) = Application.Transpose(d.items)
            v = d.items
            d.RemoveAll
            
            'First Issue Date Transactions
            For i = 2 To UBound(vT)
                If IsEmpty(d(vT(i, 1))) Then
                    d.Item(vT(i, 1)) = vT(i, 3)
                Else
                    d.Item(vT(i, 1)) = WorksheetFunction.Min(d.Item(vT(i, 1)), vT(i, 3))
                End If
            Next i
            .Cells(2, 4).Resize(d.Count) = Application.Transpose(d.items)
            d.RemoveAll
            
            'Minimum Cost Transactions
            For i = 2 To UBound(vT)
                If IsEmpty(d(vT(i, 1))) Then
                    d.Item(vT(i, 1)) = vT(i, 6)
                Else
                    d.Item(vT(i, 1)) = WorksheetFunction.Min(d.Item(vT(i, 1)), vT(i, 6))
                End If
            Next i
            vMin = d.items
            .Cells(2, 5).Resize(d.Count) = Application.Transpose(d.items)
            d.RemoveAll
            
            'Maximum Cost Transactions
            For i = 2 To UBound(vT)
                If IsEmpty(d(vT(i, 1))) Then
                    d.Item(vT(i, 1)) = vT(i, 6)
                Else
                    d.Item(vT(i, 1)) = WorksheetFunction.Max(d.Item(vT(i, 1)), vT(i, 6))
                End If
            Next i
            vMax = d.items
            .Cells(2, 6).Resize(d.Count) = Application.Transpose(d.items)
            d.RemoveAll
            
            'Total Cost Transactions
            For i = 2 To UBound(vT)
                d.Item(vT(i, 1)) = d.Item(vT(i, 1)) + vT(i, 7)
            Next i
            vv = d.items
            d.RemoveAll
            
            'Average Cost Transactions
            For i = LBound(v) To UBound(v)
                If v(i) = 0 Then
                    d.Item(i + 1) = 0
                Else
                    d.Item(i + 1) = vv(i) / v(i)
                End If
            Next i
            .Cells(2, 7).Resize(d.Count) = Application.Transpose(d.items)
            d.RemoveAll
            
            'Percent Change
            For i = LBound(vMin) To UBound(vMin)
                If vMin(i) > 0 Then vMin(i) = (vMax(i) - vMin(i)) / vMin(i)
            Next i
            .Cells(2, 8).Resize(UBound(vMin) + 1) = Application.Transpose(vMin)
            
            With .UsedRange
                .Columns("A:B").NumberFormat = "@"
                .Columns("C").NumberFormat = "#,##0"
                .Columns("D").NumberFormat = "d/m/yyyy"
                .Columns("E:G").NumberFormat = "$* #,##0.00"
                .Columns("H").NumberFormat = "0.0%"
            End With
            
        End With
        
        MsgBox Timer - t
    End Sub
    Thanks AlphaFrog for replying. It is absolutely working fine where the prices have been increased over the time but it is not where the prices have been decreased, example, for item #23456789, the % change should be (21-32)/32 = -34% instead of 52.4%. Please refer in the attached.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Average sales before price change & after price change
    By michts in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-04-2021, 10:32 AM
  2. Replies: 3
    Last Post: 10-10-2020, 01:14 AM
  3. [SOLVED] Excel 2013 wants to calculate Sales Price , If cost price exits and wants change SP
    By Bitto in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-13-2015, 12:49 PM
  4. Calculate period between multiple dates _VBA script
    By raw_geek in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-07-2014, 08:03 AM
  5. [SOLVED] Compare 1 price against multiple prices and change the price according to a formula
    By CharlieAziz in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 10-16-2012, 11:05 AM
  6. Vba Script That Calculates The Payback Period - Excel
    By FSJ in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-24-2012, 02:05 AM
  7. Replies: 11
    Last Post: 06-25-2009, 04:47 PM

Tags for this Thread

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