0

次のマクロがあります。これは、数字の合計が 7 桁になるまで、数字の文字列の先頭にいくつかのゼロを追加します。現在、A列のみを実行します。選択した列のマクロを実行したいので、実行する必要があるすべての列を常にカットアンドペーストして再カットアンドペーストする必要はありません。何か案は?

Sub AddZeroes1()
'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
4

3 に答える 3

1

MyCol 行のみを変更します。

Sub AddZeroes1()
Dim cl As Range
Dim i As Long, endrow As Long
Dim MyCol As String
MyCol = "A"
Application.ScreenUpdating = False
    Columns(MyCol & ":" & MyCol).NumberFormat = "@"
    endrow = ActiveSheet.Range(MyCol & "1048576").End(xlUp).Row
    For i = 1 To endrow - 1
        Set cl = Range(MyCol & i)
        With cl
            Do While Len(.Value) < 7
                .Value = "0" & .Value
            Loop
        End With
    Next i
Application.ScreenUpdating = True
End Sub

未検証

于 2013-08-21T16:04:47.283 に答える