简体   繁体   English

Excel VBA如果value = 0,则不进行目标搜索

[英]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. 嗨,我是vba的新手,并且整日都在寻找使这项工作可行的方法。 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. 无论如何,当“ EX”中的相邻单元格> 0时,我需要我的GoalSeek宏来影响“ EW”列中的所有单元格以调整EP的值,如果“ EX”中的相邻单元格的值保留为空白或将值更改为0 “ =0。” EP“,” EW“和” EX“列彼此影响值。 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块的末尾没有End If If I have understand you, it should be after the cell loop. 如果我了解您,则应该在cell循环之后。 You also need to put Next j , to show program where to finish iteration of j loop. 您还需要放入Next j ,以显示程序在哪里完成j循环的迭代。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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