我在Cell中有这样的格式:
S: 7
P:
M: 1
L:1
K: 1
Q: 7
我想提取每个数字并将其写在另一列中,例如forS:7我想在coumn BC写7,P:0,0在coulmn BD和...(数字之间的空格和:很灵活) 我怎么能用vba做到这一点?
更新:
Dim objRegex As Object
Dim objRegMC As Object
Dim objRegM As Object
Dim lngCnt As Long
usedRowCount = Worksheets("Übersicht_2013").UsedRange.Rows.Count
For i = 1 To usedRowCount
cellAYvalue = Worksheets("Übersicht_2013").Cells(i, "AY").Value
cellAYvalue = Replace(cellAYvalue, " ", "")
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "[A-Z]:\s?(\d?)"
.Global = True
If .test(cellAYvalue) Then
Set objRegMC = .Execute(cellAYvalue)
For Each objRegM In objRegMC
'[bc1].Offset(0, lngCnt) = objRegM.submatches(0)
Worksheets("Übersicht_2013").Cells(i, "BC") = objRegM.submatches(0)
lngCnt = lngCnt + 1
Next
End If
End With
Next i
由于
答案 0 :(得分:2)
我使用regexp而不是屠夫进行长串测试
此代码采用字符串
S:7 P:M:1L:1K:1Q:7
并在BC1
中添加7,将BD1
留空,在BE1
等中添加1
指向一个单元格 - 比如A1
- 你可以使用
StrIn = Range("A1")
<强>码强>
Sub RecUt()
Dim StrIn As String
Dim objRegex As Object
Dim objRegMC As Object
Dim objRegM As Object
Dim lngCnt As Long
StrIn = "S: 7 P: M: 1L:1K: 1Q: 7"
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "[A-Z]:\s?(\d?)"
.Global = True
If .TEST(StrIn) Then
Set objRegMC = .Execute(StrIn)
For Each objRegM In objRegMC
[bc1].Offset(0, lngCnt) = objRegM.submatches(0)
lngCnt = lngCnt + 1
Next
End If
End With
End Sub