script to convert all characters in excel sheet to A.

Hi Experts,
Looking for a script that will perform the following
open a given spreadsheet, loop thru all columns/rows and replace all data as follows. (except for first row)
for every character found, replace it with letter A
for every digit found, replace it with 1
should leave any special character like slashes or space intact.
Thanks
LVL 6
bfuchsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ardhendu SarangiSr. Project ManagerCommented:
Here is a simple script that would work.. Give it a spin and let me know.

Sub ReplaceAll()
      
      Dim LastRow As Long
      With ActiveSheet
            LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
      End With

      Dim LastCol As Integer
      With ActiveSheet
            LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
      End With
      
      For j = 1 To LastCol
            For i = 2 To LastRow
                  replStr = Cells(i, j)
                  
                  For n = 1 To Len(replStr)
                        replLtr = Mid(replStr, n, 1)
                        If Asc(replLtr) >= 65 And Asc(replLtr) <= 90 Then
                              Mid(replStr, n, 1) = "A"
                        End If
                        If Asc(replLtr) >= 97 And Asc(replLtr) <= 122 Then
                              Mid(replStr, n, 1) = "A"
                        End If
                        If Asc(replLtr) >= 48 And Asc(replLtr) <= 57 Then
                              Mid(replStr, n, 1) = "1"
                        End If
                  Next
                  Cells(i, j) = replStr
            Next
      Next
End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sam JacobsDirector of Technology Development, IPMCommented:
You may wish to try this:

Option Explicit

Function GetFile(startPath As String, fileTypes As String)
 
With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = False
    'Filter to selected file types - sample format:
    ' "Excel Files", "*.xslx; *.xlsm; *.xls" | "All Files", "*.*"
        .Filters.Clear
        If InStr(fileTypes, "|") < 1 Then
        Dim aFilter() As String
        aFilter = Split(fileTypes, ",")
        .Filters.Add Trim(aFilter(0)), Trim(aFilter(1))
    Else
        Dim aExtensions() As String
        aExtensions = Split(fileTypes, "|")
        Dim idx As Integer
        For idx = 0 To UBound(aExtensions)
            aFilter = Split(Trim(aExtensions(idx)), ",")
            .Filters.Add Trim(aFilter(0)), Trim(aFilter(1))
        Next
    End If
    'Show the dialog box
    If .Show = -1 Then
        GetFile = .SelectedItems.Item(1)
    Else
        GetFile = ""
    End If
End With
 
End Function

Function ReplaceCell(cellValue As String)

    Dim RE As Object
    Set RE = CreateObject("VBScript.RegExp")

    RE.ignoreCase = True
    RE.Global = True

    ' replace characters
    RE.Pattern = "[A-Za-z]"
    cellValue = RE.Replace(cellValue, "A")
    ' replace numbers
    RE.Pattern = "[0-9]"
    cellValue = RE.Replace(cellValue, "1")
    
    ReplaceCell = cellValue
End Function

Sub Main()

Dim cell As Range

Dim strFile As String
    strFile = GetFile(Application.ActiveWorkbook.Path, "Excel Files, *.xslx; *.xlsm; *.xls")
    If strFile = "" Then End
    Workbooks.Open strFile
    Application.ScreenUpdating = False
    For Each cell In ActiveSheet.UsedRange
        If cell.Row > 1 Then
            cell.Value = ReplaceCell(cell.Value)
        End If
    Next
    Application.ScreenUpdating = True
End Sub

Open in new window

bfuchsAuthor Commented:
Both seem to work.
Thank you!
bfuchsAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.