这是我到目前为止的代码,它实际上删除了单元格中的所有内容,但是删除了数字。这看起来像这样:
Sub myTest()
Dim myCel As Range
Dim i As Integer
i = 0
'copier le contenu de la colonne A dans la colonne B
Columns("A:A").Select
Selection.Copy
Columns("B:B").Select
ActiveSheet.Paste
'on supprime tout ce qui n'est pas un nombre
With CreateObject("VBScript.Regexp")
.Global = True
.Pattern = "\D+"
For Each myCel In Range("B1:B900")
myCel.Value = .Replace(myCel.Value, "")
Next
End With
End Sub
它工作正常......现在,我想要做的是将单元格的内容分成3个单元格:一个包含数字之前的所有内容,一个包含数字,另一个包含数字之后的内容。我有这样的专栏:
<g0 t="bold">
</g0>
<g1>
</g1>
<g2>
</g2>
<g3>
</g3>
<g4>
</g4>
<i5 t="lb"/>
<i6 t="lb"/>
<g7>
</g7>
<g8>
</g8>
<i9 t="lb"/>
<i10 t="lb"/>
<i11 t="lb"/>
我想得到这样的结果:(在NP ++中,正则表达式是(&lt;。?)([0-9] {1,3})(。&gt;)然后用\ 1 \ t \ 2 \ t \ 3替换。好吧,在VBA中它更复杂,我想在这个上寻求帮助...希望有人会有一个想法:)
<g 0 t="bold">
</g 0 >
<g 1 >
</g 1 >
<g 2 >
</g 2 >
<g 3 >
</g 3 >
<g 4 >
</g 4 >
<i 5 t="lb"/>
<i 6 t="lb"/>
<g 7 >
</g 7 >
<g 8 >
</g 8 >
<i 9 t="lb"/>
<i 10 t="lb"/>
<i 11 t="lb"/>
现在我的问题不在于正则表达式本身(我的正则表达式相当不错),而不是VBA代码,它允许我真正拆分单元格并将3个部分发送到循环中的3个不同单元格...... / p>
答案 0 :(得分:2)
你与Regex非常接近,我不得不做一个小修改。
首先,请确保将引用添加到&#34; Microsoft VBScript正则表达式5.5&#34;至于您的VBA模块(请参阅此link了解如何执行此操作)。
Private Sub TestRegex()
Dim regEx As New RegExp
Dim strPattern As String
Dim strInput As String
Dim strRaplace As String
Dim strOutput As String
Dim Myrange As Range
Set Myrange = ActiveSheet.Range("B1:B19")
For Each C In Myrange
strPattern = "(\<.?.?)([0-9]{1,3})(.*>)"
If strPattern <> "" Then
strInput = C.Value
strReplace = "$1"
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.Test(strInput) Then
C.Offset(0, 1) = regEx.Replace(strInput, "$1")
C.Offset(0, 2) = regEx.Replace(strInput, "$2")
C.Offset(0, 3) = regEx.Replace(strInput, "$3")
Else
C.Offset(0, 1) = "(Not matched)"
End If
End If
Next
End Sub
我的输入在B列,输出显示在C,D&amp; ë
答案 1 :(得分:0)
如果没有正则表达式,请选择您的单元格:
Sub parser101()
Dim r As Range, v As String, L As Long
Dim I As Long, N1 As Long, N2 As Long
N1 = 0
N2 = 0
For Each r In Selection
v = r.Text
L = Len(v)
For I = 1 To L
If IsNumeric(Mid(v, I, 1)) Then
N1 = I
GoTo escape101
End If
Next I
'
escape101:
'
For I = N1 To L
If Not IsNumeric(Mid(v, I, 1)) Then
N2 = I
GoTo escape102
End If
Next I
'
escape102:
'
r.Offset(0, 1) = Mid(v, 1, N1 - 1)
r.Offset(0, 2) = Mid(v, N1, N2 - N1)
r.Offset(0, 3) = Mid(v, N2)
Next r
End Sub