SUMIFS带有特定日期和名称的输出将在带有月份名称的下一张工作表中输出

时间:2019-07-11 07:52:09

标签: excel vba

我开始对Excel进行编程,以合并下个月工作表中具有月份名称的特定时期和特定人员的数据。

在这里附上我的屏幕截图和屏幕截图。

我知道我的编码不好。请提出建议并帮助您得出结果。

Screen Shot

Sub January()
    Sheets("Temp").Select
    Sheets("Temp").Copy After:=Sheets(ThisWorkbook.Worksheets.Count)
    Sheets("Temp (2)").Select
    Sheets("Temp (2)").Name = ThisWorkbook.Worksheets("QA Perf").Range("X1").Value

    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim wsActiveSheet As Worksheet
    Set wsActiveSheet = wb.ActiveSheet
    Dim rng As Range

    With Sheets("QA Perf")
        Set rng = .Range(.[G3], .Cells(Rows.Count, "G").End(xlUp))
    End With

    With wsActiveSheet
        rng.Copy .[A2]
        .Range(.[A1], .Cells(Rows.Count, "A").End(xlUp)).RemoveDuplicates Columns:=1, Header:=xlYes
        .Range(.[A2], .Cells(Rows.Count, "A").End(xlUp)).Copy
        .[A2].PasteSpecial Paste:=xlPasteAll, Transpose:=False
    End With

    With wsActiveSheet.Sort
         .SortFields.Add Key:=Range("A2"), Order:=xlAscending
         .SetRange Range("A:A")
         .Header = xlYes
        .Apply
    End With

    Dim i&, j&
    Dim pws As Worksheet
    Dim startdate As Date, enddate As Date
    Dim mnt As Integer
    Dim yr As Integer

    Set pws = Worksheets("QA Perf")
    yr = pws.Range("Y1").Value
    mnt = Month("01-" & pws.Range("X1").Value & "-" & yr)


    startdate = VBA.DateSerial(yr, mnt, 1)
    enddate = Application.WorksheetFunction.EoMonth(startdate, 1)
    Application.ScreenUpdating = 0
    With wsActiveSheet
        For j = 8 To 19
            For i = 2 To wsActiveSheet.Cells(.Rows.Count, 1).End(xlUp).Row
                wsActiveSheet.Cells(i, j - 6).Formula =     Application.SumIfs(pws.Columns(j), pws.Range("G:G"), wsActiveSheet.Range("A" & i), pws.Range("A:A"), ">=" & startdate, pws.Range("A:A"), "<=" & enddate)
            Next
        Next
    End With

    Application.ScreenUpdating = True
End Sub


Private Sub CommandButton1_Click()
    If Worksheets("QA Perf").Range("X1") = "January" Then
        Call January
    End If
End Sub

0 个答案:

没有答案