A Simple Solution for Pluralizing Strings

Reader Patrick Jackman of Vancouver, BC has submitted a useful “fit-and-finish” tip.

Here is a simple tip that I use quite often in MsgBox prompts when the message involves reporting the results of a count.

Function Pluralize(ByVal varCount As Variant, Optional Singular _
                    As Variant, Optional Plural As Variant) _
                    As String

    If Not IsNull(varCount) Then
        If varCount <> 1 Then
            If IsMissing(Plural) Then
                Pluralize = "s"
            Else
                Pluralize = Plural
            End If
        Else
            If Not IsMissing(Singular) Then
                Pluralize = Singular
            End If
        End If
    End If
End Function

Here is an example of how I would use it without the optional parameters:

strMsg = "Your search returned " & intCount & " item" & _
            Pluralize(intCount) & "."

When intCount is 1, strMsg would be:

Your search returned 1 item.

When intCount is 2, strMsg would be:

Your search returned 2 items.

Here is an example using the optional parameters:

strMsg = "Print " & intCount & " cop" & _
            Pluralize(intCount, "y", "ies") & "?"

When intCount is 1, strMsg would be:

Print 1 copy?

When intCount is 2, strMsg would be:

Print 2 copies?

For more information, you can contact Patrick here.

Office Blogs Comments

Comments: (7) Collapse

  • Very easy in English, much more difficult in Czech.

  • I suppose thats great until you return deers, gooses, and mouses.

  • Pluralize is not required when reporting deer counts. Use the optional parameters to avoid gooses and mouses:

    strMsg = intCount & " blind " & Pluralize(intCount, "mouse", "mice")

  • Another way, perhaps using fewer lines of code: iif(intCount=1,"y","ies")

  • Nice, but: 1. You can streamline the code by setting default values for the optional parameters, which eliminates the If/Then test and use of the IsMissing function: Function Pluralize(ByVal intCount As Long, Optional strSingular As String= "", Optional strPlural As String= "s") As String 2. The code can be made more generic by passing the full strings for strSingular and strPlural to the function and having it return the full message string. 3. The line: "If varCount <> 1 Then" could generate a run-time error if zero or a negative value is passed to the function.

  • Thanks for your suggestion Peter. Would you prefer to replace the Function's logic or replace the function call?

  • Tony, I like your default parameter suggestion although it would limit this Tip to Access versions having that feature. Your second suggestion to return the full message could be more complicated:

    strMsg = intPosted & Pluralize(intItems, " item", " items") & " in this batch " & Pluralize(intPosted, "was", "were") & " posted". When intCount = 0, the Plural condition returns correct English: strMsg = intCount & Pluralize(intCount, " row ", " rows ") & "returned." A negative intCount value does not cause a run-time error. A value of -1 returns an incorrect message for degree(s) Celsius but I would consider -1 a logic error when reporting count results.

Comments

Comments: (loading) Collapse