Sumif in Excel with macro仅根据日期添加一系列数字

时间:2017-07-29 00:58:24

标签: excel vba excel-vba

带有宏的Excel 2013。宏将镜像范围内的任何数字(" C2:C")并将数据放入UserForm中的相应文本框中。该文件将自动更改cmbDate中的日期。

我只希望只镜像我们在同一天添加的那些数字。

我目前的设置是,即使它们有不同的日期,它也会添加所有内容。

此处的屏幕截图:Screenshot

代码:

Private Sub cmdEnter_Click()

Dim x As Integer

With Sheet1
    With .Range("A" & .Rows.Count).End(xlUp)
        .Offset(1).Resize(1, 3).Value = Array(txtDate.Value, cmbNetwork.Value, txtCharge.Value)

        End With

        'txtDate.Value = ""
        cmbNetwork.Value = ""
        txtCharge.Value = ""

End With

Dim a As Long
Dim b As Long
Dim c As Long
Dim lastrow As Long

a = 0
b = 0
c = 0

lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To lastrow
    'If Cells(i, "A").Value = CDate(cmbDate.Value) Then
        If Cells(i, 2) = "ATT" Then
        a = Cells(i, 3) + a

        ElseIf Cells(i, 2) = "Verizon" Then
        b = Cells(i, 3) + b

        ElseIf Cells(i, 2) = "Comcast" Then
        c = Cells(i, 3) + c
        End If
    'End If

Next i

'Range("A16") = a

txtAtt.Value = a
txtVerizon.Value = b
txtComcast.Value = c

x = CDbl(txtAtt.Value) + CDbl(txtVerizon.Value) + CDbl(txtComcast.Value)

txtTotal.Value = x

Sheets("Sheet1").Range("F2") = txtAtt.Value
Sheets("Sheet1").Range("F3") = txtVerizon.Value
Sheets("Sheet1").Range("F4") = txtComcast.Value

End Sub

0 个答案:

没有答案