Macro to insert excel function in cell

Feenstra

New Member
Joined
Sep 21, 2010
Messages
6
Hey there,

I would like my macro to insert a function into a cell. The function works in Excel but if I use the function in my macro it gives an error. Any idea how to write it correctly?

Sub Prepare_data()

Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "Version"
Range("A2").Select
ActiveCell.FormulaR1C1 = "=MID(CELL(""filename"");FIND(""."";CELL(""filename""))-1;1)"

LastRow = ActiveSheet.UsedRange.Rows.Count
Range("A2").AutoFill Destination:=Range("A2:A" & LastRow)

End sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Could you please provide code to replace the value in the cell G1 (value is 'Jan') with the function : =DATE(YEAR(TODAY()),1,1)
and values in cells H1:AD1 with function =DATE(YEAR(G1),MONTH(G1)+1,DAY(G1))
Thank you in advance
 
Upvote 0
Try this, edit sheet name.

Code:
[COLOR=darkblue]Sub[/COLOR] InsertDates()
   [COLOR=darkblue]With[/COLOR] Sheets("[COLOR=red]Sheet1[/COLOR]")
      .Range("G1").Formula = "=DATE(YEAR(TODAY()),1,1)"
      .Range("H1:AD1").Formula = "=DATE(YEAR(G1),MONTH(G1)+1,DAY(G1))"
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0
Why formulas?

Code:
Sub InsertDates()
   With Sheets("Sheet1")
      .Range("G1") = DateSerial(Year(Now), 1, 1)
      .Range("G1:AD1").DataSeries Type:=xlChronological, Date:=xlMonth
   End With
End Sub
 
Upvote 0
Why formulas?

Code:
Sub InsertDates()
   With Sheets("Sheet1")
      .Range("G1") = DateSerial(Year(Now), 1, 1)
      .Range("G1:AD1").DataSeries Type:=xlChronological, Date:=xlMonth
   End With
End Sub

Following on from the OP. Didn't realise it was a different guy.
 
Upvote 0

Forum statistics

Threads
1,223,276
Messages
6,171,139
Members
452,381
Latest member
Nova88

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