2017-07-07 68 views
0

我在Sheet1中有表格。我可以總結某個國家的所有數字數據。 但是,我只想總結相同日期的數字。 我有ComboBox的日期。因此,例如,如果我選擇7月1日,它只會在七月添加這些號碼1.根據另一列中的另一個標準在列中添加一組數字值

我的期望,如果我選擇7月1日結果如下: 美國24 巴西56 加拿大68

這裏是我的截圖:screenshot

mycode的:

Private Sub ComboBox1_Change() 

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, 3) = "America" Then 
a = Cells(i, 4) + a 

ElseIf Cells(i, 3) = "Brazil" Then 
b = Cells(i, 4) + b 

ElseIf Cells(i, 3) = "Canada" Then 
c = Cells(i, 4) + c 
End If 
Next i 

'Range("A16") = a 

txtAmerica.Value = a 
txtBrazil.Value = b 
txtCanada.Value = c 

End Sub 

Private Sub UserForm_Initialize() 

ComboBox1.List = Sheets("Sheet1").Range("G1:G10").Value 

End Sub 

回答

0

假設在列A的值是日期,並且還假設你的區域設置使用毫米/日/年的DAT e格式,這隻需簡單地將您的求和碼打包在If聲明中即可查看日期:

For i = 2 To lastrow 
    If Cells(i, "A").Value = CDate(ComboBox1.Value) Then 
     If Cells(i, 3) = "America" Then 
      a = Cells(i, 4) + a 
     ElseIf Cells(i, 3) = "Brazil" Then 
      b = Cells(i, 4) + b 
     ElseIf Cells(i, 3) = "Canada" Then 
      c = Cells(i, 4) + c 
     End If 
    End If 
Next i 
+0

非常感謝。這就是我真正想做的事。 –

相關問題