2016-10-18 15 views
0

こんにちは私はvbaを初めて使い、この仕事をするために一日中検索してきました。とにかく、 "EX"> 0の隣接セルの場合はEPの値を調整し、 "EX"の隣接セルの値が0の場合は空白または値を0に変更するために、 "EW"列内のすべてのセルに影響を与えるGoalSeekマクロが必要です"= 0.列" EP "" EW "と" EX "はすべて互いに影響し合います。以下は、私が一緒に掻き集めたコードです。あなたが書く必要があるテキスト値が0の場合はExcel VBA、それ以外の場合はゴールシーク

Application.DisplayAlerts = True 
Application.ScreenUpdating = True 

前に任意のヘルプ

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 

答えて

0

ため

おかげエンドがなければ、場合ブロック:

end if 
next j 

それはこのように私は、コンパイルエラーを受け付けております次のようになります。

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

エラーメッセージには、End IfIfブロックの末尾にないことが示されています。私があなたを理解していれば、それはcellループの後でなければなりません。ループjの反復を終了する場所をプログラムに表示するには、Next 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 
関連する問題