1

实际上我必须找到一个名为“帐户”的列,并且我必须删除在该列中输入的数据。假设列名“帐户”在单元格“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
4

1 回答 1

2

您可以使用FindNext 方法执行此操作

此方法“继续使用 Find 方法开始的搜索

另外,尽量不要使用Selectand Activate。有很多数据,这对性能非常不利。

最后,您可以检查rng1您之后的内容Find以避免使用此行出现 Range Error :

If Not rng1 Is Nothing Then
于 2013-05-18T13:58:54.493 回答