0

数字が合計 7 桁になるまで数字にゼロを追加する次のコードがあります。今までは問題なく動作していましたが、コードは次のとおりです。

Sub AddZeroes()
'Declarations
Dim i As Integer, j As Integer, endrow As Long
'Converts the A column format to Text format
Application.ScreenUpdating = False
Columns("A:A").Select
Selection.NumberFormat = "@"
'finds the bottom most row
endrow = ActiveSheet.Range("A1").End(xlDown).Row
'selects the top cell in column A
ActiveSheet.Range("A1").Select

'loop to move from cell to cell
For i = 1 To endrow - 1
            'Moves the cell down 1. Assumes there's a header row so really starts at row 2
            ActiveCell.Offset(1, 0).Select
            'The Do-While loop keeps adding zeroes to the front of the cell value until it     hits     a length of 7
Do While Len(ActiveCell.Value) < 7
                            ActiveCell.Value = "0" & ActiveCell.Value
            Loop
Next i
Application.ScreenUpdating = True
End Sub

コードは A 列をループし、数値の合計数が 7 でない場合は、先頭に 0 を追加します。コードの部分にエラーが表示されています

FOR I = 1 TO ENDROW - 1

何が悪いのか理解できないようです。この部分はマクロに、リストの最後に到達すると空白を見つけて 1 つ上に移動し、最後の番号で停止し、今日まで機能していることをマクロに伝えます。

4

1 に答える 1

1

iその変数でオーバーフローを引き起こしている可能性が高い整数値を使用しています。

これを試して:

Option Explicit
Sub AddZeroes()
'Declarations
Dim cl As Range
Dim i As Long, endrow As Long

    Application.ScreenUpdating = False
        'Converts the A column format to Text format
        Columns("A:A").NumberFormat = "@"
        'finds the bottom most row
        endrow = ActiveSheet.Range("A1048576").End(xlUp).Row
        '## Or, for Excel 2003 and prior: ##'
        'endrow = ActiveSheet.Range("A65536").End(xlUp).Row

        'loop to move from cell to cell
        For i = 1 To endrow - 1
            Set cl = Range("A" & i)
            With cl
            'The Do-While loop keeps adding zeroes to the front of the cell value until it     hits     a length of 7
                Do While Len(.Value) < 7
                    .Value = "0" & .Value
                Loop
            End With
        Next i
    Application.ScreenUpdating = True
End Sub
于 2013-05-08T18:03:58.403 に答える