如何清除重复命名的列?
其实我必须find一个名为“帐户”的列,我必须删除在该列中input的数据。 假设列名“账号”在单元格“B9”中,并且值已经input到“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
您可以使用FindNext方法执行此操作
这种方法“ 继续search以Find方法开始 ”
另外,尽量不要使用Select
和Activate
。 有了大量的数据,这对性能来说是非常糟糕的。
最后,您可以在使用下面这行Find
以避免范围错误之后检查rng1
内容:
If Not rng1 Is Nothing Then