简体   繁体   English

使用范围函数删除特定行

[英]Delete specific rows using range function

I want to delete all rows in excel sheet if specific column value starts with 1.如果特定列值以 1 开头,我想删除 excel 表中的所有行。

For example, if range of A1:A having values starts with 1 then I want to delete all those rows using excel vba.例如,如果A1:A的值范围以 1 开头,那么我想使用 excel vba 删除所有这些行。

How to get it?如何得到它?

  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
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    

Here's the required code with comments on how it works.这是所需的代码,并附有有关其工作原理的注释。 Feed the worksheet and column number to the sub and call it eg Delete Rows 2, Sheets("myWorksheet"):将工作表和列号提供给 sub 并调用它,例如 Delete Rows 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

You may be pushing the bounds of what is reasonable to do in Excel vba.您可能正在超越 Excel vba 中合理的操作范围。

Consider importing the Excel file into Microsoft Access.考虑将 Excel 文件导入 Microsoft Access。

Then, you can write 2 Delete Queries and they will run uber fast:然后,您可以编写 2 个删除查询,它们将运行得非常快:

DELETE FROM MyTable WHERE col1 like '2*'

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

After deleting those records, you can export the data to a new Excel file.删除这些记录后,您可以将数据导出到新的 Excel 文件。

Also, you can write an Access Macro to import the Excel File, run the Delete Queries, and Export the data back to Excel.此外,您可以编写一个 Access 宏来导入 Excel 文件、运行删除查询并将数据导出回 Excel。

And you can do all of this without writing a line of VBA Code.您无需编写一行 VBA 代码即可完成所有这些操作。

You can try:你可以试试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM