excel - VBA Declare date/time data -


i ve got hourly data , when try execute following code runtime error 91.the format of data in cyc sheet example #07/07/2009 23:00:00# (at row 194), when enter dt automatically converts #7/7/2009 11:00:00 pm#. (please note shtcyc , shtco have been declared , set).

dim dt date dt = #7/7/2009 11:00:00 pm#         shtcyc.activate     'finds day     set rng = shtcyc.range("a3:a1514").find(dt, , xlvalues)     'copies dates     shtcyc.range("a" & rng.row - 191 & ":a" & rng.row + 24).copy (this debug highlights)     shtco.range("b10").pastespecial paste:=xlpastevalues 

anyone got ideas..? many many thanks!

well not problem see. see code below.

  1. to find dates have use datevalue because of various formatting reasons.
  2. you need check if value found
  3. you need check if rng.row falls in specific range

i have explained in comments. let me know if still have questions.

sub sample()     dim dt date     dim shtcyc worksheet     dim rng range      dt = #7/7/2009 11:00:00 pm#      set shtcyc = activesheet '<~~ change relevant sheet      shtcyc         set rng = .range("a3:a1514").find(what:=datevalue(dt), lookin:=xlformulas)          '~~> check if match found         if not rng nothing             '~~> check required because if rng.row <=191 or >=1048552?             '~~> understand have mentioned range "a3:a1514"             '~~> tom if use .cells then?             '~~> rng.row - 191 / rng.row + 24 give error in case             if rng.row > 191 or rng.row < (.rows.count - 24)                 .range("a" & rng.row - 191 & ":a" & rng.row + 24).copy                 'shtco.range("b10").pastespecial paste:=xlpastevalues             end if         else             msgbox "match not found"         end if     end end sub 

tested in excel 2013. worksheet looks this.

enter image description here


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -