需要帮助制作一个删除超过7个字符的单元格的宏

时间:2011-08-25 14:32:04

标签: excel vba

我是Excel新手,如果一个单元格包含超过7个字符,我很难找到一个宏来删除一行。我知道我应该使用= len(a1)来获取长度,但语言对我来说真的很古老。

如果有人能让我成为宏观,我真的很感激。

3 个答案:

答案 0 :(得分:2)

Sub test2()
    Dim last As Long, i As Long
    'make sure it works with any sheet size
    last = Cells(Rows.Count, 1).End(xlUp).Row
    'loop bottom to top !
    For i = last To 1 Step -1
        If Len(Cells(i, 1)) > 7 Then
            Cells(i, 1).EntireRow.Delete
        End If
    Next i
End Sub

答案 1 :(得分:1)

尝试此操作,假设数据在A列中:

Sub DeleteRows()

    Dim r As Integer

    Let r = Range("A65536").End(xlUp).Row
    Application.ScreenUpdating = False
    Do Until r = 0
        If Len(Cells(r, 1)) > 7 Then Cells(r, 1).EntireRow.Delete
        Let r = r - 1
    Loop
    Let r = Empty
    Application.ScreenUpdating = True

End Sub    

答案 2 :(得分:0)

为了好玩,我想我会给你另一个解决方案。我尝试了它,但它确实有效。

Sub DeleteAbove7()

    Dim b1 As Boolean
    Dim i As Integer
    Dim r1 As Range
    Dim v1 As Variant

    v1 = Application.Transpose(ActiveSheet.Range("A1:A" _
         & Cells(Application.Rows.Count, 1).End(xlUp).Row).Value2)

    b1 = False
    For i = LBound(v1) To UBound(v1)
        If Len(v1(i)) > 7 Then
            If b1 Then
                Set r1 = Union(r1, Cells(i, 1))
            Else
                Set r1 = Cells(i, 1)
                b1 = True
            End If
        End If
    Next i

    r1.EntireRow.Delete

    'ActiveSheet.Range("B1:B" & UBound(v1)) = Application.Transpose(v1)

    Set r1 = Nothing
    Set v1 = Nothing

End Sub