Macro to replace digits with 9, letters (upper or lower case) with L, other with _

ellison

Active Member
Joined
Aug 1, 2012
Messages
356
Office Version
  1. 365
Platform
  1. Windows
Hi, we are trying to find a macro which would replace the alphanumeric contents of each cell with:
-letters with "L"
-digits with "9"
-any other remaining characters with "_"

Example:
[TABLE="width: 197"]
<tbody>[TR]
[TD="class: xl75, width: 197, align: left"]XC/73:54-15PC 44C
becomes
LL_99_99_99LL_99L[/TD]
[/TR]
</tbody>[/TABLE]

Best
NEil
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
I suspect someone may be able to come up with something more efficient, but here is a User Defined Function (UDF) that will do the conversion:
Code:
Function ConvertEntry(myEntry As String) As String

    Dim i As Long
    Dim a As Integer
    Dim myString As String

'   Exit if empty
    If Len(myEntry) = 0 Then Exit Function
    
'   Loop through all characters
    For i = 1 To Len(myEntry)
'       First check for numbers
        If IsNumeric(Mid(myEntry, i, 1)) Then
            myString = myString & "9"
        Else
'           Check for letters
            a = Asc(Mid(myEntry, i, 1))
            If ((a >= 65) And (a <= 90)) Or ((a >= 97) And (a <= 122)) Then
                myString = myString & "L"
            Else
'               Default rest to underscore
                myString = myString & "_"
            End If
        End If
    Next i
    
    ConvertEntry = myString

End Function
So after placing that in VBA, you can use it like any other function right on the worksheet, or in VBA, i.e.

Worksheet formula

Code:
=ConvertEntry(A1)

VBA
Code:
Dim cell As Range
For Each cell In Range("A1:A10")
    cell.Value = ConvertEntry(cell.Value)
Next cell
 
Last edited:
Upvote 0
I suspect someone may be able to come up with something more efficient, but here is a User Defined Function (UDF) that will do the conversion:
Code:
Function ConvertEntry(myEntry As String) As String

    Dim i As Long
    Dim a As Integer
    Dim myString As String

'   Exit if empty
    If Len(myEntry) = 0 Then Exit Function
    
'   Loop through all characters
    For i = 1 To Len(myEntry)
'       First check for numbers
        If IsNumeric(Mid(myEntry, i, 1)) Then
            myString = myString & "9"
        Else
'           Check for letters
            a = Asc(Mid(myEntry, i, 1))
            If ((a >= 65) And (a <= 90)) Or ((a >= 97) And (a <= 122)) Then
                myString = myString & "L"
            Else
'               Default rest to underscore
                myString = myString & "_"
            End If
        End If
    Next i
    
    ConvertEntry = myString

End Function
Here is another way to write your UDF...
Code:
Function ConvertEntry(ByVal myEntry As String) As String
  Dim X As Long
  For X = 1 To Len(myEntry)
    If Mid(myEntry, X) Like "[A-Za-z]*" Then
      Mid(myEntry, X) = "L"
    ElseIf Mid(myEntry, X) Like "#*" Then
      Mid(myEntry, X) = "9"
    Else
      Mid(myEntry, X) = "_"
    End If
  Next
  ConvertEntry = myEntry
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,983
Members
452,540
Latest member
haasro02

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