简体   繁体   English

如果特定列中的单元格包含数组字符串,如何从Excel文件中删除行?

[英]How to delete rows from excel files if a cell in a particular column contains the string of array?

I have many excel files in many folders and I need to delete the rows from all files where in column for ex. 我在许多文件夹中都有许多Excel文件,我需要从所有文件中的ex列中删除行。 B are words from array: B是数组中的单词:

For ex. 对于前。 my bad words list: 我的坏话清单:

the sun, tree, big car, cup, .... 太阳,树木,大汽车,杯子,...

If A2 column is 'The Sun is the star at the center of the Solar System.' 如果A2列为“太阳是太阳系中心的星星”。 - this row has been deleted. -此行已被删除。

If in column is 'thesunis the..' - this row has been deleted. 如果in列中为“ thesunis the ..”-该行已被删除。 But is bad! 但是不好!

And my questions: 我的问题是:

  1. How to delete rows with exact words of array element? 如何删除具有数组元素确切词的行?
  2. How to count array elements? 如何计算数组元素?
  3. How to escape single quote in array element (example in code below) 如何在数组元素中转义单引号(下面的代码示例)
  4. How to open all files from folder "C://folder" and after run code save all? 如何打开文件夹“ C:// folder”中的所有文件,并在运行代码后保存所有文件?

Here is my code: 这是我的代码:

Sub code()
    Dim MyValue As String
    Dim a As Integer
    '------------------------------------------------------
    ArrayValueToRemove = Array("the sun", "code 'in", "another")
    Range("B:B").Select
    '------------------------------------------------------
    For Each cell In Selection
        MyValue = CStr(cell.Value)
        For a = 0 To 2
            If InStr(1, LCase(MyValue), LCase(ArrayValueToRemove(a))) > 0 Then
                cell.EntireRow.Delete
                Exit For
            End If
        Next
    Next cell
End Sub
Sub deleteBadWordRows()
    Dim currentFile, currentSheet, badWords As Variant, lastRow, i As Integer, baseDirectory As String
    '------------------------------------------------------
    baseDirectory = "c:\folder\"
    badWords = Array("the sun", "code 'in", "another")
    '------------------------------------------------------
    currentFile = Dir(baseDirectory)
    While (currentFile <> "")
        Workbooks.Open baseDirectory + currentFile
            For Each currentSheet In Workbooks(currentFile).Worksheets
                lastRow = currentSheet.Cells(currentSheet.Rows.Count, "B").End(xlUp).Row
                For j = 1 To lastRow
                    For i = 0 To UBound(badWords)
                        If InStr(1, LCase(CStr(currentSheet.Cells(j, "B").Value)), LCase(badWords(i))) > 0 Then
                            currentSheet.Rows(j).Delete
                            j = j - 1
                            lastRow = lastRow - 1
                            Exit For
                        End If
                    Next
                Next
            Next
        Workbooks(currentFile).Save
        Workbooks(currentFile).Close
        currentFile = Dir
    Wend
End Sub

Consider an SQL solution to query your string searches using the LIKE operator with wildcard, % . 考虑使用带通配符%LIKE运算符查询字符串搜索的SQL解决方案。 Excel for PC can connect to the Jet/ACE SQL Engine (Window .dll files) and query workbooks. Excel for PC可以连接到Jet / ACE SQL引擎(Window .dll文件)并查询工作簿。 Here you avoid the nested looping except for iterating through workbooks. 在这里,除了迭代工作簿之外,您避免了嵌套循环。

Below assumes all worksheets are tabular in structure with column headers all beginning at A1. 下面假设所有工作表的结构均为表格形式,列标题均始于A1。 Query results are dumped to a new worksheet where you can delete current worksheet afterwards. 查询结果将转储到新的工作表中,您可以在此之后删除当前工作表。 Be sure to replace placeholders with actual names, CurrentWorksheet , ColumnA , NewWorksheet : 确保将占位符替换为实际名称CurrentWorksheetColumnANewWorksheet

Sub DeleteSQL()
    Dim conn As Object, rst As Object
    Dim strConnection As String, strSQL As String
    Dim i As Integer
    Dim wb As Workbook

    Dim dirpath As String: dirpath = "C:\\Folder"
    Dim xlfile As Variant: xlfile = Dir(dirpath & "\*.xls*")

    Do While (xlfile <> "")            
        Set wb = Workbooks.Open(dirpath & "\" & xlfile)
        Set conn = CreateObject("ADODB.Connection")
        Set rst = CreateObject("ADODB.Recordset")

        ' WORKBOOK CONNECTION
        strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" _
                           & "Data Source='" & dirpath & "\" & xlfile & "';" _
                           & "Extended Properties=""Excel 8.0;HDR=YES;"";"            
        ' OPEN DB CONNECTION
        conn.Open strConnection

        ' OPEN RECORDSET
        strSQL = " SELECT * FROM [CurrentWorksheet$]" _
                   & " WHERE [ColumnA] LIKE ""%the sun%"" OR [ColumnA]" _
                   & " LIKE ""%code 'in%"" OR [ColumnA] LIKE ""%another%"""
        rst.Open strSQL, conn

        wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count).Name = "NewWorkSheet" 

        ' RESULTSET COLUMNS
        For i = 1 To rst.Fields.Count
            wb.Worksheets("NewWorkSheet").Cells(1, i) = rst.Fields(i - 1).Name
        Next i      

        ' RESULTSET DATA ROWS
        wb.Worksheets("NewWorkSheet").Range("A2").CopyFromRecordset rst

        wb.Close True
        rst.Close: conn.Close
        Set rst = Nothing: Set conn = Nothing

        xlfile = Dir
    Loop
End Sub

暂无
暂无

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

相关问题 Excel-如果特定列中的单元格包含在另一列中找到的值,则删除行 - Excel- Delete rows if a cell in a particular column contains values found in another column 如果单元格包含VBA / Excel中的特定值,如何从范围中删除列 - How to delete column from range if cell contains specific value in VBA/Excel 如果单元格包含数组名称,则在Excel中复制行 - Copy rows in Excel if cell contains name from an array 当单元格/列包含某些文本时,如何从Excel数据集中提取所有行 - How to extract all rows from excel data set when cell/column contains certain text 如果第一列中的单元格不是粗体,如何删除excel中的行? - How to delete rows in excel if the cell in the first column isn't bold? 如果单元格/列不包含文本,如何删除Excel中的行? - How to I delete rows in Excel if a cell/column does not contain text? VBA:Excel宏,用于删除中的文件,除非文件名中单元格内容中包含字符串 - VBA: Excel macro to delete files in a unless the filename contains a string in the cell contents Excel:如何在包含字符串的列中查找单元格并在另一列行中返回单元格的值? - Excel: How to find cell in a column that contains a string and return value of a cell in another column of row? 如何从数组中获取特定值,例如 Excel 中单元格中的数据 - How to get a particular value from an array like data in a cell in Excel Excel VBA-当单元格包含日期时删除行 - Excel VBA - Delete rows when a cell contains a date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM