I have two sheets.
Sheet1 is an imported XML table so it looks a little awkward, like a stairstep of tables with several thousand rows.
Sheet2 is a single column that is a list of strings with a few hundred entries.
I want to find every instance of each exact string in Sheet2 within Sheet1, then delete the whole row in Sheet1.
Both Sheet1 and Sheet2 data sources will grow and change over time, so I want to make a general solution.
I have a Python script that is supposed to trim out the XML source using what would be the contents of Sheet2, but the resulting XML is broken and unacceptable as it ends up missing data types.
I don't know if doing this via Excel will preserve the data types.
Assuming sheets are like arrays indexed by [rows,cols]. This is what I'm trying to do, with the functions standing in for whatever needs to be in VBA to achieve the same goal.
String str;
Int r, foundStrRow;
*/rowFind(sheet, string) being a function that finds arg string within sheet and returns the row index
Delete(sheet, row) being a function that deletes the index row from sheet*/
While(Sheet2[r,1]) {
Str = Sheet2[r, 1];
foundStrRow = rowFind(Sheet1,Str);
Delete(Sheet1, foundStrRow);
r++
}
The data cannot be shared for security reasons but I can provide a faux set of the data here.
https://i.stack.imgur.com/kQVGB.jpg
Imagine that Sheet1 has that stairstep that is about 20000 rows and 200 columns. Sheet2 is 400 rows and one column.
This is what I have come up with. It is able to find and delete a whole row if the string from Sheet2 exists in Sheet1.
I have to figure out how to make it conditional on the string existing. If the string from Sheet2 isn't in Sheet1 then I need to skip to the next entry.
Sub Trimmer()
Dim Rows2 as Integer
Dim wordToSearch as String
'Sets Rows2 to be limit of the loop
With Sheet2
With .Cells.SpecialCells(xlCellTypeLastCell)
Rows2 = .Row
End With
End With
For r2=1 to Rows2
Sheets("Sheet2").Select
wordToSearch = Cells(r2,1).Value
Sheets("Sheet1").Select
Cells.Find(What := wordToSearch, After := ActiveCell, LookIn := xlFormulas2, _
LookAt :=xlWhole, SearchOrder := xlByRows, SearchDirection:= xlNext, _
MatchCase := False, SearchFormat := False).Activate
Rows(ActiveCell.Row).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
This will iterate and take out the whole rows where the match to the list in Sheet2 exists.
I need to have it skip when something in the Sheet2 list isn't found in Sheet1.