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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try replacing the semicolons with commas:
Code:
Range("A2").Formula = "=MID(CELL(""filename""),FIND(""."",CELL(""filename""))-1,1)"
 
Upvote 0
This looks like it should be returning the single character before the first '.' in the filename. Is that what you intend?

It works here ok if I replace the ;s (colons) with ,s (commas):
Code:
ActiveCell.FormulaR1C1 = "=MID(CELL(""filename""),FIND(""."",CELL(""filename""))-1,1)"
Maybe it's that?
What's the error you get? 1004?
 
Upvote 0
I need to insert function: "=IF(OR(WEEKDAY(C6)=7,WEEKDAY(C6)=1)"1","")"
i Tried this: range("D6").Formula = "=IF(OR(WEEKDAY(C6)=7,WEEKDAY(C6)=1)"1","")"
, but it says that there is an error and this ^ is supposed to be the end of the statement

:confused::LOL:(y):oops:
 
Last edited:
Upvote 0
Hi,may be ..

range("D6").Formula = "=IF(OR(WEEKDAY(C6)=7,WEEKDAY(C6)=1),"1","")"

just add comma there.
 
Upvote 0
Also need to double up the quotes..

range("D6").Formula = "=IF(OR(WEEKDAY(C6)=7,WEEKDAY(C6)=1),""1"","""")"
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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