Using the DateDiff function in VBA to get number of days between 2 dates -


so i'm trying 2 dates in excel sheet , use datediff function number of days between 2 dates. adding number of days , dividing the number of rows , average amount of days. far have total amount of days every row gets added , displayed on column "e" , number of rows placed on column "f". know close because @ 1 point worked dumb , changed , not. here code , excel sheet.

 sub getdays()      range("c1").select      until activecell.value = ""          date1 = datevalue(activecell.offset(1, 0).value)         date2 = datevalue(activecell.offset(1, 0).entirerow.cells(1, "d").value)          daycount = datediff("d", date1, date2) + daycount         activecell.offset(1, 0).entirerow.cells(1, "e").value = daycount          studentcount = studentcount + 1         activecell.offset(1, 0).entirerow.cells(1, "f").value = studentcount          activecell.offset(1, 0).select     loop   end sub! 

here snippet of sheet

the issue discovered when testing code loop comparing activecell value determine when exit, code operating on cell below activecell, result of offset(1,0) call. when loop on last line of data, activecell.value = "3/25/2015 10:52", next line of code trying populate date1 datevalue of null since offset down 1 row. throws type mismatch error.

i've adjusted code below, works me:

sub getdays()    range("c1").select    until activecell.value = ""      date1 = datevalue(activecell.value)     date2 = datevalue(activecell.offset(0, 1).value)      daycount = datediff("d", date1, date2) + daycount     activecell.offset(0, 2).value = daycount      studentcount = studentcount + 1     activecell.offset(0, 3).value = studentcount      activecell.offset(1, 0).select   loop  end sub 

i adjusted offset command looking @ same row @ times each loop. replaced "entirerow.cells(1, "d")" sections using column integer in offset().

you may need change second line to: range ("c2").select code work, depending on if data starts on row 1 or row 2.


Popular posts from this blog