【问题标题】:Looping through string in excel, replacing Chars with Alphabet在excel中循环字符串,用字母替换字符
【发布时间】:2016-10-26 08:26:15
【问题描述】:

我在 Excel 中处理字符串,尝试用字母(按顺序)替换字符(忽略空格、破折号和句点)。 这些字符串位于单独的单元格中。我只想一次做一个单元格。 前任。 SG6 -099 将变成 ABC -DEF 和 3F5234-42- GA 将变成 ABCDEF-GH- IJ

这些字符串不会超过 26 个字符。

这可能吗?

提前致谢

【问题讨论】:

  • 欢迎来到 StackOverflow。请注意,这不是免费的代码编写服务。然而,我们渴望用他们的代码帮助其他程序员(和有志者)。请阅读How do I Ask a Good Question 上的帮助主题。您可能还想take the tour 并在这样做的同时获得徽章。之后,请使用您迄今为止编写的 VBA 代码更新您的问题,以完成您希望完成的任务。
  • SG6 -099 will turn into ABC -EFG 为什么会跳过D
  • 修好了,是我的错

标签: sql string excel vba loops


【解决方案1】:

我能够组合两个潜艇(一个我有,另一个我找到了)。这是如何工作的?

Function replace_Text(ByVal cel as Range) as String


Dim editText$
editText = cel.Value

Dim i&, k&
k = 1
For i = 1 To Len(editText)
    If IsLetter(Mid(editText, i, 1)) Then
        editText = WorksheetFunction.Substitute(editText, Mid(editText, i, 1), Chr$(64 + k), 1)
        k = k + 1
    End If
Next i

replace_Text = editText
End Function

Function IsLetter(strValue As String) As Boolean
' https://techniclee.wordpress.com/2010/07/21/isletter-function-for-vba/
    Dim intPos As Integer
    For intPos = 1 To Len(strValue)
        Select Case Asc(Mid(strValue, intPos, 1))
            Case 48 To 57, 65 To 90, 97 To 122
                IsLetter = True
            Case Else
                IsLetter = False
                Exit For
        End Select
    Next
End Function

在单元格中,键入=text_Replace(A1),其中A1 是带有字符串的单元格。

【讨论】:

  • 我是 excel 脚本的新手,您能解释一下这是如何工作的吗?我将其粘贴到工作表的视图代码中,并在单元格 A1 中键​​入了字符串,但似乎没有任何反应。我错过了什么吗?
  • @Luke - 万岁!如果这可行,介意标记为答案吗? (检查帖子左侧的标记)
【解决方案2】:

这就是我最终使用的。它将输出填充到相邻单元格

例如。 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

【讨论】:

    猜你喜欢
    • 2019-05-31
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 2015-11-28
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多