-1

数字の合計桁数が 7 桁になるまで、列 a のすべての数字にゼロを追加するマクロがあります。それは今日まで正常に機能しており、For1= i=1 To endrow = -1 という行でエラーが発生しています。私はそれが何を意味するのか理解できません。

コードは

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
4

2 に答える 2

2

i整数の上限 (32767) に達した可能性があります。Rowを返すLongので、のようにするi必要があります LongEndRow

Dim i As Long

于 2013-01-25T15:58:39.733 に答える
0

私はあなたのコードを書き直しました:

Sub test()
    Dim c As Range, rngTarget As Range
    Set rngTarget = Range("A:A").SpecialCells(xlCellTypeConstants, xlNumbers)
    rngTarget.NumberFormat = "@"
    For Each c In rngTarget
        c.Value = Left(c.Value & "0000000", 7)
    Next c
End Sub

より明確でより速く。

于 2013-01-29T08:11:47.923 に答える