vanduynguyen1983
Thành viên hoạt động



- Tham gia
- 21/6/13
- Bài viết
- 101
- Được thích
- 8



Sub Tachso()
Dim ReG As Object, REMatches, j As Long, i As Long, k As Long
Set ReG = CreateObject("vbscript.regexp")
For i = 5 To 18
With ReG
.Global = True
.Pattern = "\d+"
Set REMatches = ReG.Execute(Cells(i, 2).Value)
End With
j = 2
For k = 0 To 2
j = j + 1
Cells(i, j).Value = REMatches(k)
Next
Next
End Sub
=LOOKUP(99^99,--MID(B5,MIN(FIND({0,1,2,3,4,5,6,7,8,9},B5&"0123456789")),ROW(INDIRECT("1:"&LEN(B5)))))
=LOOKUP(99^99,--MID(SUBSTITUTE(SUBSTITUTE(RIGHT(B5,LEN(B5)-SEARCH("d",B5,1)),",","x")," ","x"),MIN(FIND({0,1,2,3,4,5,6,7,8,9},SUBSTITUTE(SUBSTITUTE(RIGHT(B5,LEN(B5)-SEARCH("d",B5,1)),",","x")," ","x")&"0123456789")),ROW(INDIRECT("1:"&LEN(B5)))))
=LOOKUP(99^99,--MID(RIGHT(SUBSTITUTE(SUBSTITUTE(B5,",","x")," ","x"),LEN(B5)-SEARCH("x",SUBSTITUTE(SUBSTITUTE(B5,",","x")," ","x"),1)),MIN(FIND({0,1,2,3,4,5,6,7,8,9},RIGHT(SUBSTITUTE(SUBSTITUTE(B5,",","x")," ","x"),LEN(B5)-SEARCH("x",SUBSTITUTE(SUBSTITUTE(B5,",","x")," ","x"),1))&"0123456789")),ROW(INDIRECT("1:"&LEN(B5)))))
=--LEFT(B5,COUNT(--LEFT(B5,COLUMN(INDIRECT("1:"&LEN(B5))))))
=LOOKUP(99^99,--MID(SUBSTITUTE(RIGHT(B5,LEN(B5)-LEN(J5)),","," "),MIN(FIND({0,1,2,3,4,5,6,7,8,9},SUBSTITUTE(RIGHT(B5,LEN(B5)-LEN(J5)),","," ")&"0123456789")),ROW(INDIRECT("1:"&LEN(SUBSTITUTE(RIGHT(B5,LEN(B5)-LEN(J5)),","," "))))))
He he he - cái đầu tiên có thể ngắn chút dhn46 ạ :
PHP:=--LEFT(B5,COUNT(--LEFT(B5,COLUMN(INDIRECT("1:"&LEN(B5))))))
Cái thứ 2 thì không cần quan tâm chữ "d" vậy, tớ lại quan tâm dấu "," thôi, hix
PHP:=LOOKUP(99^99,--MID(SUBSTITUTE(RIGHT(B5,LEN(B5)-LEN(J5)),","," "),MIN(FIND({0,1,2,3,4,5,6,7,8,9},SUBSTITUTE(RIGHT(B5,LEN(B5)-LEN(J5)),","," ")&"0123456789")),ROW(INDIRECT("1:"&LEN(SUBSTITUTE(RIGHT(B5,LEN(B5)-LEN(J5)),","," "))))))
Cái thứ 3 thì ẹc ẹc...suy nghĩ tiếp, hiiiiiiiiiiii
Sub Tachso()
Application.ScreenUpdating = False
Dim ReG As Object, j As Long, i As Long, k As Long
Set ReG = CreateObject("vbscript.regexp")
For i = 5 To Range("B65536").End(3).Row
With ReG
.Global = True
.Pattern = "(\d+)\D+(\d+).*[,|\s](\d+).*"
Cells(i, 3) = .Replace(Cells(i, 2).Value, "$1")
Cells(i, 4) = .Replace(Cells(i, 2).Value, "$2")
Cells(i, 5) = .Replace(Cells(i, 2).Value, "$3")
End With
Next
Application.ScreenUpdating = True
End Sub
He he he - cái đầu tiên có thể ngắn chút dhn46 ạ :
PHP:=--LEFT(B5,COUNT(--LEFT(B5,COLUMN(INDIRECT("1:"&LEN(B5))))))
Cái thứ 2 thì không cần quan tâm chữ "d" vậy, tớ lại quan tâm dấu "," thôi, hix
PHP:=LOOKUP(99^99,--MID(SUBSTITUTE(RIGHT(B5,LEN(B5)-LEN(J5)),","," "),MIN(FIND({0,1,2,3,4,5,6,7,8,9},SUBSTITUTE(RIGHT(B5,LEN(B5)-LEN(J5)),","," ")&"0123456789")),ROW(INDIRECT("1:"&LEN(SUBSTITUTE(RIGHT(B5,LEN(B5)-LEN(J5)),","," "))))))
Cái thứ 3 thì ẹc ẹc...suy nghĩ tiếp, hiiiiiiiiiiii
=--LEFT(B5,COUNT(INDEX(--LEFT(B5,ROW($1:$255)),0)))
=--TRIM(LEFT(SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(UPPER(B5),C5&"D"," ",1),C5&"HD"," ",1),","," "),"A"," "))," ",REPT(" ",255)),255))
=--TRIM(LEFT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(MID(TRIM(UPPER(B5)),FIND(",",SUBSTITUTE(TRIM(B5)," ",","))+1,255),"B",REPT(" ",255)),"T",REPT(" ",255)),"@",REPT(" ",255)),255))



