【问题标题】:Automatically adding a month-summery row at the end of each month?在每个月底自动添加一个月-夏季行?
【发布时间】:2016-03-17 21:25:48
【问题描述】:

我正在使用 Excel 表来跟踪我的工作时间。每一行代表一个工作日,并有“日期”、“上班时间”、“下班时间”、“总工作时间”和“日薪”。 我希望工作表在下个月每次写条目时自动创建一个月份的摘要行(即,如果日期为 13/3/16 的行后面跟着日期为 2/4/16 的行,第二行将被下推,并在其间创建带有summery的行(即:每月总小时数,每月总工资)。它应该看起来像这样:

这可能吗?如果是这样,我该怎么做? 感谢您的意见!

【问题讨论】:

    标签: excel


    【解决方案1】:

    您应该将此代码放在工作表代码模块中。它对我有用,像你这样组织的数据。

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ThisSheet As Worksheet
    Dim NewRow As Long
    Dim OldTotalRange As Range
    Dim OldTotalRow As Long
    
    Set ThisSheet = ActiveSheet
    'If not single cell is changed, exit sub
    If Target.Cells.Count = 1 Then
    'Disable events for prevent recursion
    Application.EnableEvents = False
    If Target.Column = 1 And Target.Row <> 1 And Target.value <> "" Then
        If IsDate(Target.value) And IsDate(Target.Offset(-1, 0).value) Then
            If Month(Target.value) <> Month(Target.Offset(-1, 0).value) Then
                With ThisSheet
                    NewRow = Target.Row
                    On Error Resume Next
                        Set OldTotalRange = .Columns(1).Find(What:="Total", After:=Target, SearchDirection:=xlPrevious)
                        OldTotalRow = OldTotalRange.Row
    'It's for first 'Total' when there isn't 'totals' before.
                    If OldTotalRow = 0 Then
                        OldTotalRow = 1
                    End If
                        .Rows(NewRow).Insert
                        .Cells(NewRow, 1) = "Total"
                        .Cells(NewRow, 4).FormulaR1C1 = "=SUM(R[-" & NewRow - OldTotalRow - 1 & "]C:R[-1]C)"
                        .Cells(NewRow, 5).FormulaR1C1 = "=SUM(R[-" & NewRow - OldTotalRow - 1 & "]C:R[-1]C)"
    'It's formatting, you can delete it or change
                        .Range(.Cells(NewRow, 1), .Cells(NewRow, 5)).Interior.Color = RGB(196, 215, 155)
                        .Range(.Cells(NewRow, 1), .Cells(NewRow, 5)).Font.Bold = True
                End With
            End If
        End If
    End If
    Else
    Exit Sub
    End If
    'Enable events
    Application.EnableEvents = True
    End Sub
    

    【讨论】:

    • 如果您稍微解释一下您的解决方案,您的答案会更有用。
    猜你喜欢
    • 1970-01-01
    • 2012-01-11
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    相关资源
    最近更新 更多