我的VBA Excel 2013代码无法编译

时间:2017-12-05 02:46:13

标签: excel-vba excel-2013 vba excel

当我运行此代码时:

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

出现错误说:
编译错误:下一个没有For 但是每个Next都肯定有For 那我哪里出错了?
注意:' 37&只是填充数字,我知道它显示为浅蓝色。

1 个答案:

答案 0 :(得分: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在当前For块中没有与If相关联的End If语句。这就是你收到错误的原因。

我怀疑您打算在Next j之前设置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 ,因此您的代码看起来像:

function shipImages(num,rotation){
  var im = shipImage(num);
  //create hidden canvas...
  var hc = document.createElement("canvas");
  hc.width = 256;
  hc.height = 256;
  hc.style = "display:none";
  document.body.appendChild(hc);
  var hcc = hc.getContext("2d");
  //now to draw it rotated
  var x = hc.width / 2;
  var y = hc.height / 2;
  var angleInRadians = rotation * (Math.PI / 180);

  hcc.translate(x, y);
  hcc.rotate(angleInRadians);
  hcc.drawImage(im, -hc.width / 2, -hc.height / 2, im.width, im.height);
  hcc.rotate(-angleInRadians);
  hcc.translate(-x, -y);
  var b64 = hc.toDataURL();
  document.getElementById(""+num).src = b64;
  hc.remove();
  return document.getElementById(""+num)
}