2012-08-01 256 views
3

我一直在嘗試使用一些片段來說明如何刪除Excel VBA上的整個行,但我無法修改它們以包含「IsNumber」驗證。Excel VBA - 如果單元格是一個整數,刪除整個行

我需要能夠選擇一個活動區域,如:

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 

然後宏將刪除所有的行,除了第四其一是

NA NA NA NA 
+0

範圍。( 「B1:C5」)SpecialCells(xlCellTypeConstants,xlNumbers).EntireRow.Delete – tracer 2012-08-01 21:25:51

+0

感謝您的建議和幫助下,每個人。這個評論正好在我想要的,我希望它可以幫助某人。 – tracer 2012-08-01 21:36:47

+1

這是使用它的不正確方法,它可能會在任何時間點拋出錯誤。我給了你兩種久經考驗的方法,你的意思是兩者都沒有幫助? – 2012-08-01 21:45:37

回答

5

隨你挑:)

WAY 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 

WAY 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 
+0

謝謝!它確實刪除了正確單元格的內容,但是,如果單元格是數字,我想要刪除整行。我嘗試了「rng.Delete」,但它似乎並不是那樣的。 – tracer 2012-08-01 21:09:08

+0

它刪除行的內容,然後將底部的空白行按下。如果行具有任何格式,您也可以使用'rng.Clear'而不是'rng.ClearContents'。 – 2012-08-01 21:10:42

+0

感謝您的幫助!我用這個specialCells部分製作了一個非常小的版本。我在原始文章中添加了評論 – tracer 2012-08-01 21:23:57

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 

不嘗試,但容易代碼來了解刪除和則IsNumeric測試。

+0

Range(「A」&currentPos).Value.IsNumeric'? – 2012-08-01 21:09:33

+0

出錯。 IsNumeric是這樣使用的:'If(IsNumeric(Range(「A3」)))Then MsgBox(「是一個數字值」) End If' – Dekx 2012-08-01 21:52:05

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 
相關問題