如何根据从宏创建的新值更新列值?

问题描述 投票:0回答:2

为暧昧的头衔道歉。

我目前在Excel中有一个工作簿,有5张。每张纸都是相互依存的。根据劳动生产率,成本,销售的新产品等,该工作簿用于锻炼利润。

在主工作表上,可以修改参数,利润显示10年。 10年是我预测的默认年数。但是,使用以下宏,我可以添加和删除年份(扩展预测和减少)从最初的10年开始。

延长年限:

Sub YearsNumberExtension()

    Dim LastCol As Long
    Dim DelCnt As Integer
    DelCnt = Sheets("Clients Control Panel").Range("E17").Value
    Dim copyCol As Range
    Dim DestRange As Range
    Dim lastRow As Long

    With ThisWorkbook.Sheets("Employees")

        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

        Set copyCol = .Range(.Cells(1, LastCol), .Cells(lastRow, LastCol))

        Set DestRange = .Range(.Cells(1, LastCol), .Cells(lastRow, LastCol + DelCnt))

        copyCol.AutoFill Destination:=DestRange, Type:=xlFillDefault

    End With

  With ThisWorkbook.Sheets("PL")

        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

        Set copyCol = .Range(.Cells(1, LastCol), .Cells(lastRow, LastCol))

        Set DestRange = .Range(.Cells(1, LastCol), .Cells(lastRow, LastCol + DelCnt))

        copyCol.AutoFill Destination:=DestRange, Type:=xlFillDefault

    End With


  With ThisWorkbook.Sheets("Contracts")

        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

        Set copyCol = .Range(.Cells(1, LastCol), .Cells(lastRow, LastCol))

        Set DestRange = .Range(.Cells(1, LastCol), .Cells(lastRow, LastCol + DelCnt))

        copyCol.AutoFill Destination:=DestRange, Type:=xlFillDefault

    End With

    With ThisWorkbook.Sheets("Employee Time")

        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

        Set copyCol = .Range(.Cells(1, LastCol), .Cells(lastRow, LastCol))

        Set DestRange = .Range(.Cells(1, LastCol), .Cells(lastRow, LastCol + DelCnt))

        copyCol.AutoFill Destination:=DestRange, Type:=xlFillDefault

    End With

End Sub

减少年限

Sub YearsNumberReduction()
Dim LastCol As Long
Dim DelCnt As Integer
DelCnt = Sheets("Clients Control Panel").Range("E19").Value
With ThisWorkbook.Sheets("Contracts")
    LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    If LastCol >= DelCnt Then
        .Range(.Cells(1, LastCol - DelCnt + 1), .Cells(1, LastCol)).EntireColumn.Delete
    End If
End With


With ThisWorkbook.Sheets("Employee Time")
    LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    If LastCol >= DelCnt Then
        .Range(.Cells(1, LastCol - DelCnt + 1), .Cells(1, LastCol)).EntireColumn.Delete
    End If
End With

With ThisWorkbook.Sheets("Employees")
    LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    If LastCol >= DelCnt Then
        .Range(.Cells(1, LastCol - DelCnt + 1), .Cells(1, LastCol)).EntireColumn.Delete
    End If
End With

With ThisWorkbook.Sheets("PL")
    LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    If LastCol >= DelCnt Then
        .Range(.Cells(1, LastCol - DelCnt + 1), .Cells(1, LastCol)).EntireColumn.Delete
    End If
End With

End Sub

当我执行任一宏时,他们添加或删除列数N次,在主电子表格中指定我调整各种参数。

在这里,N是4,所以从10年开始,它增加了4年,在我运行宏之后。

enter image description here

但是,在我查看所有统计数据的主页面上,年数保持为10年,而不是14年。

enter image description here

我想确保主面板上的年数反映了Profit Sheet(PL)中的年数,因为它是动态的,我可以使用以下宏添加/删除年份。什么是可行的方法来做到这一点? (目前已修复且仅限于10年,我使用hlookup函数链接它们)

使用解决方案:

enter image description here

enter image description here

excel vba excel-vba
2个回答
3
投票

在主标签中使用此公式代替Year 0Year 1Year 2 ...

=IFERROR(INDEX(PL!$1:$1,MATCH("Year " &ROW()-1,PL!$1:$1,0)),"")

Row()-1调整为Year 0将开始的任何行,然后根据需要向下拖动多行。

当PL在PL选项卡中插入或列时,年份将正确显示在主选项卡中。


0
投票

你真的需要宏吗?难道你不能只使用if返回你的细胞配方或""?所以,假设您的年数在一个名为years_param的单元格中,我将使用XXX代表您在Employees和PL表格上的当前单元格公式等,然后在这些工作表上替换公式

=IF(ROW() - 4 <= years_param, XXX, "")

我假设你的数据从第4行开始,我可能会被一个人关闭,但公式应该给你一般的想法。

然后,您可以轻松地为TOTAL PROFIT列执行类似的操作(例如,假设第0行在第29行,相应地进行调整):

=IF(ROW()-29 <= years_param, "Year "&ROW()-29, "")

现在添加一个IF,检查在HLOOKUP之前列是否为空:

=IF(A29=="","",HLOOKUP(...
© www.soinside.com 2019 - 2024. All rights reserved.