0

我是 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

您能否提供更好的检查方法的建议。

4

2 回答 2

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。只需将它们排除在外,代码就可以正常工作。


更新:

根据您的评论及其揭示的错误,我建议您使用更有意义的变量名称。这将有助于避免此类错误。此外,谨慎使用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
于 2012-09-19T06:38:48.287 回答
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 宏......如果有的话)。

于 2012-09-19T06:38:13.430 回答