Help edit error when run result wrong!!!

Nguyen Anh Dung

Board Regular
Joined
Feb 28, 2020
Messages
180
Office Version
  1. 2016
Platform
  1. Windows
i have code as below use repeleace all file in subfolder.
when repleace column V : \\192.168.5.18 to \\192.168.5.19 is result \192.168.5.18
when repleace \192.168.5.18 to \\192.168.5.18 s result 192.168.5.18
But repleace 192.168.5.18 to \\192.168.5.19 is correct.

Code:
Sub Change_directory_select()
    Application.EnableEvents = False
    Application.AskToUpdateLinks = False
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Dim fso As Object, fold As Object, fFile As Object
    Dim fPath As String, fName As String, char_old As String, char_new As String
    Dim wb As Workbook, ws As Worksheet
    Dim lr As Long
    fPath = InputBox("Enter path folder:")
    
Set fso = CreateObject("Scripting.FileSystemObject")
Set fold = fso.GetFolder(fPath)
char_old = Application.InputBox("Enter char finding:")
char_new = Application.InputBox("Enter replace char:")
For Each fFile In fold.SubFolders

fName = Dir(fFile.Path & "\*.xls", vbNormal)
   
    i = 1
Do While fName <> ""
        FileName = fso.GetBaseName(fFile.Path)
        Set wb = Workbooks.Open(fFile.Path & "\" & fName)
        Set ws = wb.Worksheets(1)
        j = 2
        While ws.Columns("V").Rows(j) <> ""
          
        lr = wb.Sheets(1).Range("V" & Rows.Count).End(xlUp).Row
                For j = 2 To lr
                    Range("V" & j) = Replace(Range("V" & j), char_old, char_new)
                Next j
                   
            j = j + 1
        Wend
nextcode:
        wb.Close savechanges:=True
        i = i + 1
       fName = Dir
Loop
Next
    MsgBox "Ho" & ChrW(224) & "n Th" & ChrW(224) & "nh !!!"
    Application.EnableEvents = True
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    Application.AskToUpdateLinks = True
End Sub
Best regards!!!
Nguyen Anh Dung
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I cannot reproduce your error. Instead of looping though all the rows of Col V, why not use the range replace method to change them all at once?

Replace this
VBA Code:
        While ws.Columns("V").Rows(j) <> ""
          
        lr = wb.Sheets(1).Range("V" & Rows.Count).End(xlUp).Row
                For j = 2 To lr
                    Range("V" & j) = Replace(Range("V" & j), char_old, char_new)
                Next j
                   
            j = j + 1
        Wend

With this
VBA Code:
    With ws
        With .Range("V2:V" & .Range("V" & .Rows.Count).End(xlUp).Row)
            .Replace What:=char_old, Replacement:=char_new, LookAt:=xlPart, MatchCase:=False
        End With
    End With
 
Upvote 0
I cannot reproduce your error. Instead of looping though all the rows of Col V, why not use the range replace method to change them all at once?

Replace this
VBA Code:
        While ws.Columns("V").Rows(j) <> ""
         
        lr = wb.Sheets(1).Range("V" & Rows.Count).End(xlUp).Row
                For j = 2 To lr
                    Range("V" & j) = Replace(Range("V" & j), char_old, char_new)
                Next j
                  
            j = j + 1
        Wend

With this
VBA Code:
    With ws
        With .Range("V2:V" & .Range("V" & .Rows.Count).End(xlUp).Row)
            .Replace What:=char_old, Replacement:=char_new, LookAt:=xlPart, MatchCase:=False
        End With
    End With
Thanks you so much!!!
I have check office 2016 is correct. i thinks install office 2019 is error !!!

Best regards,
Nguyen Anh Dung
 
Upvote 0

Forum statistics

Threads
1,214,806
Messages
6,121,672
Members
449,045
Latest member
Marcus05

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