这就是我最终使用的。它将输出填充到相邻单元格
例如。 080110- 0015 将是输入
ABCDEF- GHIJ 将是输出
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Range("A1:A6"), Range(Target.Address)) Is Nothing Then
Call replace_Text("A", "1")
End If
End Sub
以上是我如何调用该函数。
Sub replace_Text(C As String, R As String)
Set cel = Range(C & R)
Dim editText$
editText = cel.Value
Dim i&, k&
k = 1
For i = 1 To Len(editText)
If IsLetter(Mid(editText, i, 1)) Then
Select Case (InStr(editText, Chr$(64 + k)) > 0)
Case True
editText = WorksheetFunction.Substitute(editText, Mid(editText, i, 1), Chr$(64 + k), 1)
k = k + 1
Case False
editText = WorksheetFunction.Replace(editText, i, 1, Chr$(64 + k))
k = k + 1
End Select
ElseIf IsNum(Mid(editText, i, 1)) Then
editText = WorksheetFunction.Substitute(editText, Mid(editText, i, 1), Chr$(64 + k), 1)
k = k + 1
End If
Next i
Dim cel2
Set cel2 = Range("B" & R)
cel2.Value = editText
End Sub
Function IsNum(strValue As String) As Boolean
Dim intPos As Integer
For intPos = 1 To Len(strValue)
Select Case Asc(Mid(strValue, intPos, 1))
Case 47 To 57, 47 To 57
IsNum = True
Case Else
IsNum = False
Exit For
End Select
Next
End Function
Function IsLetter(strValue As String) As Boolean
Dim intPos As Integer
For intPos = 1 To Len(strValue)
Select Case Asc(Mid(strValue, intPos, 1))
Case 65 To 90, 97 To 122
IsLetter = True
Case Else
IsLetter = False
Exit For
End Select
Next
End Function