Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 68
Default Macro to Enter Formula in a Cell

Excel 2003

I have the following two lines in a simple macro. The macro should put the
date for last Monday in the selected cell.

SubDays = (Weekday(Date) - vbMonday)
ActiveCell.Formula = "=Today()"

The obvious problem is that "=Today()" will put the current date in the
cell, instead of the date for last Monday. So I need to add a correction
factor, and SubDays (for "subtract days") is intended to make that
correction. Conceptually, the second line above should be like the
following:

ActiveCell.Formula = "=Today()-SubDays"

I know that this syntax does not work, but conceptually what would happen is
that on Tuesday one day would be subtracted, on Wednesday 2 days would be
subtracted, etc. The result would be that no matter what day of the week it
is, the date for the last Monday would always be placed in the active cell.
The macro then turns the formula in the active cell into a value.

I can create a variable in VBA that holds the correct date by inserting:
MyDate = Date - SubDays. But again, I cannot figure out how to get that
value written to the active cell.

Can someone show me the syntax to make this concept work?

--Tom


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Macro to Enter Formula in a Cell

=TODAY()-(WEEKDAY(TODAY(),2)-1)

--
Regards,
Tom Ogilvy




"Thomas M." wrote in message
...
Excel 2003

I have the following two lines in a simple macro. The macro should put
the date for last Monday in the selected cell.

SubDays = (Weekday(Date) - vbMonday)
ActiveCell.Formula = "=Today()"

The obvious problem is that "=Today()" will put the current date in the
cell, instead of the date for last Monday. So I need to add a correction
factor, and SubDays (for "subtract days") is intended to make that
correction. Conceptually, the second line above should be like the
following:

ActiveCell.Formula = "=Today()-SubDays"

I know that this syntax does not work, but conceptually what would happen
is that on Tuesday one day would be subtracted, on Wednesday 2 days would
be subtracted, etc. The result would be that no matter what day of the
week it is, the date for the last Monday would always be placed in the
active cell. The macro then turns the formula in the active cell into a
value.

I can create a variable in VBA that holds the correct date by inserting:
MyDate = Date - SubDays. But again, I cannot figure out how to get that
value written to the active cell.

Can someone show me the syntax to make this concept work?

--Tom



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 68
Default Macro to Enter Formula in a Cell

Thanks! That works. Also, I noticed that the formula can be simplified
slightly third WEEKDAY return type. So...

=TODAY()-(WEEKDAY(TODAY(),3))

also works.

--Tom

"Tom Ogilvy" wrote in message
...
=TODAY()-(WEEKDAY(TODAY(),2)-1)

--
Regards,
Tom Ogilvy




"Thomas M." wrote in message
...
Excel 2003

I have the following two lines in a simple macro. The macro should put
the date for last Monday in the selected cell.

SubDays = (Weekday(Date) - vbMonday)
ActiveCell.Formula = "=Today()"

The obvious problem is that "=Today()" will put the current date in the
cell, instead of the date for last Monday. So I need to add a correction
factor, and SubDays (for "subtract days") is intended to make that
correction. Conceptually, the second line above should be like the
following:

ActiveCell.Formula = "=Today()-SubDays"

I know that this syntax does not work, but conceptually what would happen
is that on Tuesday one day would be subtracted, on Wednesday 2 days would
be subtracted, etc. The result would be that no matter what day of the
week it is, the date for the last Monday would always be placed in the
active cell. The macro then turns the formula in the active cell into a
value.

I can create a variable in VBA that holds the correct date by inserting:
MyDate = Date - SubDays. But again, I cannot figure out how to get that
value written to the active cell.

Can someone show me the syntax to make this concept work?

--Tom





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
Using a macro to enter a formula Tami Excel Worksheet Functions 4 September 24th 09 11:47 AM
Need the formula or macro. If i enter today date in the cell (Row 1,Column 2) and on tab out, the column 1 cell should be filled with "corresponding Day" of the date kakasay Excel Discussion (Misc queries) 1 January 22nd 07 12:31 PM
Need Formula or macro. If i enter today date in the cell (Row 1,Column 2) and on tab out, the column 1 cell should be filled with "corresponding Day" of the date kakasay Excel Discussion (Misc queries) 1 January 22nd 07 12:31 PM
Enter formula in a macro Annette Excel Programming 3 May 1st 06 06:37 PM
Can you enter a formula in a cell to run a macro? Nevaeh Excel Worksheet Functions 2 February 14th 05 11:51 PM


All times are GMT +1. The time now is 04:09 PM.

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"