Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 9
Default Will Macro before a Function like Now() ??

If I run a macro for a workbook with "Sub Workbook_Open()", will the macro
run first before the functions like "Now()" in the workbook updates. I am
triggering my macro off the date function of Now().

For example, I did work today Now(10/5/09). When I open the workbook
tomorrow, I want the macro to run with the date (10/5/09) first then have the
function update to Now(10/6/09).

Here is my code. It works but I haven't tried it over a long period.
ub Workbook_Open()
Dim cell As Range, intSheet As Integer
Dim wS1 As Worksheet
Dim wSA As Worksheet
Dim wS2 As Worksheet
Dim wSB As Worksheet
Dim wS3 As Worksheet
Dim wSC As Worksheet
Dim Date1 As Date
Dim Date2 As Date
Dim Date3 As Date


Set wS1 = ThisWorkbook.Sheets(1)
Set wSA = ThisWorkbook.Sheets("History 1")
Set wS2 = ThisWorkbook.Sheets(2)
Set wSB = ThisWorkbook.Sheets("History 2")
Set wS3 = ThisWorkbook.Sheets(3)
Set wSC = ThisWorkbook.Sheets("History 3")

' For intSheet = 1 To 3
If Sheets(3).Range("H1").Value < Date Then

wS1.Range("A12:K16").Copy wSA.Range("A12:K16")
wS2.Range("A12:K16").Copy wSB.Range("A12:K16")
wS3.Range("A12:K16").Copy wSC.Range("A12:K16")
Date1 = wS1.Range("H1")
Date2 = wS2.Range("H1")
Date3 = wS3.Range("H1")
wSA.Range("H1") = Date1
wSB.Range("H1") = Date2
wSC.Range("H1") = Date3

For intSheet = 1 To 3
For Each cell In Sheets(intSheet).Range("A12:K16")
If Not cell.Locked Then cell.ClearContents
Next cell

Next
End If
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default Will Macro before a Function like Now() ??

Don't fret over whether the macro will run before or after =NOW() gets
calculated.

Just store the date/time in some cell when the workbook is closed:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets(1).Range("Z100").Value = Now
End Sub


If your macro needs the old Now, have it look in Z100. If it needs the new
Now, let it calculate it.
--
Gary''s Student - gsnu200906


"Boiler-Todd" wrote:

If I run a macro for a workbook with "Sub Workbook_Open()", will the macro
run first before the functions like "Now()" in the workbook updates. I am
triggering my macro off the date function of Now().

For example, I did work today Now(10/5/09). When I open the workbook
tomorrow, I want the macro to run with the date (10/5/09) first then have the
function update to Now(10/6/09).

Here is my code. It works but I haven't tried it over a long period.
ub Workbook_Open()
Dim cell As Range, intSheet As Integer
Dim wS1 As Worksheet
Dim wSA As Worksheet
Dim wS2 As Worksheet
Dim wSB As Worksheet
Dim wS3 As Worksheet
Dim wSC As Worksheet
Dim Date1 As Date
Dim Date2 As Date
Dim Date3 As Date


Set wS1 = ThisWorkbook.Sheets(1)
Set wSA = ThisWorkbook.Sheets("History 1")
Set wS2 = ThisWorkbook.Sheets(2)
Set wSB = ThisWorkbook.Sheets("History 2")
Set wS3 = ThisWorkbook.Sheets(3)
Set wSC = ThisWorkbook.Sheets("History 3")

' For intSheet = 1 To 3
If Sheets(3).Range("H1").Value < Date Then

wS1.Range("A12:K16").Copy wSA.Range("A12:K16")
wS2.Range("A12:K16").Copy wSB.Range("A12:K16")
wS3.Range("A12:K16").Copy wSC.Range("A12:K16")
Date1 = wS1.Range("H1")
Date2 = wS2.Range("H1")
Date3 = wS3.Range("H1")
wSA.Range("H1") = Date1
wSB.Range("H1") = Date2
wSC.Range("H1") = Date3

For intSheet = 1 To 3
For Each cell In Sheets(intSheet).Range("A12:K16")
If Not cell.Locked Then cell.ClearContents
Next cell

Next
End If
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 126
Default Will Macro before a Function like Now() ??

A possible alternative to Gary's Student's suggestion would be for
your code to update the cell with the current date & time instead of
using the formula =NOW().

On Mon, 5 Oct 2009 13:14:01 -0700, Boiler-Todd
wrote:

If I run a macro for a workbook with "Sub Workbook_Open()", will the macro
run first before the functions like "Now()" in the workbook updates. I am
triggering my macro off the date function of Now().

For example, I did work today Now(10/5/09). When I open the workbook
tomorrow, I want the macro to run with the date (10/5/09) first then have the
function update to Now(10/6/09).

Here is my code. It works but I haven't tried it over a long period.
ub Workbook_Open()
Dim cell As Range, intSheet As Integer
Dim wS1 As Worksheet
Dim wSA As Worksheet
Dim wS2 As Worksheet
Dim wSB As Worksheet
Dim wS3 As Worksheet
Dim wSC As Worksheet
Dim Date1 As Date
Dim Date2 As Date
Dim Date3 As Date


Set wS1 = ThisWorkbook.Sheets(1)
Set wSA = ThisWorkbook.Sheets("History 1")
Set wS2 = ThisWorkbook.Sheets(2)
Set wSB = ThisWorkbook.Sheets("History 2")
Set wS3 = ThisWorkbook.Sheets(3)
Set wSC = ThisWorkbook.Sheets("History 3")

' For intSheet = 1 To 3
If Sheets(3).Range("H1").Value < Date Then

wS1.Range("A12:K16").Copy wSA.Range("A12:K16")
wS2.Range("A12:K16").Copy wSB.Range("A12:K16")
wS3.Range("A12:K16").Copy wSC.Range("A12:K16")
Date1 = wS1.Range("H1")
Date2 = wS2.Range("H1")
Date3 = wS3.Range("H1")
wSA.Range("H1") = Date1
wSB.Range("H1") = Date2
wSC.Range("H1") = Date3

For intSheet = 1 To 3
For Each cell In Sheets(intSheet).Range("A12:K16")
If Not cell.Locked Then cell.ClearContents
Next cell

Next
End If
End Sub

Regards,

Tushar Mehta
Microsoft MVP Excel 2000-present
www.tushar-mehta.com
Excel and PowerPoint tutorials and add-ins
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro with sum function RobRem Excel Worksheet Functions 1 April 25th 09 03:24 AM
Macro + IF Function? Gunti Excel Discussion (Misc queries) 1 December 9th 08 09:08 AM
Sub Macro vrs Function Macro Auto Start Pat Excel Discussion (Misc queries) 7 June 6th 07 09:53 PM
Do I need a macro or just a function? Kara Excel Worksheet Functions 3 April 28th 06 04:52 PM
Macro With IF Function marello Excel Worksheet Functions 0 November 10th 05 03:55 PM


All times are GMT +1. The time now is 02:18 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"