想請教在VBA中
如何使用正則拆分字元
761___100某某股份有限公司
778956___104某某股份有限公司
7956___124某某股份有限公司
99750___某某股份有限公司
拆成
761___某某股份有限公司 與 100
778956___某某股份有限公司 與 104
7956___某某股份有限公司 與 124
99750___某某股份有限公司 "全部" < 直接填入全部這個字串<br>
wei9133 wrote:
想請教在VBA中
如何使用正則拆分字元...(恕刪)
這種文字格式用簡單函數就可以了
為了方便您看懂,公式先拆成3部份,您再自己合併
b1=ISNUMBER(VALUE(MID(A1,FIND("___",A1)+3,3)))
c1=IF(B1,REPLACE(A1,FIND("___",A1)+3,3,""),A1)
d1=IF(B1,MID(A1,FIND("___",A1)+3,3),"")

vba 也是簡單的計算就可以了,不需要用到正規表達式這種東西
Sub test()
For i = 1 To 4
check = Left(Split(Cells(i, 1), "___")(1), 3)
If IsNumeric(check) Then
Cells(i, 2) = Replace(Cells(i, 1), check, "")
Cells(i, 3) = check
Else
Cells(i, 2) = Cells(i, 1)
End If
Next i
End Sub
追加底線後的數字個數不是固定3個之情形
函數解法之公式詳解,請參考:https://goo.gl/24YHfE

joblyc017 wrote:
底線後的數字個數不是固定3個之情形...(恕刪)
如果是這樣那改用正規(則)表達式會比較方便
故意把字串變複雜一點
一、底線長度不固定
二、沒有底線,只有空格
三、數字字數不固定
四、有重覆的數字例如地址

'=============================================
Sub test()
Dim temp As Object
For i = 1 To 10
With CreateObject("vbscript.regexp")
.Pattern = "\d+"
.IgnoreCase = True
.MultiLine = False
.Global = True
Set temp = .Execute(Cells(i, 1))
If temp.Count >= 2 Then
Cells(i, 3) = temp(1)
If Split(Cells(i, 1), " ")(0) = temp(0) Then
Cells(i, 2) = Replace(Cells(i, 1), " " & temp(1), " ")
Else
Cells(i, 2) = Replace(Cells(i, 1), "_" & temp(1), "_")
End If
Else
Cells(i, 2) = Cells(i, 1)
End If
End With
Next i
End Sub
'=============================================
內文搜尋
X




























































































