8.03.2016

How to create a Excel Macro that will Find and Replace and Save a file as a CSV

How to create a Excel Macro that will Find and Replace and Save a file as a CSV


This was done in Excel 2016

Right-click on the Ribbon and add the Developer Tab. 

Go to Macro's and Create a New Macro

In the MACRO type...

The first part of the macro will automate the process of doing a find and replace and inserting "ex" for Exchange in a CSV file exported by GWAVA for the destination and UPN fields. The second part of the script automates saving the file as a CSV file with a dash at the end of the file name to quickly save the file. 

FileFormat=6 is the format for a CSV file. 

Sub FindReplace()
'
' FindReplaceMacro
'

'
    Columns("N:N").Select
    Selection.Replace What:="@domain.edu", Replacement:= _
        "@ex.domain.edu", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase _
        :=False, SearchFormat:=False, ReplaceFormat:=False
    Columns("O:O").Select
    Selection.Replace What:="@domain.edu", Replacement:= _
        "@ex.domain.edu", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase _
       :=False, SearchFormat:=False, ReplaceFormat:=False
    
    'ChDir "C:\Users\Admin\Desktop\Gwava Files\csv files up next"
    
    Dim workbook_Name As Variant
    workbook_Name = Application.GetSaveAsFilename(InitialFileName:="Merged-", FileFilter:="CSV Files, *.csv")
    
    If workbook_Name <> False Then
        ActiveWorkbook.SaveAs Filename:=workbook_Name, _
        FileFormat:=6, _
        CreateBackup:=False
        
    End If
        
End Sub

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.