【问题标题】:Delete specific rows using range function使用范围函数删除特定行
【发布时间】:2013-12-28 07:23:12
【问题描述】:

如果特定列的值以 1 开头,我想删除 Excel 工作表中的所有行。

例如,如果A1:A 的值范围以 1 开头,那么我想使用 excel vba 删除所有这些行。

如何获得?

  Dim c As Range
Dim SrchRng

 Set SrchRng = Sheets("Output").UsedRange
Do
   Set c = SrchRng.Find("For Men", LookIn:=xlValues)
  If Not c Is Nothing Then c.EntireRow.Delete
Loop While Not c Is Nothing

【问题讨论】:

    标签: excel vba


    【解决方案1】:
    Dim Value As String
    Dim CellName As String
    Dim RowNumber As Long
    Do While Value <> ""
        CellName = "A" + RowNumber
        Value = ActiveSheet.Cells(GetRowNumber(CellName), GetColumnNumber(CellName)).Value
        If Mid(Value, 1, 1) = "2" Then
           ActiveSheet.Range("A" & RowNumber).EntireRow.Delete
        End If
        RowNumber = RowNumber + 1
    Loop
    
    Private Function GetColumnNumber(ByVal CellName As String) As Long
        For L = 1 To 26
            If Left(CellName, 1) = Chr(L + 64) Then
                GetColumnNumber = L
                Exit For
            End If
        Next
    End Function
    Private Function GetRowNumber(ByVal CellName As String) As Long
        GetRowNumber = CLng(Mid(CellName, 2))
    End Function    
    

    【讨论】:

      【解决方案2】:

      以下是有关其工作原理的 cmets 所需代码。将工作表和列号提供给 sub 并调用它,例如删除第 2 行,Sheets("myWorksheet"):

         Sub DeleteRows(columnNumber as Integer, ws as WorkSheet)
      
          Dim x as long, lastRow as Long
      
          ' get the last used row
          lastRow = ws.cells(1000000, columnNumber).end(xlUp).Row
      
          'loop backwards from the last row and delete applicable rows
          For x = lastRow to 1 Step -1
      
             ' if the cell starts with a number...
             If IsNumeric(Left(ws.Cells(x, columnNumber), 1) Then
                'Delete it the row if it's equaal to 1
                If Left(ws.Cells(x, columnNumber), 1) = 1 Then ws.Rows(x &":"& x).Delete
             End If
          Next x
      
          End Sub
      

      【讨论】:

      • 请注意,lastRow = ws.cells(1000000, columnNumber).end(xlUp).Row 部分仅选择前一百万中的最后一个。你应该看看没有这个问题的suggested edit
      • 通常的方法是使用ws.rows.count而不是硬编码的数字从最后一行开始。
      【解决方案3】:

      您可能会在 Excel vba 中突破合理范围。

      考虑将 Excel 文件导入 Microsoft Access。

      然后,您可以编写 2 个删除查询,它们将运行得非常快:

      DELETE FROM MyTable WHERE col1 like '2*'
      
      DELETE FROM MyTable WHERE col2 LIKE '*for men*' OR col3 LIKE '*for men*'
      

      删除这些记录后,您可以将数据导出到新的 Excel 文件。

      另外,您可以编写一个 Access 宏来导入 Excel 文件、运行删除查询并将数据导出回 Excel。

      您无需编写一行 VBA 代码即可完成所有这些操作。

      【讨论】:

        【解决方案4】:

        你可以试试:

        Sub delete()
            tamano = Range("J2") ' Value into J2
            ifrom = 7 ' where you want to delete
            'Borramos las celdas
            'Delete column A , B and C
            For i = ifrom To tamano
            Range("A" & i).Value = ""
            Range("B" & i).Value = ""
            Range("C" & i).Value = ""
            Next i
        End Sub
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-10-14
          • 2021-08-02
          • 2016-11-25
          相关资源
          最近更新 更多