Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / September 2007

Tip: Looking for answers? Try searching our database.

Stamp a Cell with Time Q

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sean - 16 Sep 2007 20:04 GMT
I have listed in Sheet5 A1:A7, days i.e. Monday (A1) through Sunday
(A7). How would I via code, stamp in Sheet5 B1:B7 the current time
when an action is performed, depending on the Day of the week - 1
(minus one)?

For example, now is Sunday at 19:58, thus when I perfom an action I
wish B6 (Sat)  to be poplulated with 19:58, tomorrow if I perform the
same action, say at 10:07, I want B7 (Sun) to be populated. (the info
I'm reporting on is always a day before the actual day)

The only 'quirk' I want is that if the time is after 21:00 on the day
I want the appropriate cell in B1:B7 left blank

Hope the above is not too confusing

Thanks
Joel - 16 Sep 2007 22:14 GMT
A worksheet change function like the one below should work. Format sheet 5
column B for time (any time format you prefer).  I placed the code below on
sheet 1 in the VBA project window.  Worksheet change must be placed in every
worksheet you want the action to take place.  If you place the code on sheet
5 then you have to add an if statement to ignore range A1:B7.

Sub worksheet_change(ByVal Target As Range)

Myrow = Weekday(Now() - 1, vbMonday)
If Hour(Now()) < 21 Then
  Sheets("Sheet5").Cells(Myrow, "B") = _
     Now()
End If
End Sub

> I have listed in Sheet5 A1:A7, days i.e. Monday (A1) through Sunday
> (A7). How would I via code, stamp in Sheet5 B1:B7 the current time
[quoted text clipped - 12 lines]
>
> Thanks
Bill Renaud - 17 Sep 2007 00:01 GMT
Put the following code in a standard code module, then call it from a
button on a toolbar or worksheet. It formats the cell's number format
automatically as each value is entered, or you could simply format the
cells in column B ahead of time.

'----------------------------------------------------------------------
Public Sub StampTime()
 Dim dblNow As Double
 Dim dblTimeNow As Double
 Dim lngDayOfWeek As Long
 Dim lngStampRow As Long
 Dim rngStampCell As Range

 dblNow = Now

 With WorksheetFunction
   dblTimeNow = dblNow - .RoundDown(dblNow, 0)
   lngDayOfWeek = .Weekday(.RoundDown(dblNow, 0))
 End With

 lngStampRow = ((lngDayOfWeek + 4) Mod 7) + 1

 Set rngStampCell = Sheet5.Cells(lngStampRow, 2)

 With rngStampCell
   If (dblTimeNow > (21# / 24#)) _
     Then
       .ClearContents
     Else
       .Value = dblNow
       .NumberFormat = "hh:mm"
     End If
 End With
End Sub
Signature

Regards,
Bill Renaud

Joel - 17 Sep 2007 00:22 GMT
Bill: How does your code automaticaly update sheet 5 times.  Your code only
works if somebody manually runs the macro.  Weekday is a VBA function and
doesn't have to be called as a worksheetfunction.

> Put the following code in a standard code module, then call it from a
> button on a toolbar or worksheet. It formats the cell's number format
[quoted text clipped - 30 lines]
>   End With
> End Sub
Sean - 17 Sep 2007 10:59 GMT
> Bill: How does your code automaticaly update sheet 5 times.  Your code only
> works if somebody manually runs the macro.  Weekday is a VBA function and
[quoted text clipped - 39 lines]
>
> - Show quoted text -

Thanks Guys, I'll use the code as part of another Macro, thus it will
always be manually instigated
Bill Renaud - 17 Sep 2007 19:27 GMT
I guess it wasn't clear to me that the OP wanted these cells
automatically updated every time somebody entered something in ANY cell,
so I left it this way to provide more flexibility. If it needs to be
called anytime any cell is changed on this same worksheet (or any other
worksheet, for that matter), then put this code in the Worksheet_Change
event handler for this (or any other) worksheet (it simply calls the
StampTime routine):

'----------------------------------------------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)
 StampTime
End Sub

As a matter of programming flexibility, this is a better way to do event
routines anyway. I can print out the standard code module, and have
virtually all of the code, without having to go check each worksheet and
chart code module.
--
Regards,
Bill Renaud
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.