Tuesday, 6 July 2021

Find and Replace values in multiple columns and multiple excel files

'Author : Suresh Meti
'-----------------------------------------------------------------------------------------------
Change="IP1"
'-----------------------------------------------------------------------------------------------

'--------------------------------------------IP1/IP2 Changes------------------------------------
'If Change = IP2 should enter IP1 value else IP2 value to identifying..

Const FromValue = "Y|Homeowners"

fromval=Split(FromValue,",")

'This is only for when do you want update with exact Value in IP2
avalue="7"

'Which value you want update in IP1/IP2
ToValue = "MobiRoot_TextObject"
'---------------------------------------------Input End------------------------------------------

dateStamp = Now()
Set WshShell = CreateObject("WScript.Shell")
sPath    = WshShell.CurrentDirectory
Set fso = CreateObject("Scripting.FileSystemObject")
Set oExcelApp = CreateObject("Excel.Application")
Set objFsoLog = CreateObject("Scripting.FileSystemObject")
sLogFileName=sPath &"\LogFile-" &DatePart("d", Now) & "-" & DatePart("m", Now) & "-" & DatePart("yyyy", Now) &"-" & DatePart("n", Now) &".txt"
Set logOutput = objFsoLog.CreateTextFile(sLogFileName)

count=0
icount=0
Dim WshShell, strCurDir
    Set ObjFolder = fso.GetFolder(sPath)
    Set ObjFiles = ObjFolder.Files
        For Each ObjFile In ObjFiles
            If LCase(Right(ObjFile.Name, 5)) = ".xlsx" Or LCase(Right(ObjFile.Name, 4)) = ".xls" Then
Count=Count+1

With oExcelApp
   .Visible = False
   .DisplayAlerts = False
   Set oWB = .Workbooks.Open(ObjFile)
       End With
Set oWS = oWB.Sheets(1)
logOutput.WriteLine(Replace(Replace(ObjFile,sPath,""),"\","-----"))+" -------- Values are Updated in "+Change +" Column"
For j=0 to ubound(fromval)

 For i=1 to 200

  if Change="IP2" Then
Row="F"&i
if oWS.Range(Row).Value = fromval(j) then
Column="G"&i
Preval=oWS.Range(Column).Value
if avalue=Preval then
 oWS.Range(Column).Value=ToValue
 logOutput.WriteLine(cstr(dateStamp) + " -" + vbTab + "Found "+Fromval(j) +" at " +Row +" The Value at "+Column +" is:"+Preval +" Replaced with "+ToValue)
                          icount=icount+1
                         End if
                End If
Else
Row="G"&i
if oWS.Range(Row).Value = fromval(j) then
Column="F"&i
Preval=oWS.Range(Column).Value
 oWS.Range(Column).Value=ToValue
 logOutput.WriteLine(cstr(dateStamp) + " -" + vbTab + "Found "+Fromval(j) +" at " +Row +" The Value at "+Column +" is:"+Preval +" Replaced with "+ToValue)
 icount=icount+1
Else
   'MsgBox (WHAT_TO_FIND & " not found")
End If
       End If
Next
Next
' oWS.Cells.Replace FromValue, ToValue
oWB.Save
oWB.Close
oExcelApp.Quit

            End If
        Next
logOutput.Close
Set logOutput = Nothing
Set objFsoLog = Nothing
Set WshShell = Nothing
oExcelApp.Quit
msgbox "Updated "&Count& " Files and replaced in "&icount&" places,"& vbNewLine &"Please see log file for more details. ",vbOkCancel,"Excel Mass Update "

No comments:

Post a Comment