PDA

View Full Version : [SOLVED:] sum data based on dates from sheet to another



maghari
08-08-2020, 03:21 AM
hello
i try sum data based on cell value contains date so whan i write the date in g2 it supposes sum from the first date to what i write in g2 and copy theses data from a2:e to sheet2 this my code doesn't work also not give me error

Dim l As LongDim LastRow As Long
Dim oWkSht As Worksheet
Dim FirstDate As Date
Dim LastDate As Date
'Dim tsales As Long '<-- not used in this code

Set oWkSht = Worksheets("sheet2")

FirstDate = DateSerial(Year(Date), Month(Date), 1)
LastDate = DateSerial(Year(Date), Month(Date) + 1, 0)

LastRow = oWkSht.Range("A100000").End(xlUp).Row

Sheets("sheet1").Range("g2").Value = Application.WorksheetFunction.SumIfs(oWkSht.Range("e2:e" & LastRow), oWkSht.Range("a2:a" & LastRow), ">=" & CLng(FirstDate), oWkSht.Range("a2:a" & LastRow), "<=" & CLng(LastDate))

End sub

SamT
08-16-2020, 11:27 AM
Try not CLng-ing the Dates in the formula

maghari
08-16-2020, 12:55 PM
thanks sam but it doesn't show the result in sheet2 :think:

SamT
08-16-2020, 02:10 PM
Well, the code says to place the result on sheet1 G2

maghari
08-16-2020, 02:23 PM
sorry sam about it thank for make attention me