2016-05-13 14 views
1

このコードを書きました。私はエラーを得た理由を理解しない:終了ブロックなしの場合エラー

Sub mytest() 

Dim i As Integer 
Dim s As Integer 
Dim j As Integer 
Dim p As Integer 
Dim k As Integer 
s = 0 

With Worksheets("mysheet") 
.Range("B28:B75").Select 

For i = 28 To 75 
     If Cells(i, 2).Value > 0 Then Cells(i, 2).Interior.Color = RGB(255, 255, 255) 
     s = s + 1 
     End If 
Next i 

.Range("A28:A75").Select 

For j = 28 To 75 

    If Cells(i, 2).Value = 0 Then Cells(i, 2).Interior.Pattern = xlPatternLightDown 
    Cells(i, 2).Interior.Color = RGB(255, 255, 255) 
    End If 

Next j 

p = 75 - s 
For k = 1 To s 
    Cells(s + k, 1).Interior.Color = RGB(18, 0, 0) 
Next k 

End With 

END IF句波平が省略された場合はブロックせずに終了した場合。なぜ私はエラーが発生したのか分かりません

+1

'Then'on改行の後に文を移動してみてください –

+1

あなたのエラーは' If If End If If'ではなく 'End If If if Block if'です。あなたは欠けている 'End If'を持っていない、あなたはあまりにも多すぎる;)(下記のMacro manの回答か[this answer](http://stackoverflow.com/a/22580564/5350831)を参照)。 –

答えて

1

改行の後に文を移動してください。Then IMHO VBAは、その後にコードで使用すると、1行の文が必要になるため、end ifのコードを使用しないで次の行にこのエラーがスローされます。また、VBA hereの詳細については、範囲とセルの仕様を詳しくお読みください。

しかし、あなたは、単一の行にIfステートメントを記述する場合、あなたのエラーで使用するために、この

Sub mytest() 

Dim i As Integer 
Dim s As Integer 
Dim j As Integer 
Dim p As Integer 
Dim k As Integer 
s = 0 

With Worksheets("mysheet") 
.Range("B28:B75").Select 

For i = 28 To 75 
     If Cells(i, 2).Value > 0 Then 
     Cells(i, 2).Interior.Color = RGB(255, 255, 255) 
     s = s + 1 
     End If 
Next i 

.Range("A28:A75").Select 

For j = 28 To 75 

    If Cells(i, 2).Value = 0 Then 
    Cells(i, 2).Interior.Pattern = xlPatternLightDown 
    Cells(i, 2).Interior.Color = RGB(255, 255, 255) 
    End If 

Next j 

p = 75 - s 
For k = 1 To s 
    Cells(s + k, 1).Interior.Color = RGB(18, 0, 0) 
Next k 

End With 
4

のようなものは、この:

If Foo = Bar Then FooBar() 

あなたは行動するのでEnd Ifを使用する必要はありません同じ行で実行されるので、ステートメントの末尾はです。(コンパイラは、同じ行のThenの後に何かを知っています。したがって、条件付きのc

If Foo = Bar Then 
    FooBar() 
End If 

次にあなたが明示的に持ってのための他の方法がないので、条件付きコードがEnd Ifを使用して終了コンパイラに指示:ODEを使用すると、別の行にアクションを配置した場合)

を終了しますそれは知っている。

関連する問題