Mọi người giúp đỡ code này với ạ sao đừng để lỗi (1 người xem)

Liên hệ QC

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

hoanhnnc

Thành viên chính thức
Tham gia
13/3/08
Bài viết
58
Được thích
0
Function gc(cmt As Range) As Double
Dim vDat As Variant
Dim i As Long
Dim res As Double
vDat = Split(CleanString(cmt.Comment.Text), vbCrLf)
For i = LBound(vDat) To UBound(vDat)
If Len(vDat(i)) > 0 Then
res = res + CDbl(vDat(i))
End If
Next i
gc = res
End Function
 

File đính kèm

Function gc(cmt As Range) As Double
Dim vDat As Variant
Dim i As Long
Dim res As Double
vDat = Split(CleanString(cmt.Comment.Text), vbCrLf)
For i = LBound(vDat) To UBound(vDat)
If Len(vDat(i)) > 0 Then
res = res + CDbl(vDat(i))
End If
Next i
gc = res
End Function
Thêm
On Error Resume Next
Sau dòng Dim
 
Upvote 0
Function gc(cmt As Range) As Double
Dim vDat As Variant
Dim i As Long
Dim res As Double
vDat = Split(CleanString(cmt.Comment.Text), vbCrLf)
For i = LBound(vDat) To UBound(vDat)
If Len(vDat(i)) > 0 Then
res = res + CDbl(vDat(i))
End If
Next i
gc = res
End Function
Code trên không có điều kiện loại những ô không có Comment
Bạn thử như thế này xem
Mã:
Function CleanString(strIn As String) As String
    Dim objRegex
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
    .Global = True
    .Pattern = "[^0-9" & Application.DecimalSeparator & "]"
    CleanString = .Replace(strIn, vbCrLf)
End With
End Function
Function gc(cmt As Range) As Double
    Dim vDat As Variant, cll As Range
    Dim i As Long, res As Double
    For Each cll In cmt
        If Not cll.Comment Is Nothing Then
            vDat = Split(CleanString(cll.Comment.Text), vbCrLf)
            For i = LBound(vDat) To UBound(vDat)
                If Len(vDat(i)) > 0 Then
                    res = res + CDbl(vDat(i))
                End If
            Next i
        End If
    Next
    If res Then gc = res
End Function
 
Upvote 0
Code trên không có điều kiện loại những ô không có Comment
Bạn thử như thế này xem
Mã:
Function CleanString(strIn As String) As String
    Dim objRegex
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
    .Global = True
    .Pattern = "[^0-9" & Application.DecimalSeparator & "]"
    CleanString = .Replace(strIn, vbCrLf)
End With
End Function
Function gc(cmt As Range) As Double
    Dim vDat As Variant, cll As Range
    Dim i As Long, res As Double
    For Each cll In cmt
        If Not cll.Comment Is Nothing Then
            vDat = Split(CleanString(cll.Comment.Text), vbCrLf)
            For i = LBound(vDat) To UBound(vDat)
                If Len(vDat(i)) > 0 Then
                    res = res + CDbl(vDat(i))
                End If
            Next i
        End If
    Next
    If res Then gc = res
End Function
Dạ Cảm ơn Anh chị, em nhiều.
 
Upvote 0
Web KT

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

Back
Top Bottom