Hi All,
I am not too familiar with VBA and have compiled code from various sources to try and find a specified string (in my case "Author", "Owner" and "Page") and delete those rows, for multiple workbooks. My code is able to open all the files however it is not performing the delete function. Any help is much appreciated.
Thank you,
Jay
Sub Test1()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim rngFound As Range, rngToDelete As Range
Dim strFirstAddress As String
Dim varList As Variant
Dim lngCounter As Long
Application.ScreenUpdating = False
MyPath = "C:\Users\Desktop\Excel"
FNum = 0
Do While FilesInPath <> ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop
If FNum > 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
On Error GoTo 0
If Not mybook Is Nothing Then
On Error Resume Next
varList = VBA.Array("Owner:", "Author:", "Page")
For lngCounter = LBound(varList) To UBound(varList)
With Sheet1.Range("A:A")
Set rngFound = .Find( _
What:=varList(lngCounter), _
Lookat:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False _
)
If Not rngFound Is Nothing Then
If rngToDelete Is Nothing Then
Set rngToDelete = rngFound
Else
Set rngToDelete = Application.Union(rngToDelete, rngFound)
End If
strFirstAddress = rngFound.Address
Set rngFound = .FindNext(After:=rngFound)
Do Until rngFound.Address = strFirstAddress
Set rngToDelete = Application.Union(rngToDelete, rngFound)
Set rngFound = .FindNext(After:=rngFound)
Loop
End If
End With
Next lngCounter
If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete
End If
mybook.Close savechanges:=True
Next FNum
End If
End Sub
I am not too familiar with VBA and have compiled code from various sources to try and find a specified string (in my case "Author", "Owner" and "Page") and delete those rows, for multiple workbooks. My code is able to open all the files however it is not performing the delete function. Any help is much appreciated.
Thank you,
Jay
Sub Test1()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim rngFound As Range, rngToDelete As Range
Dim strFirstAddress As String
Dim varList As Variant
Dim lngCounter As Long
Application.ScreenUpdating = False
MyPath = "C:\Users\Desktop\Excel"
FNum = 0
Do While FilesInPath <> ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop
If FNum > 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
On Error GoTo 0
If Not mybook Is Nothing Then
On Error Resume Next
varList = VBA.Array("Owner:", "Author:", "Page")
For lngCounter = LBound(varList) To UBound(varList)
With Sheet1.Range("A:A")
Set rngFound = .Find( _
What:=varList(lngCounter), _
Lookat:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False _
)
If Not rngFound Is Nothing Then
If rngToDelete Is Nothing Then
Set rngToDelete = rngFound
Else
Set rngToDelete = Application.Union(rngToDelete, rngFound)
End If
strFirstAddress = rngFound.Address
Set rngFound = .FindNext(After:=rngFound)
Do Until rngFound.Address = strFirstAddress
Set rngToDelete = Application.Union(rngToDelete, rngFound)
Set rngFound = .FindNext(After:=rngFound)
Loop
End If
End With
Next lngCounter
If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete
End If
mybook.Close savechanges:=True
Next FNum
End If
End Sub