Best way to handle singular/plural in messages

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,525
Office Version
  1. 365
Platform
  1. Windows
Is there a really good way to generate messages in VBA that are grammatically correct with regard to singular and plural nouns with variable numbers?

I have a UDF that calculates how many wins a team needs to move into first place. I'd like messages like these:
  • Just 3 wins needed
  • Just 2 wins needed
  • Just 1 win needed
I know I can do it with an IF statement, but then I have two places to edit if I change the message. What I have been doing is:
  • Wins needed: 3
  • Wins needed: 2
  • Wins needed: 1
That sorta works, but I'd prefer the first set if there is an easy way to achieve it.

Thanks
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
You just need to remove s which can be handled with single line IF statement. I'm not sure if you need to edit two places with this simple demo.

VBA Code:
Sub test()

strwin = " wins"
x = Range("A1")

If x = 1 Then strwin = Left(strwin, Len(strwin) - 1)
Statement = "Just " & x & strwin & " needed"
Range("C1") = Statement

End Sub
 
Upvote 0
You just need to remove s which can be handled with single line IF statement. I'm not sure if you need to edit two places with this simple demo.

VBA Code:
Sub test()

strwin = " wins"
x = Range("A1")

If x = 1 Then strwin = Left(strwin, Len(strwin) - 1)
Statement = "Just " & x &strwin & " needed"
Range("C1") = Statement

End Sub
Thanks.

And here's another solution that does not involve an If statement:
VBA Code:
wins=0
?"win" & left("s",1+(wins=1))
wins

wins=1
?"win" & left("s",1+(wins=1))
win

wins=2
?"win" & left("s",1+(wins=1))
wins

wins=10
?"win" & left("s",1+(wins=1))
wins

It can be used inline or in a separate line. :)
 
Upvote 0
You could use Iif.
VBA Code:
wins = 1
msg = "Just " & wins & " win" & IIf(wins > 1, "s", "") & " needed."
Debug.Print (msg)

wins = 2
msg = "Just " & wins & " win" & IIf(wins > 1, "s", "") & " needed."
Debug.Print (msg)
 
Upvote 0
Solution
You could use Iif.
VBA Code:
wins = 1
msg = "Just " & wins & " win" & IIf(wins > 1, "s", "") & " needed."
Debug.Print (msg)

wins = 2
msg = "Just " & wins & " win" & IIf(wins > 1, "s", "") & " needed."
Debug.Print (msg)
Very nice! (y) (y)

I like it better than my solution.

Is the first "I" for "Insert" -- Insert If?
 
Upvote 0

Forum statistics

Threads
1,214,431
Messages
6,119,462
Members
448,899
Latest member
maplemeadows

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top