简体   繁体   中英

Goal seek in VBA error "reference isn't valid"

I have a problem with the GoalSeek function in Excel's VBA.

It says "reference isn't valid", and I know that it refers to the reference r and c , cause if I put in numbers instead, it runs smooth.
However, if I split up the code, I can see that r =68 and c =7, which is the correct numbers. So it's when the reference is put into the Cells reference in the goal seek function it does not work and I get the error "reference isn't valid".
Are there some kind of Dim or Set that I am missing?

Sub MacroTest1()
    ActiveSheet.Calculate
    
    r = Range("LeverageRatioPeriod").Row
    c = WorksheetFunction.Match(Range("MaxAcquisitionYear"), Range("YearRange"), 0)
    
    Cells(r, c).GoalSeek Goal:=4, ChangingCell:=Range("MaxEV")

Please check what is entered in the cell Range("MaxEV"). If there is a formula there, please replace it with a simple value. In my case, this resolved a similar issue, and GoalSeek finally returned a value.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM