简体   繁体   中英

Getting a Message box With Count-if to work

Im 'trying' to make a message box that display how many present and absents there are in a column so the user can either click ok and the data being copied or pressing cancel and ending the code.

The problem is the i cant seem to get the CountIf part to work where it counts the number of absents and presents before displaying them in the Message Box.

Im Pretty new to coding so its probably a real mess but any help and id be grateful:)

Sub SubmitAttendance()

    Dim Response As String
    Dim Question As String
    Dim PresentNumber As String
    Dim AbsentNumber As String


    Function As Integer
        PresentNumber = Countif(Range("E:E"), Present)
        AbsentNumber = Countif(Range("E:E"), Absent)
    End Function


    Question = "PresentNumber Present and AbsentNumber Absent"
    Response = MsgBox(Question, vbOKCancel, "Register Totals")

    If Response = vbOK Then
        Range("E:E").Select
        Selection.Copy

        Range("F:ZZ").Find("").Select
        Selection.PasteSpecial
    Else
        Exit Function
    End If
End Sub

Excel functions are available in WorkSheetFunctions module, like:

WorksheetFunctions.CountIf(Arg1 As Range, Arg2)

Also, pass your second argument as string, not as a variable (VB's hectic nature will declare an uninitialized variable of that name for you, which is probably not what you want.

So your CountIf line should be like:

WorksheetFunction.CountIf( Range("E:E") , "Present")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM