Tách số không dùng vòng lặp (3 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

ndu96081631

Huyền thoại GPE
Thành viên BQT
Super Moderator
Tham gia
5/6/08
Bài viết
30,703
Được thích
53,957
Sưu tầm được 1 hàm tách số khá hay, xin chia sẽ với mọi người!
Điểm đặc biệt là không dùng tí vòng lập nào!
PHP:
Function TachSo(Cell As Range) As Double
  Set Temp = CreateObject("VBScript.RegExp")
  Temp.Global = True
  Temp.Pattern = "[^0-9]"
  TachSo = Temp.Replace(Cell, "")
End Function
Các bạn có thể tham khảo và phát triển thêm tùy theo yêu cầu riêng!
 

File đính kèm

Góp vui

Tách lấy số

PHP:
Function TachSo(Cell As Range) As Double
  Set Temp = CreateObject("VBScript.RegExp")
  Temp.Global = True
  Temp.Pattern = "\D"
  TachSo = Temp.Replace(Cell, "")
End Function


Tách lấy chữ
PHP:
Function TachChu(Cell As Range) As String
Set Temp = CreateObject("VBScript.RegExp")
Temp.Global = True
Temp.Pattern = "\d"
TachChu = Temp.Replace(Cell, "")
End Function

Tham khảo thêm để tùy biến:

Symbol​
|
Description​
|
\|Marks the next character as either a special character (such as \n for the newline character) or as a literal (if that character otherwise has special meaning in a pattern search string). The special characters are:|
| |
|\f|
| |
|form feed character|
| |
|\n|
| |
|newline character|
| |
|\r|
| |
|carriage return character|
| |
|\t|
| |
|tab character|
| |
|\v|
| |
|vertical tab character|
| |
^|Matches the beginning of input.|
$|Matches the end of input.|
*|Matches the preceding atom zero or more times.|
+|Matches the preceding atom one or more times.|
?|Matches the preceding atom zero or one time.|
.|Matches any single character except a newline character.|
( )|Defines a subexpression within the larger subexpression. A subexpression:|
| |
|Overrides the order of precedence used in evaluating pattern strings.|
| |
|Can be referenced again in the pattern string. To insert the result of the subexpression later in the pattern string, reference it by its one-based ordinal position among subexpressions, preceded by the backslash symbol (e.g., \1). See the example using the \num syntax in the "Programming Tips and Gotchas" section.|
| |
|Can be referenced again in the replacement string in calls to the RegExp.Replace method. To use the result of the original subexpression as a replacement string, reference its one-based ordinal position among subexpressions, preceded by a dollar sign (e.g., $1). See RegExp.Replace Method for an example.|
| |
x|y|Matches either x or y.|
{n}|Matches exactly n times, where n is a nonnegative integer.|
{n,}|Matches at least n times, where n is a nonnegative integer. o{1,} is the same as o+, and o{0,} is the same as o*.|
{n,m}|Matches at least n and at most m times, where m and n are nonnegative integers. o{0,1} is the same as o?.|
[abc]|Matches any one of the enclosed characters (represented by abc) in the character set.|
[^xyz]|Matches any character (represented by xyz) not enclosed in the character set. For example, [^abc] matches the "p" in "plain."|
[a-z]|Matches any character in a range of characters (represented by a-z).|
[^m-z]|Matches any character not included in a range of characters (represented by m-z).|
\b|Matches a word boundary; that is, the position between a word and a space. The word boundary symbol does not include newline characters or the end of input (see the \s symbol).|
\B|Matches a nonword boundary. ea*r\B matches the "ear" in "never early."|
\d|Matches a digit character. Equivalent to [0-9].|
\D|Matches a nondigit character. Equivalent to [^0-9].|
\s|Matches any whitespace, including space, tab, form-feed, etc. Equivalent to [ \f\n\r\t\v].|
\S|Matches any nonwhitespace character. Equivalent to [^ \f\n\r\t\v].|
\w|Matches any word character including underscore. Equivalent to [A-Za-z0-9_].|
\W|Matches any nonword character, including whitespace and carriage returns. Equivalent to [^A-Za-z0-9_].|
\num|Matches the subexpression (enclosed in parentheses) whose ordinal position in the pattern is num, where num is a positive integer.|
\n|Matches n, where n is the octal value of an ASCII code. Octal escape values must be 1, 2, or 3 digits long and must not exceed 256; if they do, only the first two digits are used.|
\xn|Matches n, where n is the hexadecimal value of an ASCII code. Hexadecimal escape values must be two digits long.|
 
Upvote 0
Âu Dương Phong sưu tầm bài rất hay. Nhưng mình thử tách số thì thấy có 1 vấn đề không giải thích được:
2 function có nội dung hoàn toàn giống nhau, nhưng 1 hàm ra dạng số, 1 hàm ra dạng chuỗi.
|A|B|C
1| | |
2|00abcd123prt7474rur099|001237474099|=TachChu(A2)
3|00abcd123prt7474rur099|1237474099|=TachSo(A3)
Mã:
Function TachSo(Cell As Range) As Double
Set Temp = CreateObject("VBScript.RegExp")
Temp.Global = True
Temp.Pattern = "[^0-9]"
TachSo = Temp.Replace(Cell, "")
End Function
Mã:
Function TachChu(Cell As Range) As String
Set Temp = CreateObject("VBScript.RegExp")
Temp.Global = True
Temp.Pattern = "[^0-9]"
TachChu = Temp.Replace(Cell, "")
End Function
 

File đính kèm

Upvote 0
Âu Dương Phong sưu tầm bài rất hay. Nhưng mình thử tách số thì thấy có 1 vấn đề không giải thích được:
2 function có nội dung hoàn toàn giống nhau, nhưng 1 hàm ra dạng số, 1 hàm ra dạng chuỗi.
|A|B|C
1| | |
2|00abcd123prt7474rur099|001237474099|=TachChu(A2)
3|00abcd123prt7474rur099|1237474099|=TachSo(A3)
Mã:
Function TachSo(Cell As Range) As Double
.....
End Function
Mã:
Function TachChu(Cell As Range) As String
....
End Function

Bác Long xem kiểu trả về phía sau hàm thì thấy khác nhau.
 
Chỉnh sửa lần cuối bởi điều hành viên:
Upvote 0
Upvote 0
Âu Dương Phong sưu tầm bài rất hay. Nhưng mình thử tách số thì thấy có 1 vấn đề không giải thích được:
2 function có nội dung hoàn toàn giống nhau, nhưng 1 hàm ra dạng số, 1 hàm ra dạng chuỗi.
Thầy khai báo TachChu là dạng String thì đương nhiên nó phải là chuổi rồi!
Hay ý thầy là... cái gì khác nữa (chứ lý nào thầy lại không biết vụ này)
Ẹc... Ẹc...
 
Chỉnh sửa lần cuối bởi điều hành viên:
Upvote 0
Thầy khai báo TachChu là dạng String thì đương nhiên nó phải là chuổi rồi!
Hay ý thầy là... cái gì khác nữa (chứ lý nào thầy lại không biết vụ này)
Ẹc... Ẹc...
Ồ !!!
Mình copy code trên GPE rồi chỉnh nội dung lại. Thấy 2 code giống nhau nhưng không chú ý khai báo String,Double.
Nếu đi thi chắc là theo Bùi Kiệm rồi !
Thank ndu96081631
 
Upvote 0
Upvote 0
Anh thử vào Tools | References ... | chọn Microsoft VBScript Regular Expressions xx (cái nào mới nhất thì chọn) xem có chạy được không.

Thêm 1 tài liệu tham khảo dạng PDF nữa về VBscript

http://www.indusoft.com/pdf/VBScript Reference.pdf
Không kiếm ra, kiếm hết vần M rồi. Chắc phải cài thêm .dll hay .ocx. Không dám down theo link Microsoft, sợ MS phát hiện gian lận lại hiện ngôi sao.
 
Upvote 0
Mình có một danh sách dãy số 09135665611, vì nó thừa số 3 trong dãy, mình muốn bỏ nó đi phải làm thế nào, các bác giúp với
 
Upvote 0
Bạn muốn loại trừ chữ số 3 tại vị trí thứ 4 (Nếu có) trong số dãy số bạn có thể dùng công thức như sau
Ví dụ: Tư A2:A20 chứa danh sách dãy số , tại C2 gõ công thức: =IF(MID(A2,4,1)="3",REPLACE(A2,4,1,""),A2)
-Fill hết C2:C20 ta được vùng kết quả
-Chép vùng kết quả
-Chọn A2:A20-->PasteSpeacial-->Value-->OK
 
Upvote 0
Tách số trong chuổi text

Mình có dữ liệu như thế này ai có thể giúp mình với!
Menu,18 Parameter,31 => #18.31
Menu,1 Parameter,31 => #1.31
Menu,1 Parameter,3 => #1.3
Menu,18 Parameter,1 => #18.1
Mình không rành về excel
Cám ơn nhiều nhé!
 
Lần chỉnh sửa cuối:
Upvote 0
Nếu thuần thế này thì dùng công thức

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A4," ",""),"Menu,","#"),"Parameter,",".")
 
Upvote 0
Cảm ơn bạn nhiều! Gần được rồi nhưng mình ghi thiếu một chút bạn có thể làm lại dùm mình xem sao?
Tách số trong chuổi text

Mình có dữ liệu như thế này ai có thể giúp mình với!
Menu,18 Parameter,31 => #18.31
Menu,1 Parameter,31 => #01.31
Menu,1 Parameter,3 => #01.03
Menu,18 Parameter,1 => #18.01
Menu,0 Parameter,10 => #00.10
Menu,0 Parameter,0 => #00.00




Mình không rành về excel
Cám ơn nhiều nhé!​
 
Upvote 0
Thử lại xem sao

="#"&TEXT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1," ",""),"Menu,",""),"Parameter,","."),"00.00")
 
Upvote 0
--
Tất cả đều được riêng
Menu,11 Parameter 1 thì chưa được => #11.10 phải là #11.01 mới được.
Mong bạn giúp!
Cảm ơn bạn rất nhiều.
Hồi nãy bạn nói chuổi của bạn được viết theo nguyên tắc Menu,11 Parameter, 1 ---> Tức sau chữ MenuParameter là đến dấu phẩy ---> Công thức cũng sẽ theo nguyên tắc này mà tách ---> Giờ bạn lại ghi chuổi thành Menu,11 Parameter 1 ---> Sau chữ Parameter lại không có dấu phẩy nào ---> Vậy nó ra kết quả sai là hiển nhiên thôi
Thử sửa công thức thành vầy xem:
PHP:
="#"&TEXT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1," ",""),"Menu",""),"Parameter","."),",",""),"00.00")
 
Upvote 0
Hồi nãy bạn nói chuổi của bạn được viết theo nguyên tắc Menu,11 Parameter, 1 ---> Tức sau chữ MenuParameter là đến dấu phẩy ---> Công thức cũng sẽ theo nguyên tắc này mà tách ---> Giờ bạn lại ghi chuổi thành Menu,11 Parameter 1 ---> Sau chữ Parameter lại không có dấu phẩy nào ---> Vậy nó ra kết quả sai là hiển nhiên thôi
Thử sửa công thức thành vầy xem:
PHP:
="#"&TEXT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1," ",""),"Menu",""),"Parameter","."),",",""),"00.00")
Nguyên tắc vẩn như củ chỉ mình viết lại thiếu dấu "," thôi.
Vi du: Menu,1 Parameter,1 => #01.01 còn theo công thức bạn làm thì lại là #01.10
Tức các số nhập vào <9 thì trước đó phải thêm số 0.
Bạn xem còn có cách nào nữa không.
Cảm ơn bạn nhiều!
 
Lần chỉnh sửa cuối:
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom