How do I find and Replace in anouther excel document
I am tring to create a macro (in Excel) to open another excel worksheet, do a find-replace, then save it.
I have already achieved this with a word document like this:
...
Dim wrdDoc As Word.Document
If wrdApp Is Nothing Then Set wrdApp = CreateObject("Word.Application")
Set wrdDoc = wrdApp.Documents.Open(TemplateFilePath)
...
Call WordReplace(wrdDoc,"a","b")
....
Private Sub WordReplace(wrdDoc As Word.Document, sFind As String, sReplace As String)
With wrdDoc.Content.Find
.Text = sFind
.Replacement.Text = sReplace
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub
I have crudely tried to replace the word "Word" with "Excel" in the above code to no avail.
Can somone point me in the 开发者_Go百科right direction please?
You could do:
Sub a()
Dim excDoc As Workbook
....
Set excDoc = Workbooks.Open("c:\mata.xls")
Call WordReplace(excDoc, "a", "b")
....
End Sub
Private Sub WordReplace(excDoc As Variant, sFind As String, sReplace As String)
Dim sht As Worksheet
For Each sht In excDoc.Worksheets
With sht
.Cells.Replace What:=sFind, Replacement:=sReplace, LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
End With
Next
End Sub
精彩评论