格式化工作表时,下一步没有错误

时间:2012-09-19 06:33:58

标签: vba excel-vba excel

我是VBA的新手。我正在尝试在工作表上运行格式检查。

错误为Next without For error。我想要做的是检查行号33到58中的列H和O的数字格式错误。它在“Next n”处显示错误。

代码是这样的:

Public Sub PercentageCheck()
Dim CTRYname As String
Dim x As Integer
Dim n As Integer
Dim m As Integer


For n = 1 To 13

CTRYname = ThisWorkbook.Sheets("Country lookup").Range("A1").Offset(n, 0).Value

For m = 33 To 58
For x = 8 To 15

If x = 9 Or x = 10 Or x = 11 Or x = 12 Or x = 13 Or x = 14 Then
GoTo Names
Else
wkbCurr.Sheets(CTRYname).Activate
    If IsNumeric(wkbCurr.Sheets(CTRYname).Cells(x, m).Value) Then
        If wkbCurr.Sheets(CTRYname).Cells(x, m).Value > 9.99 Then
            wkbCurr.Sheets(CTRYname).Cells(x, m).Value = ">999%"
        ElseIf wkbCurr.Sheets(CTRYname).Cells(x, m).Value < -9.99 Then
            wkbCurr.Sheets(CTRYname).Cells(x, m).Value = "<-999%"
        End If
    End If


 Names:
 Next x


Next m


Next n

End Sub

您能否提供建议,以便更好地检查它。

2 个答案:

答案 0 :(得分:3)

第二个问题:suggest a better way to check it.
答:勤奋进行缩进。这很容易引起遗漏的代码行

Public Sub PercentageCheck()
    Dim CTRYname As String
    Dim x As Integer
    Dim n As Integer
    Dim m As Integer

    For n = 1 To 13
        CTRYname = ThisWorkbook.Sheets("Country lookup").Range("A1").Offset(n, 0).Value
        For m = 33 To 58
            For x = 8 To 15
                If x = 9 Or x = 10 Or x = 11 Or x = 12 Or x = 13 Or x = 14 Then
                    GoTo Names
                Else
                    wkbCurr.Sheets(CTRYname).Activate
                    If IsNumeric(wkbCurr.Sheets(CTRYname).Cells(x, m).Value) Then
                        If wkbCurr.Sheets(CTRYname).Cells(x, m).Value > 9.99 Then
                            wkbCurr.Sheets(CTRYname).Cells(x, m).Value = ">999%"
                        ElseIf wkbCurr.Sheets(CTRYname).Cells(x, m).Value < -9.99 Then
                            wkbCurr.Sheets(CTRYname).Cells(x, m).Value = "<-999%"
                        End If
                    End If
'  ---> Missing End If
Names:
            Next x
        Next m
    Next n
End Sub

顺便说一下,GoTo Names在此代码中并不是必需的。 wkbCurr.Sheets(CTRYname).Activate也不是。{1}}。把它们留下来,代码也一样。


<强>更新

根据您的评论及其显示的错误,我建议您使用更有意义的变量名称。这有助于避免这种错误。此外,谨慎使用With可以使您的代码更具可读性(也更快)

这是一个演示

的重构版本
Public Sub PercentageCheck()
    Dim CTRYname As String
    Dim col As Integer
    Dim n As Integer
    Dim rw As Integer

    For n = 1 To 13
        CTRYname = ThisWorkbook.Sheets("Country lookup").Range("A1").Offset(n, 0).Value
        With wkbCurr.Sheets(CTRYname)
            For rw = 33 To 58
            For col = 8 To 15
                If col < 9 Or col > 14 Then
                    With .Cells(rw, col)
                        If IsNumeric(.Value) Then
                            If .Value > 9.99 Then
                                .Value = ">999%"
                            ElseIf .Value < -9.99 Then
                                .Value = "<-999%"
                            End If
                        End If
                    End With
                End If
            Next col, rw
        End With
    Next n
End Sub

答案 1 :(得分:1)

您错过了END IF

If x = 9 Or x = 10 Or x = 11 Or x = 12 Or x = 13 Or x = 14 Then ... Else ...

缩进代码以提高可读性,这种事情将变得有些不言自明。 @ chris-neilsen的例子很棒。

与结束语句相比,计算开始语句将有助于缩小(这是我在此实例中调试代码所做的工作)。

使用突出显示相应开始/结束符号的IDE也会对您有帮助(但我不确定哪些IDE可用于VBA宏......如果有的话)。