繁体   English   中英

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

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

我在许多文件夹中都有许多Excel文件,我需要从所有文件中的ex列中删除行。 B是数组中的单词:

对于前。 我的坏话清单:

太阳,树木,大汽车,杯子,...

如果A2列为“太阳是太阳系中心的星星”。 -此行已被删除。

如果in列中为“ thesunis the ..”-该行已被删除。 但是不好!

我的问题是:

  1. 如何删除具有数组元素确切词的行?
  2. 如何计算数组元素?
  3. 如何在数组元素中转义单引号(下面的代码示例)
  4. 如何打开文件夹“ C:// folder”中的所有文件,并在运行代码后保存所有文件?

这是我的代码:

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

考虑使用带通配符%LIKE运算符查询字符串搜索的SQL解决方案。 Excel for PC可以连接到Jet / ACE SQL引擎(Window .dll文件)并查询工作簿。 在这里,除了迭代工作簿之外,您避免了嵌套循环。

下面假设所有工作表的结构均为表格形式,列标题均始于A1。 查询结果将转储到新的工作表中,您可以在此之后删除当前工作表。 确保将占位符替换为实际名称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.

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