3

Excel VBAで行全体を削除する方法についていくつかのスニペットを使用しようとしましたが、「IsNumber」検証を含めるように変更することはできません.

次のようなアクティブな領域を選択できる必要があります。

Set r = ActiveSheet.Range("A1:C10")

そして、行ごとに(そして領域のすべてのセルをチェックして)進むにつれて、セルに数字がある場合は行全体を削除します。

例えば:

NA NA NA 21
NA 22 NA 44
00 NA NA NA
NA NA NA NA
55 NA NA NA

マクロは、4 番目の行を除いて、すべての行を削除します。

NA NA NA NA
4

3 に答える 3

5

好きなものを選んでください :)

方法1(試行およびテスト済み)

これはSpecialCells、番号のある行を識別するために使用します。

Sub Sample()
    Dim ws As Worksheet
    Dim rng As Range

    On Error GoTo Whoa

    Set ws = Sheets("Sheet1")

    With ws
        Set rng = .Cells.SpecialCells(xlCellTypeConstants, xlNumbers).EntireRow

        rng.ClearContents '<~~ or rng.Clear if cells have formatting

        .Cells.Sort Key1:=.Range("A1")
    End With

    Exit Sub
Whoa:
    MsgBox Err.Description
End Sub

方法2(試行およびテスト済み)

これはループを使用Count()し、数値をチェックします

Sub Sample()
    Dim ws As Worksheet
    Dim delrange As Range
    Dim lRow As Long, i As Long

    On Error GoTo Whoa

    Set ws = Sheets("Sheet1")

    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        For i = 1 To lRow
            If Application.WorksheetFunction.Count(.Rows(i)) > 0 Then
                If delrange Is Nothing Then
                    Set delrange = .Rows(i)
                Else
                    Set delrange = Union(delrange, .Rows(i))
                End If
            End If
        Next i

        If Not delrange Is Nothing Then delrange.Delete
    End With

    Exit Sub
Whoa:
    MsgBox Err.Description
End Sub

方法3(試行およびテスト済み)

これは自動フィルターを使用します。行1にヘッダーがあり、範囲内に空白のセルがないと想定しています。

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long, lCol As Long, i As Long
    Dim ColN As String

    On Error GoTo Whoa

    Set ws = Sheets("Sheet1")

    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row
        lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

        For i = 1 To lCol
            '~~> Remove any filters
            .AutoFilterMode = False
            ColN = Split(.Cells(, i).Address, "$")(1)

            '~~> Filter, offset(to exclude headers) and delete visible rows
            With .Range(ColN & "1:" & ColN & lRow)

                .AutoFilter Field:=1, Criteria1:=">=" & _
                Application.WorksheetFunction.Min(ws.Columns(i)), _
                Operator:=xlOr, Criteria2:="<=" & _
                Application.WorksheetFunction.Max(ws.Columns(i))

                .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
            End With

            '~~> Remove any filters
            .AutoFilterMode = False
        Next
    End With

    Exit Sub
Whoa:
    MsgBox Err.Description
End Sub
于 2012-08-01T21:04:32.827 に答える
1
Sub DeleteNumeric()

    Dim i As Long
    Dim rCell As Range
    Dim rRow As Range
    Dim rRng As Range

    'identify the range to search
    Set rRng = Sheet1.Range("A1:D5")

    'loop backwards when deleting rows
    For i = rRng.Rows.Count To 1 Step -1
        'loop through all the cells in the row
        For Each rCell In rRng.Rows(i).Cells
            If IsNumeric(rCell.Value) Then
                'delete the row and go to the next one
                rCell.EntireRow.Delete
                Exit For
            End If
        Next rCell
    Next i

End Sub
于 2012-08-01T21:21:53.190 に答える
0
Dim currentPos As Integer

currentPos = 1

Do While (currentPos < yourNumberofRow)
If (Range("A" & currentPos).Value.IsNumeric = True) Then
    Rows(currentPos & ":" & currentPos).Select
    Selection.Delete
End If

currentPos = currentPos +1
Loop

試してはいませんが、delete と IsNumeric のテストを理解するための簡単なコードです。

于 2012-08-01T21:07:19.953 に答える