Chase_Luke
New Member
- Joined
- Mar 30, 2022
- Messages
- 3
- Office Version
- 365
- 2016
My goal is to delete rows from multiple different workbooks by using a specific set of data as the criteria.
My data set for the deletion criteria is just over 18K rows (A2:A18052), containing both letters and numbers. Something like this:
So I want any rows with these contract numbers deleted from all workbooks in my file path. Contract Number in external workbooks is in column "C".
Here is what I have so far:
I have an issue with the InStr function line -
My data set for the deletion criteria is just over 18K rows (A2:A18052), containing both letters and numbers. Something like this:
ContractNumber |
123123123 |
123ABC123ABC |
345345345 |
So I want any rows with these contract numbers deleted from all workbooks in my file path. Contract Number in external workbooks is in column "C".
Here is what I have so far:
VBA Code:
Sub DeleteArray()
Dim wb As Workbook
Dim ws As Worksheet
Dim MyArray As Variant
Dim currentFile, currentSheet, lastRow, baseDirectory As String
With ActiveSheet
MyArray = Range("A2:A18052").Value
End With
baseDirectory = "[folder path for external workbooks]"
currentFile = Dir(baseDirectory)
While (currentFile <> "")
Workbooks.Open baseDirectory + currentFile
For Each currentSheet In Workbooks(currentFile).Worksheets
lastRow = currentSheet.Cells(currentSheet.Rows.Count, "C").End(xlUp).Row
For j = 1 To lastRow
If InStr(1, LCase(CStr(currentSheet.Cells(j, "C").Value)), MyArray) > 0 Then
currentSheet.Cells(j, 1).EntireRow.Delete
j = j - 1
lastRow = lastRow - 1
Exit For
End If
Next
Next
Workbooks(currentFile).Save
Workbooks(currentFile).Close
currentFile = Dir
Wend
End Sub
I have an issue with the InStr function line -
VBA Code:
If InStr(1, LCase(CStr(currentSheet.Cells(j, "C").Value)), MyArray) > 0 Then
I basically used this to look up the deletion criteria, but is there a different function I can use? Or can I set it up differently to get it to recognize my array?