Giúp viết 1 hàm VB. (3 người xem)

Liên hệ QC

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

huynhson0102

Thành viên mới
Tham gia
25/8/16
Bài viết
17
Được thích
0
Mình có 1 file báo cáo, ở dòng Total mình đã dùng 1 hàm VB để đếm số lượng công việc. Ở dòng Finished Mình muốn đếm lượng việc đã kết thúc bằng cách đếm ở dòng O6:O125 (tìm chuỗi có chữ Done ở cuối và cộng dồn tương tự Total).
Mình nhờ các bạn dựa vào Total giúp viết CountDone để đếm việc đã hoàn thành. Cám ơn...
 

File đính kèm

Có 37 thôi; không fải 38 như bạn muốn:

PHP:
Option Explicit
Function Done(Rng As Range)
 Dim Arr()
 Dim J As Long

 Arr() = Rng.Value
 For J = 1 To UBound(Arr())
    If Arr(J, 1) = "" Then Exit For
    If InStr(Arr(J, 1), "Done") Then
        Done = Done + 1
    End If
 Next J
End Function
 
Có 37 thôi; không fải 38 như bạn muốn:

PHP:
Option Explicit
Function Done(Rng As Range)
 Dim Arr()
 Dim J As Long

 Arr() = Rng.Value
 For J = 1 To UBound(Arr())
    If Arr(J, 1) = "" Then Exit For
    If InStr(Arr(J, 1), "Done") Then
        Done = Done + 1
    End If
 Next J
End Function
Bác ơi, còn lọc lại dữ liệu trùng nữa, giống hàm CountUnique trong file vậy đó. Ví dụ Cột O6 là "aDone", cột O12 là "aDone" thì nó chỉ tính là 1 thôi. Hàm bác viết giúp mình là cái nào có Done thì đếm thôi, k có tính dữ liệu trùng.
Hàm CountUnique:

PHP:
Option Explicit
Function CountUnique(Rng As Range) As Integer
 Dim Clls As Range, tStr As String, temp As Long
 tStr = "@"
 For Each Clls In Rng
    If Clls <> "" And InStr(1, tStr, "@" & Clls & "@") = 0 Then
        temp = 1 + temp
        tStr = tStr & Clls & "@"
    End If
 Next
CountUnique = temp
End Function
Hàm này chỉ lọc được thôi, không có chức năng tìm chữ Done, bác tìm cách thêm chữ done vô giúp em với.
 
Lần chỉnh sửa cuối:
Bác ơi, còn lọc lại dữ liệu trùng nữa, giống hàm CountUnique trong file vậy đó. Ví dụ Cột O6 là "aDone", cột O12 là "aDone" thì nó chỉ tính là 1 thôi. Hàm bác viết giúp mình là cái nào có Done thì đếm thôi, k có tính dữ liệu trùng.
Hàm CountUnique:

PHP:
Option Explicit
Function CountUnique(Rng As Range) As Integer
 Dim Clls As Range, tStr As String, temp As Long
 tStr = "@"
 For Each Clls In Rng
    If Clls <> "" And InStr(1, tStr, "@" & Clls & "@") = 0 Then
        temp = 1 + temp
        tStr = tStr & Clls & "@"
    End If
 Next
CountUnique = temp
End Function
Hàm này chỉ lọc được thôi, không có chức năng tìm chữ Done, bác tìm cách thêm chữ done vô giúp em với.
Bạn thay đổi 1 tí là được
Mã:
[COLOR=#0000bb][FONT=Courier New]Option Explicit
[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]Function [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]CountUniqueDone[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Rng [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]As [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Range[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]) As [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Integer
 Dim Clls [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]As [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Range[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]tStr [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]As [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]String[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]temp [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]As [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Long
 tStr [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]= [/FONT][/COLOR][COLOR=#dd0000][FONT=Courier New]"@"
 [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]For [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Each Clls In Rng
    [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]If [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Clls [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]<> [/FONT][/COLOR][COLOR=#dd0000][FONT=Courier New]"" [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]And [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]InStr[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]1[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]tStr[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#dd0000][FONT=Courier New]"@" [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]& [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Clls [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]& [/FONT][/COLOR][COLOR=#dd0000][FONT=Courier New]"@"[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]) = [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]0 [COLOR=#007700][FONT=Courier New]And [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]InStr[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]1[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]clls[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], "Done"[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]) > [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]0[/FONT][/COLOR] Then
        temp [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]= [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]1 [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]+ [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]temp
        tStr [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]= [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]tStr [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]& [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Clls [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]& [/FONT][/COLOR][COLOR=#dd0000][FONT=Courier New]"@"
    [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]End [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]If
 [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Next
CountUniqueDone [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]= [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]temp
End [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]Function  [/FONT][/COLOR]
 
Bạn thay đổi 1 tí là được
Mã:
[COLOR=#0000bb][FONT=Courier New]Option Explicit
[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]Function [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]CountUniqueDone[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Rng [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]As [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Range[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]) As [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Integer
 Dim Clls [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]As [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Range[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]tStr [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]As [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]String[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]temp [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]As [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Long
 tStr [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]= [/FONT][/COLOR][COLOR=#dd0000][FONT=Courier New]"@"
 [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]For [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Each Clls In Rng
    [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]If [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Clls [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]<> [/FONT][/COLOR][COLOR=#dd0000][FONT=Courier New]"" [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]And [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]InStr[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]1[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]tStr[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#dd0000][FONT=Courier New]"@" [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]& [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Clls [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]& [/FONT][/COLOR][COLOR=#dd0000][FONT=Courier New]"@"[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]) = [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]0 [COLOR=#007700][FONT=Courier New]And [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]InStr[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]([/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]1[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]clls[/FONT][/COLOR][COLOR=#007700][FONT=Courier New], "Done"[/FONT][/COLOR][COLOR=#007700][FONT=Courier New]) > [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]0[/FONT][/COLOR] Then
        temp [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]= [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]1 [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]+ [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]temp
        tStr [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]= [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]tStr [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]& [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Clls [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]& [/FONT][/COLOR][COLOR=#dd0000][FONT=Courier New]"@"
    [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]End [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]If
 [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]Next
CountUniqueDone [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]= [/FONT][/COLOR][COLOR=#0000bb][FONT=Courier New]temp
End [/FONT][/COLOR][COLOR=#007700][FONT=Courier New]Function  [/FONT][/COLOR]
ok. Cảm ơn 2 bác nhé..
 
Web KT

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

Back
Top Bottom