I'm a newcomer on this site, found a lot of solutions in the past as a passive reader, but now I'm stuck with something in VBA.
So, I want to write in excel in a specific cell, a formula that calls a function (already in my code). I want it to be written as a formula, and not just as a result.
The function that I call is the interpolate function.
When I write this piece of code, it works :
ActiveSheet.Cells(2, 8).Formula = "=Interpolate(J11:J43,K11:K46,F3,FALSE)"
Now, I want to make a part of the formula variable
ActiveSheet.Cells(2, 8).String = "=Interpolate(J11:J"&Lastrow1&",K11:K46,F3,FALSE)"
In my code Lastrow1 = 43
and is undefined
I also tried this, no chance
ActiveSheet.Cells(2, 8).String = "=Interpolate(J11:J""&Lastrow1&"",K11:K46,F3,FALSE)"
Or this :
ActiveSheet.Cells(2, 8).String = "=Interpolate(J11:J"""&Lastrow1&""",K11:K46,F3,FALSE)"
Any help on how to do that ?
I tried using this piece of code
Dim Lastrow1 as long
Lastrow1 = 43
Worksheets("YourSheetName").Cells(2, 8).Formula = "=Interpolate(J11:J""&Lastrow1&"",K11:K46,F3,FALSE)"
It doesn't work neither, I have the error message "Application-defined or object-defined error".
You need to use the Cells(2, 8).Formula
.
Try to stay away from ActiveSheet
, instead use the qualifed version Worksheets("YourSheetName").Cells(2, 8).Formula
...
Try the little piece of code to demonstrate how to get it to work:
Sub FormulaLastRow()
Dim Lastrow1 As Long
Lastrow1 = 43 ' <-- just for tests
Worksheets("TEST").Cells(2, 8).Formula = "=Interpolate(J11:J" & Lastrow1 & ",K11:K46,F3,FALSE)"
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.