如何计算增加的​​利润率?

时间:2017-07-31 18:40:29

标签: excel excel-vba excel-formula excel-2010 vba

我想算一家公司从现在起持续增加多少年的利润。

Example here

在图片中,它必须计算到2014年,因为利润相对于2013年没有增加。对于稳定的利润,它必须计算到2012年,因为在2011年利润高于此之后。所以这些是应该完成的两个计算。导入是它不应该计算利润为0 - 0 - 0 - 0的年份行数(=稳定,但0根本没有利润)。

因此,就像质量标准一样,看看公司在业务上的表现如何。因此,如果有一个中断,那么以前的一切都是无关紧要的。

公式应该动态变化,所以如果有2018年,2019年的数据......那么它应该从最新的数据中自动计算。 (公式栏和最近一年的栏目之间会有多年的空间)

对这个新问题的解释:我需要一个更灵活,更复杂的解决方案来解决我的问题,并希望在这个问题上区分它。所以这可能有助于其他有类似问题的人。

2 个答案:

答案 0 :(得分:1)

除非您想编写自定义函数(VBA),否则实现此目标的最简单方法是在利润表中设置corresponding table,以跟踪公司的利润。该对应表格表示在以下情况下返回1时存在利润的年份:

  1. 当前年度值大于上一年度值,
  2. 后续年度逻辑返回true(我在当前年份列右侧的列中放置了'x'以避免当前年份的逻辑)
  3. 然后将逻辑值

    相加

答案 1 :(得分:1)

要计算Years of Increases,请在Cell L2

中输入以下公式
=IFERROR(COLUMN(J2)-COLUMN(INDEX(B2:J2,,MATCH(9.99E+307,IF(B2:J2>A2:I2=FALSE,1,"")))),0)

并计算Years of Steady ProfitsCell M2

中输入以下公式
=IFERROR(COLUMN(J2)-COLUMN(INDEX(B2:J2,,MATCH(9.99E+307,IF(B2:J2>=A2:I2=FALSE,1,"")))),0)

上述两个公式都是数组公式,因此按 Ctrl + Shift + Enter 进行提交。根据需要拖动/复制。见图片以供参考。

enter image description here

如果您希望此公式为动态,即在将新列添加到另一年后,您希望公式正常工作,请考虑以下内容。

Column K中输入一些虚拟角色说x,然后在Cell L2

中输入以下公式
=IFERROR(COLUMN(K2)-COLUMN(INDEX(B2:K2,,MATCH(9.99E+307,IF(B2:K2>A2:J2=FALSE,1,""))))-1,0)

并在Cell M2

=IFERROR(COLUMN(K2)-COLUMN(INDEX(B2:K2,,MATCH(9.99E+307,IF(B2:K2>=A2:J2=FALSE,1,""))))-1,0)

两个公式都是数组公式。根据需要拖动/复制。见图片以供参考。

enter image description here

现在,当您选择Column K并插入新列时,公式将相应更改。

注意下图中的公式(公式栏)。

enter image description here

编辑: 避免计算零的条纹0-0-0-0

对于Years of Steady Profits Cell M2

中使用以下公式
=IFERROR(COLUMN(J2)-COLUMN(INDEX(B2:J2,,MATCH(9.99E+307,IF(((B2:J2>=A2:I2)*(B2:J2<>0))=0,1,"")))),0)

这是一个数组公式。

VBA解决方案:

Option Explicit

Sub Demo()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Dim lastRow As Long, lastCol As Long, rIndex As Long, cIndex As Long
    Dim increaseCnt As Long, steadyCnt As Long
    Dim ws As Worksheet
    Dim isSteady As Boolean, isZero As Boolean

    Set ws = ThisWorkbook.Worksheets("Sheet2")  'change to your data sheet
    With ws
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row    'last row with data using Column A
        lastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column 'last column with data using Row 2
        increaseCnt = 0
        steadyCnt = 0
        isSteady = False

        For rIndex = 2 To lastRow               'loop through row 2 to last row
            For cIndex = lastCol To 2 Step -1   'loop through last column to column 2
                If .Cells(rIndex, cIndex) <> "NA" Then  'check for NA
                    If .Cells(rIndex, cIndex) <> 0 Then 'cheeck for 0
                        If .Cells(rIndex, cIndex) = .Cells(rIndex, cIndex - 1) Then 'compare cells for steady count
                            steadyCnt = steadyCnt + 1       'increment steadyCnt
                            isSteady = True                 'set steady flag true
                        ElseIf .Cells(rIndex, cIndex) > .Cells(rIndex, cIndex - 1) Then 'compare cells for increase count
                            If Not isSteady Then
                                increaseCnt = increaseCnt + 1   'increment increaseCnt
                                steadyCnt = steadyCnt + 1       'increment steadyCnt
                            ElseIf .Cells(rIndex, cIndex) <> 0 Then 'check for cell is 0
                                steadyCnt = steadyCnt + 1       'increment steadyCnt
                            End If
                        Else
                            Exit For                'exit for loop
                        End If
                    Else
                        Exit For                    'exit for loop
                    End If
                End If
            Next cIndex
            .Cells(rIndex, lastCol + 2) = increaseCnt   'display increaseCnt
            .Cells(rIndex, lastCol + 3) = steadyCnt     'display steadyCnt
            increaseCnt = 0
            steadyCnt = 0
            isSteady = False
        Next rIndex
    End With
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
End Sub

参见图片以供参考。

enter image description here