Giúp repleace wildcards!!!

Liên hệ QC

nguyenanhdung8111982

Thành viên hoạt động
Tham gia
1/11/19
Bài viết
120
Được thích
33
Giới tính
Nam
có cách nào repleace trong vba:
ví dụ nhập: không tên * khi đó đổi Không tên 01, Không tên 02 bằng rỗng.
hoặc nhập Nguyễn Văn * thay cho Nguyễn Văn A, Nguyễn Văn B, Nguyễn Vẵn C

Mã:
Option Explicit

Sub change_character()
    Dim wb_new As Workbook
    Dim lr As Long
    Dim i As Long, j As Long
    Dim path As String, fpath As String
    Dim arr As Variant
    Dim char_old As String, char_new As String
    Application.DisplayAlerts = False
    Application.AskToUpdateLinks = False
    path = ThisWorkbook.path
    arr = GetFileNames(path)
    char_old = Application.InputBox("Nhap tu can thay the:")
    char_new = Application.InputBox("Nhap tu muon thay the:")
    For i = 1 To UBound(arr) - 1
        For Each wb_new In Workbooks
            If wb_new.Name = arr(i) Then
                lr = Workbooks(wb_new.Name).Sheets(1).Range("N" & Rows.Count).End(xlUp).Row
                For j = 2 To lr
                    Range("N" & j) = Replace(Range("N" & j), char_old, char_new)
                Next j
            Else
                fpath = path & "\" & arr(i)
                Set wb_new = Workbooks.Open(fpath)
                lr = Workbooks(wb_new.Name).Sheets(1).Range("N" & Rows.Count).End(xlUp).Row
                For j = 2 To lr
                    Range("N" & j) = Replace(Range("N" & j), char_old, char_new)
                Next j
                wb_new.Close True
            End If
        Next wb_new
    Next i
    MsgBox "Da Xong!!!"
    Application.AskToUpdateLinks = True
    Application.DisplayAlerts = True
End Sub

Function GetFileNames(ByVal FolderPath As String) As Variant
Dim Result As Variant
Dim i As Integer
Dim MyFile As Object
Dim MyFSO As Object
Dim MyFolder As Object
Dim MyFiles As Object
Set MyFSO = CreateObject("Scripting.FileSystemObject")
Set MyFolder = MyFSO.GetFolder(FolderPath)
Set MyFiles = MyFolder.Files
ReDim Result(1 To MyFiles.Count)
i = 1
For Each MyFile In MyFiles
Result(i) = MyFile.Name
i = i + 1
Next MyFile
GetFileNames = Result
End Function
 
Lần chỉnh sửa cuối:
Nên dùng thuộc tính
Range.Replace(....,...,xlPart)
thì có thể dùng wildcard
 
Upvote 0
Web KT
Back
Top Bottom