2017-12-05 9 views
0

をコンパイルされていません。
コンパイルエラー::次
のためにすることなく、しかし間違いなく、それぞれ次のための場合がありマイVBAエクセル2013のコードは

Private Sub Workbook_Open() 
Dim i As Integer 
Dim j As Integer 
Dim range1 As Integer 
Dim range2 As Integer 
range1 = 53 
range2 = 102 

For i = range1 To range2 
    For j = (range1 - 50) To (range2 - 50) 

     If Cells(2, i) = Cells(2, j) Then 
      If Cells(7, i) > Cells(7, j) Then 
      Cells(2, i).Interior.ColorIndex = 37 'Went up; Green 
      ElseIf Cells(7, i) = Cells(7, j) Then 
      Cells(2, i).Interior.ColorIndex = 37 'No change; Grey 
      Else 
      Cells(2, i).Interior.ColorIndex = 37 'Went down; Red 
     End If 

    Next j 
    If Cells(2, i).Interior.ColorIndex = 0 Then 'Hasn't Changed; No Fill 
    Cells(2, i).Interior.ColorIndex = 37 'New Song; Blue 
    End If 

Next i 
End Sub 

エラーが言って表示されます。
だからどこが間違っていたのですか?
注: '37'はフィラーの数字です。明るい青色で表示されます。一貫した方法でコードをインデント

+0

そこにある「End If」の前に?)、それゆえに 'If'の中に' Next'を見つけ出し、 'If'の中に' For'はありません。あなたの 'If'ステートメントの中であなたのコードをインデントすることを学び、そのような問題が非常に目立つようになります。 – YowE3K

+0

@ YowE3Kそうですが、スプレッドシートを読み込んでも何も起きません。セルB53:B102のセルの色がG3:G52の値に応じてG53:G102の値によって変化すると言えるのであれば、何も起こりません。しかし何も起こらなかった、あなたはその点で何が間違っていたのか知​​っていますか? – 7XCOLE

+0

あなたは 'B53:B102'と言いますが、あなたのコードは' BA2:CX2'を処理しています。 'Cells'のパラメータは' row、column'ですが、 'column、row'を使っていると思います。 ( 'Cells(2、i)'を 'Cells(i、2)'、 'Cells(i、" B ")'に変更します)。 – YowE3K

答えて

1

は以下を与える:

Private Sub Workbook_Open() 
    Dim i As Integer 
    Dim j As Integer 
    Dim range1 As Integer 
    Dim range2 As Integer 
    range1 = 53 
    range2 = 102 

    For i = range1 To range2 
     For j = (range1 - 50) To (range2 - 50) 

      If Cells(2, i) = Cells(2, j) Then 
       If Cells(7, i) > Cells(7, j) Then 
        Cells(2, i).Interior.ColorIndex = 37 'Went up; Green 
       ElseIf Cells(7, i) = Cells(7, j) Then 
        Cells(2, i).Interior.ColorIndex = 37 'No change; Grey 
       Else 
        Cells(2, i).Interior.ColorIndex = 37 'Went down; Red 
       End If 

       Next j ' <--- This Next has no For associated with it 

       If Cells(2, i).Interior.ColorIndex = 0 Then 'Hasn't Changed; No Fill 
        Cells(2, i).Interior.ColorIndex = 37 'New Song; Blue 
       End If 
      Next i 
     End Sub 

あなたは非常に迅速にNext jは現在Ifブロック内で、それに関連したFor文を持っていないことをインデントレベルで伝えることができます。これがあなたのエラーを得る理由です。

私はあなたが直前にEnd Ifを持っていることを意図していることを疑うことNext jので、あなたのコードは次のようになります。あなたが `どこか` j`ループのために(おそらくちょうど内If`を終了不足している

Private Sub Workbook_Open() 
    Dim i As Integer 
    Dim j As Integer 
    Dim range1 As Integer 
    Dim range2 As Integer 
    range1 = 53 
    range2 = 102 

    For i = range1 To range2 
     For j = (range1 - 50) To (range2 - 50) 

      If Cells(2, i) = Cells(2, j) Then 
       If Cells(7, i) > Cells(7, j) Then 
        Cells(2, i).Interior.ColorIndex = 37 'Went up; Green 
       ElseIf Cells(7, i) = Cells(7, j) Then 
        Cells(2, i).Interior.ColorIndex = 37 'No change; Grey 
       Else 
        Cells(2, i).Interior.ColorIndex = 37 'Went down; Red 
       End If 
      End If  

     Next j 

     If Cells(2, i).Interior.ColorIndex = 0 Then 'Hasn't Changed; No Fill 
      Cells(2, i).Interior.ColorIndex = 37 'New Song; Blue 
     End If 
    Next i 
End Sub