vba - 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 , need delete rows files in column ex. b words array:
for ex. bad words list:
the sun, tree, big car, cup, ....
if a2 column 'the sun star @ center of solar system.' - row has been deleted.
if in column 'thesunis the..' - row has been deleted. bad!
and questions:
- how delete rows exact words of array element?
- how count array elements?
- how escape single quote in array element (example in code below)
- how open files folder "c://folder" , after run code save all?
here code:
sub code() dim myvalue string dim integer '------------------------------------------------------ arrayvaluetoremove = array("the sun", "code 'in", "another") range("b:b").select '------------------------------------------------------ each cell in selection myvalue = cstr(cell.value) = 0 2 if instr(1, lcase(myvalue), lcase(arrayvaluetoremove(a))) > 0 cell.entirerow.delete exit end if next next cell end sub
sub deletebadwordrows() dim currentfile, currentsheet, badwords variant, lastrow, integer, basedirectory string '------------------------------------------------------ basedirectory = "c:\folder\" badwords = array("the sun", "code 'in", "another") '------------------------------------------------------ currentfile = dir(basedirectory) while (currentfile <> "") workbooks.open basedirectory + currentfile each currentsheet in workbooks(currentfile).worksheets lastrow = currentsheet.cells(currentsheet.rows.count, "b").end(xlup).row j = 1 lastrow = 0 ubound(badwords) if instr(1, lcase(cstr(currentsheet.cells(j, "b").value)), lcase(badwords(i))) > 0 currentsheet.rows(j).delete j = j - 1 lastrow = lastrow - 1 exit end if next next next workbooks(currentfile).save workbooks(currentfile).close currentfile = dir wend end sub
Comments
Post a Comment