如何清除重复命名的列?

时间:2013-05-18 13:30:25

标签: excel vba excel-vba

实际上我必须找到一个名为“account”的列,我必须删除在该列中输入的数据。 让我们说列名“帐户”在单元格“B9”中,并且输入的值直到“B30”(它是可变的),然后我必须将数据从“B10”删除到“B30”。而且,如果我在“帐户”名称中还有一列,那么我也必须为该列做同样的事情。

我编写了一列。我想把它写成多列。

这是我的编码,

Private Sub CommandButton1_Click()
    Dim xlapp As Excel.Application
    Dim wb As Workbook
    Dim FindRow As Range
    Dim ad As String
    Dim AcCell As String

    Dim de As String
    Dim lad As String

    Dim col As Integer
    Dim rw As Integer
    Dim r As Integer
    Dim rw2 As Integer
    Dim myrange As Range

    On Error GoTo ErrHandler:

    MsgBox "Please browse for the document"
    Set xlapp = CreateObject("Excel.Application")

    filestr1 = Application.GetOpenFilename()
    Workbooks.Open Filename:=filestr1  , Notify:=False

    With xlapp    
        Set rng1 = ActiveSheet.UsedRange.Find("Account", , xlValues, xlWhole)
        col = rng1.Column
        'MsgBox col'
        rw = rng1.Row
        'MsgBox rw'
        r = rw + 1
        'MsgBox r'
        ad = rng1.Address
        'MsgBox ad'
        ActiveSheet.Range(ad).Activate
        ActiveCell.Offset(1, 0).Activate
        rw2 = ActiveCell.Row

        de = ActiveCell.Address
        'MsgBox de'

        ActiveSheet.Cells(Rows.Count, col).End(xlUp).Activate

        lad = ActiveCell.Address
        'MsgBox lad'

        Set myrange = ActiveSheet.Range(de & ":" & lad)
        myrange.Select
        Selection.ClearContents

        On Error GoTo ErrHandler:

        filestr4 = Application.GetSaveAsFilename("RemovedAccNo")
        ActiveWorkbook.SaveAs (filestr4)
        On Error GoTo ErrHandler:    
    End With
    Exit Sub

    ErrHandler:
    MsgBox ("User Cancelled.")

End Sub

1 个答案:

答案 0 :(得分:2)

您可以使用FindNext Method

执行此操作

此方法“继续使用”查找方法

开始搜索

另外,请尝试不要使用SelectActivate。对于大量的数据,这对性能来说非常糟糕。

最后,您可以在rng1之后检查Find内容以避免此行的范围错误:

If Not rng1 Is Nothing Then