Lấy số từ chuỗi bằng Vbscript.RegExp

Liên hệ QC

lameco411

Thành viên hoạt động
Tham gia
27/11/15
Bài viết
162
Được thích
63
Em tìm được đoạn code láy số từ chuỗi trên mạng, nhưng về dùng thì nó bán lỗi NAME:
Em đọc trong code thấy nó sao sao á. Mong mọi người giúp đỡ:
Mã:
Public Function lay_so(s As String) As String
    Dim s2 As String
    Dim replace_hyphen As String
    replace_hyphen = " "
    Set re = CreateObject("vbscript.regexp")
    If re Is Nothing Then Set re = CreateObject("vbscript.regexp")
    re.IgnoreCase = True
    re.Global = True
    re.Pattern = "[^0-9 -]" 'includes space, if you want to exclude space "[^0-9]"
    s2 = re.Replace(s, vbNullString)
    re.Pattern = "[^0-9 ]"
    lay_so = re.Replace(s2, replace_hyphen)
End Function
 
Em tìm được đoạn code láy số từ chuỗi trên mạng, nhưng về dùng thì nó bán lỗi NAME:
Em đọc trong code thấy nó sao sao á. Mong mọi người giúp đỡ:
Mã:
Public Function lay_so(s As String) As String
    Dim s2 As String
    Dim replace_hyphen As String
    replace_hyphen = " "
    Set re = CreateObject("vbscript.regexp")
    If re Is Nothing Then Set re = CreateObject("vbscript.regexp")
    re.IgnoreCase = True
    re.Global = True
    re.Pattern = "[^0-9 -]" 'includes space, if you want to exclude space "[^0-9]"
    s2 = re.Replace(s, vbNullString)
    re.Pattern = "[^0-9 ]"
    lay_so = re.Replace(s2, replace_hyphen)
End Function
Quan trọng bạn phải biết xác định pattern cho chuỗi đó.
Bạn có tìm bài Thầy @ndu96081631 đó. Bài Tách số không dùng vòng lặp đó.
 
Em tìm được đoạn code láy số từ chuỗi trên mạng, nhưng về dùng thì nó bán lỗi NAME:
Em đọc trong code thấy nó sao sao á. Mong mọi người giúp đỡ:
Mã:
Public Function lay_so(s As String) As String
    Dim s2 As String
    Dim replace_hyphen As String
    replace_hyphen = " "
    Set re = CreateObject("vbscript.regexp")
    If re Is Nothing Then Set re = CreateObject("vbscript.regexp")
    re.IgnoreCase = True
    re.Global = True
    re.Pattern = "[^0-9 -]" 'includes space, if you want to exclude space "[^0-9]"
    s2 = re.Replace(s, vbNullString)
    re.Pattern = "[^0-9 ]"
    lay_so = re.Replace(s2, replace_hyphen)
End Function
Bạn thử thêm dòng dưới đây trong phần khai báo xem sao
Mã:
Dim re As Object
Chạy thử trên office2016 thấy bình thường
 
Web KT
Back
Top Bottom