简体   繁体   中英

VBA Array Formula and Goal Seek in IF Then Statement

This code doesnt run. It first looks into a range, if 0 does not exist then select least negative value, by using array formula. Then goal seek to set selected cell to 0 by changing value of a cell on same row, left 4 columns. If 0 exists do nothing. Any help appreciated

Sub Test()
    Dim Cel As Integer
        For Each Cel In ThisWorkbook.Sheets("Sheet1").Range("V17:V57")
            If Cel.Value <> 0 Then
            Cel.Find(Application.WorksheetFunction.FormulaArray(MAX(IF(V17:V57<=0,V17:V57),MIN(V17:V57))))
            Cel.Select
            Cel.GoalSeek Goal:=0, ChangingCell:=Cel.Offset(0, -4)
            End If
    Next Cel
End Sub

Looks like the previous comments/responses were deleted. Here's the latest version of the code, modified as per previous responses. Still doesn't run. I removed .Activate at the end of Cel.Find . Now there's a compile Syntax error. Any help apprciated

Sub Test()
Dim Cel As Range
    For Each Cel In ThisWorkbook.Sheets("Sheet1").Range("V17:V57").Cells
        If Cel.Value <> 0 Then

        Cel.Find(What:="MAX(IF(V17:V57<=0,V17:V57),MIN(V17:V57))", After:= _
        ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

        Cel.GoalSeek Goal:=0, ChangingCell:=Cel.Offset(0, -4)
        End If
    Next Cel
End Sub 

Just delete the parentheses in your Find method.

Use:

Cel.Find What:="MAX(IF(V17:V57<=0,V17:V57),MIN(V17:V57))", After:= _
ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False

Instead of:

Cel.Find(What:="MAX(IF(V17:V57<=0,V17:V57),MIN(V17:V57))", After:= _
ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

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