How do I delete a list of keywords from multiple files?

formula1234

New Member
Joined
Jul 28, 2018
Messages
6
HI. I have a list of keywords in 1 excel file. I need to somehow search and delete it from multiple excel files. What is the best way to do this? I really need to do this for my job. Thank you all for your help
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Paste code into a module in the 'app' workbook.
col. A has the list of all keywords
cell C1 has the folder name to search and replace.

add a button on the main sheet to run macro FIXALLFILES.
it reads the foldername in C1 and cycles thru each xl file in the folder removing the keywords.

Code:
Public gcolWords As Collection
Public Const kDirCELL = "C1"
Public Sub FixAllFiles()
Dim vDir
vDir = Range(kDirCELL).Value
If vDir = "" Then
   MsgBox "No folder given in " & kDirCELL
Else
    RemoveKeywordsFromAllFiles vDir
End If
End Sub

'del all keywords in list in all worksheets in all files
Private Sub RemoveKeywordsFromAllFiles(ByVal pvDir)
Dim ws As Worksheet
Dim vFil, vTargT
Dim i As Integer
Dim fso
Dim oFolder, oFile
  'get all keywords
Set gcolWords = New Collection
Range("A2").Select
While ActiveCell.Value <> ""
    gcolWords.Add ActiveCell.Value
 
   ActiveCell.Offset(1, 0).Select 'next row
Wend

If gcolWords.Count = 0 Then
   MsgBox "No keywords assigned"
   Exit Sub
End If
'cycle thru all files
On Error GoTo errRun
If Right(pvDir, 1) <> "\" Then pvDir = pvDir & "\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(pvDir)
For Each oFile In oFolder.Files
     If InStr(oFile.Name, ".xls") > 0 Then      'ONLY DO EXCEL FILES
          Workbooks.Open oFile
            For Each ws In Sheets
                ws.Activate
                ReplaceWords
            Next
         
            ActiveWorkbook.Close True
    End If
Next
Set ws = Nothing
Set gcolWords = Nothing
MsgBox "done"
Exit Sub
errRun:
MsgBox Err.Description, , Err
End Sub

Private Sub ReplaceWords()
Dim i As Integer
Dim wrd
For i = 1 To gcolWords.Count
   wrd = gcolWords(i)
 
    Range("A1").Select
    Cells.Replace What:=wrd, Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,325
Messages
6,124,252
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top