VBA 如何在字符串拆分后保留特定的逗号

VBA 如何在字符串拆分后保留特定的逗号

我试图在字符串拆分后保留一个逗号,所以当我在单元格中输入一些数据时,例如-C1 宽度 2,-保留这个逗号 C8 合并 2,而不是像当前输出-C1 宽度 2 C8 合并 2。所以这是我的代码:

Sub Worksheet_Change(ByVal Target As Range)


Application.EnableEvents = False
Dim BigS As String
Dim arr As Variant
Dim a As Variant


If Intersect(Range("G:G"), Target) Is Nothing Then Exit Sub
arr = Split(Target, " ")
For Each a In arr
    If IsItGood(a) Then
    MsgBox (" In cell" + Target.Address(0, 0)) & vbCrLf & a & vbCrLf + "is ok"
    Else
        MsgBox (" In cell" + Target.Address(0, 0)) & vbCrLf & a & vbCrLf + "has invalid values"
        Application.Undo
    End If

    Next a
Application.EnableEvents = True
End Sub

目前,此拆分方法忽略所有逗号,但我想保留 3 组字符串之间的每个逗号 - C1 宽度 2“此处为逗号”C8 合并 2

答案1

以供参考:

上一个问题

好吗()代码为:

Public Function IsItGood(aWord As Variant) As Boolean
    Dim s As String
    s = "|"
    tmp = s & aWord & s
    patern = ""

    For i = 1 To 100
        patern = patern & s & i
    Next i
    For i = 1 To 10
        patern = patern & s & "C" & i
    Next i
    patern = patern & s & "merge|complete framed|width|border left|border right" & s

    If InStr(1, patern, tmp) > 0 Then
        IsItGood = True
    Else
        IsItGood = False
    End If

End Function

要使其“盲化逗号”,请更改:

tmp = s & aWord & s

到:

tmp = Replace(s & aWord & s, ",", "")

无论是否有逗号,输入短语中的单词之间仍然需要一个空格字符。

相关内容