Thử code này xem :
PHP:Sub Tachso() Dim ReG As Object, REMatches, j As Long, i As Long, k As Long Set ReG = CreateObject("vbscript.regexp") For i = 5 To 18 With ReG .Global = True .Pattern = "\d+" Set REMatches = ReG.Execute(Cells(i, 2).Value) End With j = 2 For k = 0 To 2 j = j + 1 Cells(i, j).Value = REMatches(k) Next Next End Sub
Hị hị hị - Xem lại kết quả mẫu thì thấy k có quy luật nào - code phá sản, ẹ ẹ ẹ - chỉ có thể dùng cái này làm trung gian ĐỠ TẠM thôi người ơi, hix hix
Function So_Thanh(rng) As Long
Dim str$, match As Object
str = CStr(rng)
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "^\d+(?=D|d|H)"
If .test(str) Then
Set match = .Execute(str)
So_Thanh = CLng(match(0))
End If
End With
End Function
Function duong_kinh(rng) As Long
Dim str$, match As Object
str = CStr(rng)
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "\d+D(?=\d+)|(?!\d+).+"
If .test(str) Then
duong_kinh = CLng(.Replace(str, ""))
End If
End With
End Function
Function chieu_dai(rng) As Long
Dim str$, match As Object
str = CStr(rng)
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "\w+[,|\s+](\d+).*"
If .test(str) Then
chieu_dai = CLng(.Replace(str, "$1"))
End If
End With
End Function
Function Tach(Str As String, Num As Long)
With CreateObject("vbscript.Regexp")
.Global = True
.Pattern = "(\d+)\D+(\d+).*[,|\s](\d+).*"
a = "$" & Num
Tach = .Replace(Str, "$" & Num)
End With
End Function
=tach($B5,COLUMN(A:A))
Function So_Thanh(rng) As Long
Dim str$, match As Object
str = CStr(rng)
With CreateObject("vbscript.regexp")
.Global = True
[B] .Pattern = "^\d+(?=D|d|H)"[/B]
If .test(str) Then
Set match = .Execute(str)
So_Thanh = CLng(match(0))
End If
End With
End Function
Function duong_kinh(rng) As Long
Dim str$, match As Object
str = CStr(rng)
With CreateObject("vbscript.regexp")
.Global = True
[B] .Pattern = "\d+D(?=\d+)|(?!\d+).+"[/B]
If .test(str) Then
duong_kinh = CLng(.Replace(str, ""))
End If
End With
End Function
Function chieu_dai(rng) As Long
Dim str$, match As Object
str = CStr(rng)
With CreateObject("vbscript.regexp")
.Global = True
[B] .Pattern = "\w+[,|\s+](\d+).*"[/B]
If .test(str) Then
chieu_dai = CLng(.Replace(str, "$1"))
End If
End With
End Function



Đúng là mình nhầm thật, hôm qua buồn ngủ ko xem kỹ đề bàiNếu Function thì mình làm như thế này
Trong đóMã:Function Tach(Str As String, Num As Long) With CreateObject("vbscript.Regexp") .Global = True .Pattern = "(\d+)\D+(\d+).*[,|\s](\d+).*" a = "$" & Num Tach = .Replace(Str, "$" & Num) End With End Function
Num = 1: Số thanh
Num = 2: Đường kính
Num = 3: Chiều dài
=> Công thức tại C5
Kéo sang các ô và dòng còn lạiMã:=tach($B5,COLUMN(A:A))
-----------------------------------------------------------------------------------
!

