Macro to insert formula in variable cell

pug

New Member
Joined
Jul 24, 2007
Messages
3
I need a macro to insert =now() into whichever cell is active and then to turn the formula into an absolute.

I tried recording a macro but had two problems:

The macro would only work in the cell that was active at the time of recording and it put the date and time of when the macro was created and not the date and time when the macro was run.

I recorded the keystrokes =now()f9Enter where f9 was the f9 key

any help will be appreciated
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Thanks Neil

That got the time in but I need the date & time

I also need to know how to do it with other formula, like how do you include the formula into the macro as apposed to the results of the formula

cheers

pug
 
Upvote 0
OK,

To insert date and time...
Code:
Sub Insert_Time() 
ActiveCell = Now
End Sub
To insert a formula...
Code:
Sub Insert_Formula()
ActiveCell.Formula = "=SUM(A:A)"
End Sub
Note that formulae with quote marks ("") can be tricky in VBA, e.g the formula =VLOOKUP("myname",A:B,2,FALSE) would become
Code:
Sub Insert_formula()
ActiveCell.Formula = "=VLOOKUP(""myname"",A:B,2,FALSE)"
End Sub
To insert a formula as a value, there are several methods. Probably the easiest for someone new to VBA is as follows:
Code:
Sub Insert_Value
With ActiveCell
.Formula = "=VLOOKUP(""myname"",A:B,2,FALSE)"
.Formula = .Value
End With
End Sub
 
Upvote 0
Neil

You are a gem - thanks for that.

I started using Lotus123 in 1979 and could make it sing and dance but Excel's macro language I find most difficult.

But you have proved you can teach an old dog new tricks.

again, thanks

Pug
 
Upvote 0

Forum statistics

Threads
1,222,118
Messages
6,164,072
Members
451,870
Latest member
Nikhil excel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top