[英]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: 我的问题是:
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 :
确保将占位符替换为实际名称CurrentWorksheet , ColumnA , NewWorksheet :
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.