简体   繁体   中英

Excel VBA if value = 0 then don't goal seek

Hi I'm new to vba and have been searching all day for a means to make this work. Anyways I need my GoalSeek macro to effect all cells within column "EW" to adjust the value of EP when the adjacent cell in "EX" > 0 and to leave blank or change value to 0 if the value of the adjacent cell in "EX" = 0. Columns "EP" "EW" and "EX" all effect each other's value. Below is my code that I've scraped together. I am receiving Compile Error: Block if without End if

Thanks for any help

    Private Sub CommandButton1_Click()

Dim StartTime As Double
Dim MinutesElapsed As String

'Remember time when macro starts
  StartTime = Timer


Dim lr As Long
Dim cell As Variant


Application.DisplayAlerts = False
Application.ScreenUpdating = False

    Range("A9").Select
        lr = Cells.Find("*", SearchORder:=xlByRows, SearchDirection:=xlPrevious).Row

For j = 9 To lr
   If Cells(j, "EX").Value > 0 Then
           Cells(j, "EP").GoalSeek Goal:=60, ChangingCell:=Cells(j, "EW")
               For Each cell In Range("EW9:EW" & lr)
                    cell.Value = WorksheetFunction.Round(cell.Value, 0)
                        Next cell

        Application.DisplayAlerts = True
Application.ScreenUpdating = True
'Determine how many seconds code took to run
  MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")

'Notify user in seconds
  MsgBox "This code ran successfully in " & MinutesElapsed & " minutes", vbInformation


End Sub

Before the text

Application.DisplayAlerts = True
Application.ScreenUpdating = True

you need to write:

end if
next j

Thus it shall look like this:

Next cell
end if
next j
Application.DisplayAlerts = True
Application.ScreenUpdating = True

As the error message says, you don't have End If at the end of If block. If I have understand you, it should be after the cell loop. You also need to put Next j , to show program where to finish iteration of j loop.

Private Sub CommandButton1_Click()

Dim StartTime As Double
Dim MinutesElapsed As String
Dim lr As Long
Dim cell As Variant

'Remember time when macro starts
StartTime = Timer

Application.DisplayAlerts = False
Application.ScreenUpdating = False

Range("A9").Select
lr = Cells.Find("*", SearchORder:=xlByRows, SearchDirection:=xlPrevious).Row

For j = 9 To lr
    If Cells(j, "EX").Value > 0 Then
        Cells(j, "EP").GoalSeek Goal:=60, ChangingCell:=Cells(j, "EW")
        For Each cell In Range("EW9:EW" & lr)
            cell.Value = WorksheetFunction.Round(cell.Value, 0)
        Next cell
    End If
Next j

Application.DisplayAlerts = True
Application.ScreenUpdating = True
'Determine how many seconds code took to run
MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")

'Notify user in seconds
MsgBox "This code ran successfully in " & MinutesElapsed & " minutes", vbInformation


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.

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