Sub ReplaceInFolder()
Dim strPath As String
Dim strFile As String
Dim wbk As Workbook
Dim wsh As Worksheet
Dim strFind As String
Dim strReplace As String
strFind = InputBox("Enter text to find")
If strFind = "" Then
MsgBox "No find text specified!", vbExclamation
Exit Sub
End If
strReplace = InputBox("Enter replacement text")
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show Then
strPath = .SelectedItems(1)
Else
MsgBox "No folder selected!", vbExclamation
Exit Sub
End If
End With
If Right(strPath, 1) <> "\" Then
strPath = strPath & "\"
End If
Application.ScreenUpdating = False
strFile = Dir(strPath & "*.xls*")
Do While strFile <> ""
Set wbk = Workbooks.Open(Filename:=strPath & strFile, AddToMRU:=False)
For Each wsh In wbk.Worksheets
wsh.Cells.Replace What:=strFind, Replacement:=strReplace, _
LookAt:=xlWhole, MatchCase:=False
Next wsh
wbk.Close SaveChanges:=True
strFile = Dir
Loop
Application.ScreenUpdating = True
End Sub
-----------
ví dụ: thay giám đốc thành tổng giám đốc:
"Gi" & ChrW(225) & "m " & ChrW(272) & ChrW(7889) & "c"
"T" & ChrW(7893) & "ng gi" & ChrW(225) & "m " & ChrW(273) & ChrW(7889) & "c"
---
Sub Macro2()
'
' Macro2 Macro
'
'
Cells.Replace What:="Gi" & ChrW(225) & "m " & ChrW(272) & ChrW(7889) & "c", Replacement:="T" & ChrW(7893) & "ng gi" & ChrW(225) & "m " & ChrW(273) & ChrW(7889) & "c", LookAt:=xlPart _
, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
'---
Sub TextBoxReplace()
Dim shp As Shape
Dim sOld As String
Dim sNew As String
'Change as desired
sOld = "g"
sNew = "g1"
On Error Resume Next
For Each shp In ActiveSheet.Shapes
With shp.TextFrame.Characters
.Text = Application.WorksheetFunction.Substitute( _
.Text, sOld, sNew)
End With
Next
End Sub