+ Reply to Thread
Results 1 to 14 of 14

Percent increase calculations...

Hybrid View

  1. #1
    Registered User
    Join Date
    06-06-2007
    Posts
    54

    Percent increase calculations...

    Good morning to everyone...(or whatever time of day it may be)

    I have been working on this code for a little while (with lots of help form NBVC and Ska67can)

    I was wondering if anyone could help me with the small amount of code needed to be able to automatically calculate the percentage needed from one year to the next. I simply need it to automatically create a third column each time the copy old data button is pressed, and I need the column to have the percent increase ((current year - past year)/current year). Hopefully you can understand what I am aiming for. I have the current code below, and I will attach a compressed and stripped version of my workbook.

    Thanks a bunch to everyone in advance.

    'Code created and edited by MBVC and Ska67Can
    
    Sub CopyPaste()
    
    Dim sht As Worksheet
    Dim LastRow As Long, LastCol As Long, LastRow2 As Long, LastCol2 As Long
    
    For Each sht In ActiveWorkbook.Sheets
        If sht.Name <> "Instructions" And sht.Name <> "SATcosts" Then
           LastRow = sht.Cells(65536, 4).End(xlUp).Row
           LastCol = sht.Cells(5, 256).End(xlToLeft).Column
           ' Check if Year has changed.  If yes, proceed, else warning given.
           
           
               If sht.Cells(1, 2).Value <> Year(Now()) Then
               sht.Cells(4, LastCol + 1).Value = sht.Cells(1, 2).Value - 1
               Range(sht.Cells(5, 4), sht.Cells(LastRow, 5)).Copy
               sht.Cells(5, LastCol + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                   :=False, Transpose:=False
               
               
               sht.Cells(5, LastCol + 1).Value = 0
        
               LastCol2 = sht.Cells(5, 256).End(xlToLeft).Column
               LastRow2 = sht.Cells(65536, LastCol2).End(xlUp).Row
               
        
               ' Add left/right borders
               With Range(sht.Cells(4, LastCol2 - 1), sht.Cells(LastRow2, LastCol2)).Borders(xlEdgeLeft)
                   .LineStyle = xlContinuous
                   .Weight = xlThin
                   .ColorIndex = xlAutomatic
               End With
               With Range(sht.Cells(4, LastCol2 - 1), sht.Cells(LastRow2, LastCol2)).Borders(xlEdgeRight)
                   .LineStyle = xlContinuous
                   .Weight = xlThin
                   .ColorIndex = xlAutomatic
               End With
               Application.CutCopyMode = False ' Clear clipboard so big clipboard dialog will not appear when closing main workbook
            Else
                MsgBox "Year has not changed!"
                Exit For
            End If
        End If
    Next sht
    
    End Sub
    Attached Files Attached Files
    Last edited by Spitfire999; 06-15-2007 at 10:48 AM.

  2. #2
    Registered User
    Join Date
    06-06-2007
    Posts
    54
    Anyone?

    I would really appreciate it.

  3. #3
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    Not exactly sure what which columns you mean by "current year" and "past year"

    Try this code and see if it does what you want. You will see that after you hit the copy button, the 3rd column will be 0's, as you add figures in the Column E, the calculations will adjust.....is this right?

    Option Explicit
    
    'Code created and edited by NBVC and Ska67Can
    
    Sub CopyPaste()
    
    Dim sht As Worksheet
    Dim LastRow As Long, LastCol As Long, LastRow2 As Long, LastCol2 As Long
    
    For Each sht In ActiveWorkbook.Sheets
        If sht.Name <> "Instructions" And sht.Name <> "SATcosts" Then
           LastRow = sht.Cells(65536, 4).End(xlUp).Row
           LastCol = sht.Cells(5, 256).End(xlToLeft).Column
           ' Check if Year has changed.  If yes, proceed, else warning given.
           
           
               If sht.Cells(1, 2).Value <> Year(Now()) Then
               sht.Cells(4, LastCol + 1).Value = sht.Cells(1, 2).Value - 1
               Range(sht.Cells(5, 4), sht.Cells(LastRow, 5)).Copy
               sht.Cells(5, LastCol + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                   :=False, Transpose:=False
               
               
               'sht.Cells(5, LastCol + 1).Value = 0
        
               LastCol2 = sht.Cells(5, 256).End(xlToLeft).Column
               LastRow2 = sht.Cells(65536, LastCol2).End(xlUp).Row
               
               ' calculate percentage change
               With sht.Range(Cells(5, LastCol2 + 1).Address, Cells(LastRow2, LastCol2 + 1).Address)
                    .Formula = "=(" & Range("E6").Address(rowabsolute:=False) & "-" & Range("P6").Address(rowabsolute:=False) & ")/" & Range("E6").Address(rowabsolute:=False)
               End With
        
               ' Add left/right borders
               With Range(sht.Cells(4, LastCol2 - 1), sht.Cells(LastRow2, LastCol2)).Borders(xlEdgeLeft)
                   .LineStyle = xlContinuous
                   .Weight = xlThin
                   .ColorIndex = xlAutomatic
               End With
               With Range(sht.Cells(4, LastCol2 - 1), sht.Cells(LastRow2, LastCol2)).Borders(xlEdgeRight)
                   .LineStyle = xlContinuous
                   .Weight = xlThin
                   .ColorIndex = xlAutomatic
               End With
               Application.CutCopyMode = False ' Clear clipboard so big clipboard dialog will not appear when closing main workbook
            Else
                MsgBox "Year has not changed!"
                Exit For
            End If
        End If
    Next sht
    
    End Sub
    BTW: My handle is NBVC.....not MBVC...
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  4. #4
    Registered User
    Join Date
    06-06-2007
    Posts
    54
    Sort of on the right lines, I have just attached a single spread sheet to demonstrate what the desired output should look like. When I add the current year to the history table, by running the macro, I want it to insert a column like the ones i highlighted in blue.

    Hopefully that makes a bit more sense. And really sorry about the handle mishap...its all peachy now.
    Attached Files Attached Files

  5. #5
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    Try this code:

    Option Explicit
    
    'Code created and edited by NBVC and Ska67Can
    
    Sub CopyPaste()
    
    Dim sht As Worksheet
    Dim LastRow As Long, LastCol As Long, LastRow2 As Long, LastCol2 As Long
    
    For Each sht In ActiveWorkbook.Sheets
        If sht.Name <> "Instructions" And sht.Name <> "SATcosts" Then
           LastRow = sht.Cells(65536, 4).End(xlUp).Row
           LastCol = sht.Cells(5, 256).End(xlToLeft).Column
           ' Check if Year has changed.  If yes, proceed, else warning given.
           
           
               If sht.Cells(1, 2).Value <> Year(Now()) Then
               sht.Cells(4, LastCol + 1).Value = sht.Cells(1, 2).Value - 1
               Range(sht.Cells(5, 4), sht.Cells(LastRow, 5)).Copy
               sht.Cells(5, LastCol + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                   :=False, Transpose:=False
               
               
               'sht.Cells(5, LastCol + 1).Value = 0
        
               LastCol2 = sht.Cells(5, 256).End(xlToLeft).Column
               LastRow2 = sht.Cells(65536, LastCol2).End(xlUp).Row
               
               ' calculate percentage change
               With sht.Range(Cells(5, LastCol2 + 1).Address, Cells(LastRow2, LastCol2 + 1).Address)
                    .Formula = "=(" & Cells(6, LastCol2).Address(rowabsolute:=False) & "-" & Cells(6, LastCol2 - 2).Address(rowabsolute:=False) & ")/" & Cells(6, LastCol2).Address(rowabsolute:=False)
               End With
               
               With Range(Cells(5, LastCol2 + 1), Cells(LastRow2, LastCol2 + 1))
                    .Font.ColorIndex = 41
                End With
        
               ' Add left/right borders
               With Range(sht.Cells(4, LastCol2 - 1), sht.Cells(LastRow2, LastCol2)).Borders(xlEdgeLeft)
                   .LineStyle = xlContinuous
                   .Weight = xlThin
                   .ColorIndex = xlAutomatic
               End With
               With Range(sht.Cells(4, LastCol2 + 1), sht.Cells(LastRow2, LastCol2)).Borders(xlEdgeRight)
                   .LineStyle = xlContinuous
                   .Weight = xlThin
                   .ColorIndex = xlAutomatic
               End With
               With Range(sht.Cells(4, LastCol2 - 1), sht.Cells(4, LastCol2 + 1)).Borders(xlEdgeTop)
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                    .ColorIndex = xlAutomatic
               End With
                With Range(sht.Cells(4, LastCol2 - 1), sht.Cells(4, LastCol2 + 1)).Borders(xlEdgeBottom)
                            .LineStyle = xlContinuous
                            .Weight = xlThin
                            .ColorIndex = xlAutomatic
                End With
                Application.CutCopyMode = False ' Clear clipboard so big clipboard dialog will not appear when closing main workbook
              
            Else
                MsgBox "Year has not changed!"
                Exit For
            End If
        End If
    Next sht
    
    End Sub

  6. #6
    Registered User
    Join Date
    06-06-2007
    Posts
    54
    Wow, again worked just great...
    Hopefully after this my project will be about 99% done which will make me very happy.

    But just wondering, as just touch up, would you happen to know what I should put in so that

    a. when the numbers are copied and pasted from the current table, that the price is formated in currency form (U.S $ with 2 decimal places)?

    b. that the % increase cells are formatted in percent form.

    and...

    c. How if there is no value for the previous year that when calculating the increase % that it simply leaves the cell blank? (or vice versa where there is no current year price for it).

    oh, and NBVC, I owe you something for all your trouble, I just have to think of what...

+ 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