Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88030

VBA: Range.Formula Returns Application Defined or Object Defined Error

$
0
0

For my code, I am trying to find the difference between an objects value from two different days.

Sub GoingBack()

numberCube = InputBox("Which file are we going back to?")
numberYest = numberCube - 1

Workbooks.Open ("C:\Users\user\Downloads\file ("& numberCube & ").xlsx")
Workbooks.Open ("C:\Users\user\Downloads\file ("& numberYest & ").xlsx")

Set Work1 = Workbooks("file ("& numberCube & ").xlsx")
Set Work2 = Workbooks("file ("& numberCube - 1 & ").xlsx")

'Add the Time Difference Column (AA--27)
LastRow67 = Work1.Sheets("67").Cells(Rows.Count, 2).End(xlUp).Row
Work1.Sheets("67").Cells(1, 27).Value = "Time Clock Difference"
Work1.Sheets("67").Cells(1, 27).FormulaR1C1 = "=RC[-15]-VLOOKUP(RC[-21], '[file ("& numberYest & ").xlsx]67'!$F:$L, 7, FALSE)"
Work1.Sheets("67").Range("AA2").Select
Selection.AutoFill Destination:=Range("AA2:AA"& LastRow67)

Work1.Close savechanges:=True
Work2.Close savechanges:=True

End Sub

The line that is throwing the "Application Defined or Object Defined" error is:

Work1.Sheets("67").Cells(1, 27).FormulaR1C1 = "=RC[-15]-VLOOKUP(RC[-21], '[file ("& numberYest & ").xlsx]67'!$F:$L, 7, FALSE)"

I have tried using Range.Formula, and that threw the error as well.

Work1.Sheets("67").Range("AA2").Formula = "=L2-VLOOKUP(F2, '[file ("& numberYest & ").xlsx]67'!$F:$L, 7, FALSE)"

Any help would be appreciated. Thank you so much.

EDIT: I typed in the formula in Excel, and it works. I recorded the inputting of the formula, and the below is the result. I clicked/referenced columns F through L, so I'm not sure why it is only displaying C6:C12 below.

ActiveCell.FormulaR1C1 = "=RC[-15]-VLOOKUP(RC[-21],'[file.xlsx]67'!C6:C12,7,FALSE)"

Viewing all articles
Browse latest Browse all 88030

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>