Adding function in a cell while recording a macro - why not?

Patrick Niedermeyer

New Member
Joined
Apr 10, 2020
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Why doesn't the function that I entered into a cell return the requested data, specifically if I wrote the function in the cell while recording a macro?

-P.

Sub Auto_Timesheet()
'
' Auto_Timesheet Macro
'
' Keyboard Shortcut: Ctrl+v
'
Columns("F:F").Select
Range("F2").Activate
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("F2").Select
ActiveCell.FormulaR1C1 = "Cost Code Only"
Range("F3").Select
ActiveCell.FormulaR1C1 = "=left(E3,4)"
Range("F3").Select
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
When you record entering a formula, what gets recorded is the formula you entered, not the value that the formula returns.
This is the section here with your formula:
VBA Code:
Range("F3").Select
ActiveCell.FormulaR1C1 = "=left(E3,4)"
 
Upvote 0
It either needs to be
VBA Code:
ActiveCell.FormulaR1C1 = "=left(rc[-1],4)"
or
VBA Code:
ActiveCell.Formula = "=left(E3,4)"
 
Upvote 0
Fluff,

I understand there is different function to bring those first four characters, but I tried both lines of code, but and it doesn't produce what I was looking for. I've included an example data file that's been greatly simplified. In it you can see that I was trying to copy the left 4 characters from the B column to the C column. When I tried the replacements for my selection code shown above, it doesn't do what I need.

-P.
Example data.jpg
 
Upvote 0
So, is the issue that it is putting the formula in the cell as Text, and not as a formula so it isn't returning a value?
If it is doing that, it means that column C is formatted as Text. Change the format of column C to General or Number (before entering the formulas), and it will evaluate the formulas correctly.
 
Upvote 0
If the problem is that it's always looking at the same cell, try
VBA Code:
Sub Patrick()
   Range("F:F").EntireColumn.Insert
   Range("F2").Value = "Cost Code Only"
   Range("F3:F" & Range("E" & Rows.Count).End(xlUp).Row).FormulaR1C1 = "=left(rc[-1],4)"
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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