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

最初の 2 番目の質問: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 の例は素晴らしいです。

最後のステートメントと比較して、最初のステートメントを数えることは、ピンチで役立ちます (そして、このインスタンスでコードをデバッグするために私がしたことです)。

Using an IDE that highlights corresponding start/end symbols would also help you (but I'm not sure what IDE's are available for VBA macros... if anything).

于 2012-09-19T06:38:13.430 に答える