简体   繁体   中英

Goal Seek in multiple rows (with empty rows in the middle)

I want to perform a goal seek for several rows (I don't know exactly how many - in the code I have below it stops in row 100). The thing is, for example , if I have 10 rows, row number 5 can be empty. So I want it to skip from row 4 to row 6, then continue, then "jump" another empty row if they exist.

I want to set cell M2 to 0 by changing the value of cell K2. The same for row 3, row 4, etc. and I want it to skip empty rows.

Right now I just have this... a simple case

Sub GSeek()



Dim i As Long
For i = 2 To 100


    range("M" & i).GoalSeek Goal:=0, ChangingCell:=range("K" & i)


Next



End Sub

Try this

Dim i As Long

For i = 2 To 100
    If range("M" & i).Value <> "" Then
        range("M" & i).GoalSeek Goal:=0, ChangingCell:=range("K" & i)
    End If
Next i

Each time it just makes sure that the cell isn't empty. If it is, it'll just move to next i

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