Very Simple Spell (Number to Words) 001 to 999 in VBAConvert number to wordsSimple Number to Words using a Single Loop String Triplets in VBAConvert number to wordsNumber to WordsConvert number into wordsDividing a number half into words and half numberChanging Number to Words in JavaScriptCode to convert number to wordsNumber-to-words translatorFunctional FrameworkConverting number to wordsPython Converting numbers to words, using Recursive

Alternatives to using writing paper for writing practice

Is `curl something | sudo bash -` a reasonably safe installation method?

What is the closed form of the following recursive function?

What exactly is the Tension force?

Commutator subgroup of Heisenberg group.

Ezek. 24:1-2, "Again in the ninth year, in the tenth month, in the tenth day of the month, ...." Which month was the tenth month?

What caused Windows ME's terrible reputation?

What impact would a dragon the size of Asia have on the environment?

What is this old "lemon-squeezer" shaped pan

Project Euler, problem # 9, Pythagorean triplet

Why do they not say "The Baby"

How can I legally visit the United States Minor Outlying Islands in the Pacific?

Concatenation using + and += operator in Python

Can I activate an iPhone without an Apple ID?

Did the Shuttle's rudder or elevons operate when flown on its carrier 747?

Redox reactions redefined

Is this a Lost Mine of Phandelver Plot Hole?

Is it rude to tell recruiters I would only change jobs for a better salary?

I quit, and boss offered me 3 month "grace period" where I could still come back

Are there any double stars that I can actually see orbit each other?

Old short story where the future emperor of the galaxy is taken for a tour around Earth

do not have power to all my breakers

Hot object in a vacuum

Meaning of slash chord without anything left of the slash



Very Simple Spell (Number to Words) 001 to 999 in VBA


Convert number to wordsSimple Number to Words using a Single Loop String Triplets in VBAConvert number to wordsNumber to WordsConvert number into wordsDividing a number half into words and half numberChanging Number to Words in JavaScriptCode to convert number to wordsNumber-to-words translatorFunctional FrameworkConverting number to wordsPython Converting numbers to words, using Recursive






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4












$begingroup$


I have come across many spell number functions and have reviewed many VBA code available on the net.



The core function that gets called repeatedly is the function that converts numbers from 001 to 999 as it is the core function for conversion under the English numeral system.
I have seen that this core function is sometime unnecessarily split over sub-functions for converting one, tens, and 20s to 90s.



I have developed the following simple VBA function that takes an input as number in a string format from "001" to "999" and returns the output as a string.
The function uses the dash "-" for numbers e.g. Forty-Two.



The function is easily convertible to other programming languages.



With your assistance, I am looking to further improve or simplifying the function, if possible.



You may test the function like this:



Debug.Print Do999("123")
Debug.Print Do999("001")
Debug.Print Do999("099")


Thanks in advance for your contribution.



Function Do999(ThreeDigits As String)
'-----------------------------------------
'Converts number string from 001 to 999 to Words
'Uses dash for in-between numbers from 21 to 99 for UK/US English
'Mohsen Alyafei 17 Oct 2018
'On Entry: NumIn MUST be a 3 Chars digit string "001" to "999"
'On Exit : String of number in English words
'-----------------------------------------
Dim Ones(), Tens(), dash As String, h As String, t As String, N1 As Integer, N2 As Integer

Ones = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen")
Tens = Array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety", " Hundred")

'Next line is optional for English speaking words (UK, US)
If Right(ThreeDigits, 1) <> "0" Then dash = "-" 'dash as per English spelling

'------------Code starts here------------
'Get the hundreds (N1) and tens (N2)

N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1))) Else t = Ones(N2)
Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)

End Function









share|improve this question











$endgroup$











  • $begingroup$
    I meant programming languages. Sure languages using the Masculine and Feminine such as French and Arabic will need lots of modifications.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 11:45










  • $begingroup$
    The rules for the conversion are ad-hoc, so fundamental simplification isn't likely. One obvious area of improvement is to extend the functionality so that it applies to more than three digits. A minor improvement would be to make the input Variant so that you could pass it either an integer variable containing a 3-digit number or a string variable and have it work in either case without a type mismatch error.
    $endgroup$
    – John Coleman
    Jul 6 at 12:21











  • $begingroup$
    @JohnColeman Thanks John. This function is a core sub-function that will be called many times from a main function to convert vary large numbers. The main function will take care of the dirty work and will always pass a 3 digit number string for conversion. Integer conversion happens at the higher level function. The higher function will deal with any length of number from 0 to a Decillion.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 12:42

















4












$begingroup$


I have come across many spell number functions and have reviewed many VBA code available on the net.



The core function that gets called repeatedly is the function that converts numbers from 001 to 999 as it is the core function for conversion under the English numeral system.
I have seen that this core function is sometime unnecessarily split over sub-functions for converting one, tens, and 20s to 90s.



I have developed the following simple VBA function that takes an input as number in a string format from "001" to "999" and returns the output as a string.
The function uses the dash "-" for numbers e.g. Forty-Two.



The function is easily convertible to other programming languages.



With your assistance, I am looking to further improve or simplifying the function, if possible.



You may test the function like this:



Debug.Print Do999("123")
Debug.Print Do999("001")
Debug.Print Do999("099")


Thanks in advance for your contribution.



Function Do999(ThreeDigits As String)
'-----------------------------------------
'Converts number string from 001 to 999 to Words
'Uses dash for in-between numbers from 21 to 99 for UK/US English
'Mohsen Alyafei 17 Oct 2018
'On Entry: NumIn MUST be a 3 Chars digit string "001" to "999"
'On Exit : String of number in English words
'-----------------------------------------
Dim Ones(), Tens(), dash As String, h As String, t As String, N1 As Integer, N2 As Integer

Ones = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen")
Tens = Array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety", " Hundred")

'Next line is optional for English speaking words (UK, US)
If Right(ThreeDigits, 1) <> "0" Then dash = "-" 'dash as per English spelling

'------------Code starts here------------
'Get the hundreds (N1) and tens (N2)

N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1))) Else t = Ones(N2)
Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)

End Function









share|improve this question











$endgroup$











  • $begingroup$
    I meant programming languages. Sure languages using the Masculine and Feminine such as French and Arabic will need lots of modifications.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 11:45










  • $begingroup$
    The rules for the conversion are ad-hoc, so fundamental simplification isn't likely. One obvious area of improvement is to extend the functionality so that it applies to more than three digits. A minor improvement would be to make the input Variant so that you could pass it either an integer variable containing a 3-digit number or a string variable and have it work in either case without a type mismatch error.
    $endgroup$
    – John Coleman
    Jul 6 at 12:21











  • $begingroup$
    @JohnColeman Thanks John. This function is a core sub-function that will be called many times from a main function to convert vary large numbers. The main function will take care of the dirty work and will always pass a 3 digit number string for conversion. Integer conversion happens at the higher level function. The higher function will deal with any length of number from 0 to a Decillion.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 12:42













4












4








4


1



$begingroup$


I have come across many spell number functions and have reviewed many VBA code available on the net.



The core function that gets called repeatedly is the function that converts numbers from 001 to 999 as it is the core function for conversion under the English numeral system.
I have seen that this core function is sometime unnecessarily split over sub-functions for converting one, tens, and 20s to 90s.



I have developed the following simple VBA function that takes an input as number in a string format from "001" to "999" and returns the output as a string.
The function uses the dash "-" for numbers e.g. Forty-Two.



The function is easily convertible to other programming languages.



With your assistance, I am looking to further improve or simplifying the function, if possible.



You may test the function like this:



Debug.Print Do999("123")
Debug.Print Do999("001")
Debug.Print Do999("099")


Thanks in advance for your contribution.



Function Do999(ThreeDigits As String)
'-----------------------------------------
'Converts number string from 001 to 999 to Words
'Uses dash for in-between numbers from 21 to 99 for UK/US English
'Mohsen Alyafei 17 Oct 2018
'On Entry: NumIn MUST be a 3 Chars digit string "001" to "999"
'On Exit : String of number in English words
'-----------------------------------------
Dim Ones(), Tens(), dash As String, h As String, t As String, N1 As Integer, N2 As Integer

Ones = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen")
Tens = Array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety", " Hundred")

'Next line is optional for English speaking words (UK, US)
If Right(ThreeDigits, 1) <> "0" Then dash = "-" 'dash as per English spelling

'------------Code starts here------------
'Get the hundreds (N1) and tens (N2)

N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1))) Else t = Ones(N2)
Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)

End Function









share|improve this question











$endgroup$




I have come across many spell number functions and have reviewed many VBA code available on the net.



The core function that gets called repeatedly is the function that converts numbers from 001 to 999 as it is the core function for conversion under the English numeral system.
I have seen that this core function is sometime unnecessarily split over sub-functions for converting one, tens, and 20s to 90s.



I have developed the following simple VBA function that takes an input as number in a string format from "001" to "999" and returns the output as a string.
The function uses the dash "-" for numbers e.g. Forty-Two.



The function is easily convertible to other programming languages.



With your assistance, I am looking to further improve or simplifying the function, if possible.



You may test the function like this:



Debug.Print Do999("123")
Debug.Print Do999("001")
Debug.Print Do999("099")


Thanks in advance for your contribution.



Function Do999(ThreeDigits As String)
'-----------------------------------------
'Converts number string from 001 to 999 to Words
'Uses dash for in-between numbers from 21 to 99 for UK/US English
'Mohsen Alyafei 17 Oct 2018
'On Entry: NumIn MUST be a 3 Chars digit string "001" to "999"
'On Exit : String of number in English words
'-----------------------------------------
Dim Ones(), Tens(), dash As String, h As String, t As String, N1 As Integer, N2 As Integer

Ones = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen")
Tens = Array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety", " Hundred")

'Next line is optional for English speaking words (UK, US)
If Right(ThreeDigits, 1) <> "0" Then dash = "-" 'dash as per English spelling

'------------Code starts here------------
'Get the hundreds (N1) and tens (N2)

N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1))) Else t = Ones(N2)
Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)

End Function






vba numbers-to-words






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 6 at 16:31









200_success

134k21 gold badges171 silver badges441 bronze badges




134k21 gold badges171 silver badges441 bronze badges










asked Jul 6 at 11:28









Mohsen AlyafeiMohsen Alyafei

516 bronze badges




516 bronze badges











  • $begingroup$
    I meant programming languages. Sure languages using the Masculine and Feminine such as French and Arabic will need lots of modifications.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 11:45










  • $begingroup$
    The rules for the conversion are ad-hoc, so fundamental simplification isn't likely. One obvious area of improvement is to extend the functionality so that it applies to more than three digits. A minor improvement would be to make the input Variant so that you could pass it either an integer variable containing a 3-digit number or a string variable and have it work in either case without a type mismatch error.
    $endgroup$
    – John Coleman
    Jul 6 at 12:21











  • $begingroup$
    @JohnColeman Thanks John. This function is a core sub-function that will be called many times from a main function to convert vary large numbers. The main function will take care of the dirty work and will always pass a 3 digit number string for conversion. Integer conversion happens at the higher level function. The higher function will deal with any length of number from 0 to a Decillion.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 12:42
















  • $begingroup$
    I meant programming languages. Sure languages using the Masculine and Feminine such as French and Arabic will need lots of modifications.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 11:45










  • $begingroup$
    The rules for the conversion are ad-hoc, so fundamental simplification isn't likely. One obvious area of improvement is to extend the functionality so that it applies to more than three digits. A minor improvement would be to make the input Variant so that you could pass it either an integer variable containing a 3-digit number or a string variable and have it work in either case without a type mismatch error.
    $endgroup$
    – John Coleman
    Jul 6 at 12:21











  • $begingroup$
    @JohnColeman Thanks John. This function is a core sub-function that will be called many times from a main function to convert vary large numbers. The main function will take care of the dirty work and will always pass a 3 digit number string for conversion. Integer conversion happens at the higher level function. The higher function will deal with any length of number from 0 to a Decillion.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 12:42















$begingroup$
I meant programming languages. Sure languages using the Masculine and Feminine such as French and Arabic will need lots of modifications.
$endgroup$
– Mohsen Alyafei
Jul 6 at 11:45




$begingroup$
I meant programming languages. Sure languages using the Masculine and Feminine such as French and Arabic will need lots of modifications.
$endgroup$
– Mohsen Alyafei
Jul 6 at 11:45












$begingroup$
The rules for the conversion are ad-hoc, so fundamental simplification isn't likely. One obvious area of improvement is to extend the functionality so that it applies to more than three digits. A minor improvement would be to make the input Variant so that you could pass it either an integer variable containing a 3-digit number or a string variable and have it work in either case without a type mismatch error.
$endgroup$
– John Coleman
Jul 6 at 12:21





$begingroup$
The rules for the conversion are ad-hoc, so fundamental simplification isn't likely. One obvious area of improvement is to extend the functionality so that it applies to more than three digits. A minor improvement would be to make the input Variant so that you could pass it either an integer variable containing a 3-digit number or a string variable and have it work in either case without a type mismatch error.
$endgroup$
– John Coleman
Jul 6 at 12:21













$begingroup$
@JohnColeman Thanks John. This function is a core sub-function that will be called many times from a main function to convert vary large numbers. The main function will take care of the dirty work and will always pass a 3 digit number string for conversion. Integer conversion happens at the higher level function. The higher function will deal with any length of number from 0 to a Decillion.
$endgroup$
– Mohsen Alyafei
Jul 6 at 12:42




$begingroup$
@JohnColeman Thanks John. This function is a core sub-function that will be called many times from a main function to convert vary large numbers. The main function will take care of the dirty work and will always pass a 3 digit number string for conversion. Integer conversion happens at the higher level function. The higher function will deal with any length of number from 0 to a Decillion.
$endgroup$
– Mohsen Alyafei
Jul 6 at 12:42










2 Answers
2






active

oldest

votes


















3












$begingroup$

Code Formatting



What certainly could be improved is the indentation of your code for better readability:



Function Do999(ThreeDigits As String)
'Everything after your function header until End Function should be indented
' ...
End Function


Same for further conditional code blocks:



N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then
t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1)))
Else
t = Ones(N2)
End If

Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)


Function naming



Do999 isn't very clear / self descriptive about what the function does.
Convert0UpTo999ToWords might be a better choice for example.



Number output in words




The function uses the dash "-" for numbers e.g. Forty-Two.




That's not how numbers are naturally written in words. Usually 42 would be written as forty-two.



Avoid the 1st letter of the number words to be capitalized.






share|improve this answer











$endgroup$












  • $begingroup$
    Thanks. Please check this "Names of numbers in English" in Wikipedia. Which says: When writing other numbers between 21 and 99, you must use a hyphen (-). 21: twenty-one 29: twenty-nine 64: sixty-four 99: ninety-nine
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 13:29











  • $begingroup$
    @MohsenAlyafei OK, the dash seems to be common according this WP article. The capitalized 1st letters not though. I've reflected that in my answer.
    $endgroup$
    – πάντα ῥεῖ
    Jul 6 at 13:33










  • $begingroup$
    Agree. Capitalised 1st letter is not always common, unless sometimes the number is part of a currency conversion, where some accountants prefer it.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 13:37


















2












$begingroup$

The word "hundred" doesn't belong in the Tens array:



  • It is the only element in the array that starts with a space

  • It is only accessed at one place in the code

  • It is never accessed from the place in the code where the other elements are accessed

Therefore it is better to either:



  • Declare it as a single string variable

  • Just use the string " hundred" literally, directly in the one place where it is needed

Another strange thing is that the dash variable contains a dash in most cases but sometimes also contains nothing at all. In the latter case its variable name dash doesn't accurately describe what the variable contains. Rename it to separator or sep instead.






share|improve this answer









$endgroup$












  • $begingroup$
    Thanks Roland for your observations and advice. The reason I put the string " hundred" in the array too was because it was the only string that oddly appeared as part of the code. The var "dash" could also be named correctly "hyphen".
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 16:10










  • $begingroup$
    It doesn't matter whether it's hyphen or dash. The confusing part is only when it is called that way but contains something entirely different, in this case an empty string.
    $endgroup$
    – Roland Illig
    Jul 6 at 18:40










  • $begingroup$
    True. It contains an empty string "". The Empty String is a valid element in concatenation operation. It is a valid string data type with zero length. It simplifies coding and exists in all programming languages. What code alternative do you suggest? Thanks
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 19:05










  • $begingroup$
    The empty string is great. It just shouldn't be stored in a variable whose name is dash or hyphen, since then the name of the variable does not match what the variable contains.
    $endgroup$
    – Roland Illig
    Jul 6 at 19:41










  • $begingroup$
    I did in fact change the var name to "Separator" to avoid confusion as suggested by another commentor above. Please see below updates.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 19:51













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f223615%2fvery-simple-spell-number-to-words-001-to-999-in-vba%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3












$begingroup$

Code Formatting



What certainly could be improved is the indentation of your code for better readability:



Function Do999(ThreeDigits As String)
'Everything after your function header until End Function should be indented
' ...
End Function


Same for further conditional code blocks:



N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then
t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1)))
Else
t = Ones(N2)
End If

Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)


Function naming



Do999 isn't very clear / self descriptive about what the function does.
Convert0UpTo999ToWords might be a better choice for example.



Number output in words




The function uses the dash "-" for numbers e.g. Forty-Two.




That's not how numbers are naturally written in words. Usually 42 would be written as forty-two.



Avoid the 1st letter of the number words to be capitalized.






share|improve this answer











$endgroup$












  • $begingroup$
    Thanks. Please check this "Names of numbers in English" in Wikipedia. Which says: When writing other numbers between 21 and 99, you must use a hyphen (-). 21: twenty-one 29: twenty-nine 64: sixty-four 99: ninety-nine
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 13:29











  • $begingroup$
    @MohsenAlyafei OK, the dash seems to be common according this WP article. The capitalized 1st letters not though. I've reflected that in my answer.
    $endgroup$
    – πάντα ῥεῖ
    Jul 6 at 13:33










  • $begingroup$
    Agree. Capitalised 1st letter is not always common, unless sometimes the number is part of a currency conversion, where some accountants prefer it.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 13:37















3












$begingroup$

Code Formatting



What certainly could be improved is the indentation of your code for better readability:



Function Do999(ThreeDigits As String)
'Everything after your function header until End Function should be indented
' ...
End Function


Same for further conditional code blocks:



N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then
t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1)))
Else
t = Ones(N2)
End If

Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)


Function naming



Do999 isn't very clear / self descriptive about what the function does.
Convert0UpTo999ToWords might be a better choice for example.



Number output in words




The function uses the dash "-" for numbers e.g. Forty-Two.




That's not how numbers are naturally written in words. Usually 42 would be written as forty-two.



Avoid the 1st letter of the number words to be capitalized.






share|improve this answer











$endgroup$












  • $begingroup$
    Thanks. Please check this "Names of numbers in English" in Wikipedia. Which says: When writing other numbers between 21 and 99, you must use a hyphen (-). 21: twenty-one 29: twenty-nine 64: sixty-four 99: ninety-nine
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 13:29











  • $begingroup$
    @MohsenAlyafei OK, the dash seems to be common according this WP article. The capitalized 1st letters not though. I've reflected that in my answer.
    $endgroup$
    – πάντα ῥεῖ
    Jul 6 at 13:33










  • $begingroup$
    Agree. Capitalised 1st letter is not always common, unless sometimes the number is part of a currency conversion, where some accountants prefer it.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 13:37













3












3








3





$begingroup$

Code Formatting



What certainly could be improved is the indentation of your code for better readability:



Function Do999(ThreeDigits As String)
'Everything after your function header until End Function should be indented
' ...
End Function


Same for further conditional code blocks:



N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then
t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1)))
Else
t = Ones(N2)
End If

Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)


Function naming



Do999 isn't very clear / self descriptive about what the function does.
Convert0UpTo999ToWords might be a better choice for example.



Number output in words




The function uses the dash "-" for numbers e.g. Forty-Two.




That's not how numbers are naturally written in words. Usually 42 would be written as forty-two.



Avoid the 1st letter of the number words to be capitalized.






share|improve this answer











$endgroup$



Code Formatting



What certainly could be improved is the indentation of your code for better readability:



Function Do999(ThreeDigits As String)
'Everything after your function header until End Function should be indented
' ...
End Function


Same for further conditional code blocks:



N1 = Left(ThreeDigits, 1): N2 = Right(ThreeDigits, 2)
If N2 > 19 Then
t = Tens(Val(Mid(ThreeDigits, 2, 1))) & dash & Ones(Val(Right(ThreeDigits, 1)))
Else
t = Ones(N2)
End If

Do999 = Trim(IIf(N1 > 0, Ones(N1) & Tens(10), "") & " " & t)


Function naming



Do999 isn't very clear / self descriptive about what the function does.
Convert0UpTo999ToWords might be a better choice for example.



Number output in words




The function uses the dash "-" for numbers e.g. Forty-Two.




That's not how numbers are naturally written in words. Usually 42 would be written as forty-two.



Avoid the 1st letter of the number words to be capitalized.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jul 7 at 9:44









Vogel612

22.1k5 gold badges48 silver badges131 bronze badges




22.1k5 gold badges48 silver badges131 bronze badges










answered Jul 6 at 12:59









πάντα ῥεῖπάντα ῥεῖ

4,0293 gold badges14 silver badges28 bronze badges




4,0293 gold badges14 silver badges28 bronze badges











  • $begingroup$
    Thanks. Please check this "Names of numbers in English" in Wikipedia. Which says: When writing other numbers between 21 and 99, you must use a hyphen (-). 21: twenty-one 29: twenty-nine 64: sixty-four 99: ninety-nine
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 13:29











  • $begingroup$
    @MohsenAlyafei OK, the dash seems to be common according this WP article. The capitalized 1st letters not though. I've reflected that in my answer.
    $endgroup$
    – πάντα ῥεῖ
    Jul 6 at 13:33










  • $begingroup$
    Agree. Capitalised 1st letter is not always common, unless sometimes the number is part of a currency conversion, where some accountants prefer it.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 13:37
















  • $begingroup$
    Thanks. Please check this "Names of numbers in English" in Wikipedia. Which says: When writing other numbers between 21 and 99, you must use a hyphen (-). 21: twenty-one 29: twenty-nine 64: sixty-four 99: ninety-nine
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 13:29











  • $begingroup$
    @MohsenAlyafei OK, the dash seems to be common according this WP article. The capitalized 1st letters not though. I've reflected that in my answer.
    $endgroup$
    – πάντα ῥεῖ
    Jul 6 at 13:33










  • $begingroup$
    Agree. Capitalised 1st letter is not always common, unless sometimes the number is part of a currency conversion, where some accountants prefer it.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 13:37















$begingroup$
Thanks. Please check this "Names of numbers in English" in Wikipedia. Which says: When writing other numbers between 21 and 99, you must use a hyphen (-). 21: twenty-one 29: twenty-nine 64: sixty-four 99: ninety-nine
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:29





$begingroup$
Thanks. Please check this "Names of numbers in English" in Wikipedia. Which says: When writing other numbers between 21 and 99, you must use a hyphen (-). 21: twenty-one 29: twenty-nine 64: sixty-four 99: ninety-nine
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:29













$begingroup$
@MohsenAlyafei OK, the dash seems to be common according this WP article. The capitalized 1st letters not though. I've reflected that in my answer.
$endgroup$
– πάντα ῥεῖ
Jul 6 at 13:33




$begingroup$
@MohsenAlyafei OK, the dash seems to be common according this WP article. The capitalized 1st letters not though. I've reflected that in my answer.
$endgroup$
– πάντα ῥεῖ
Jul 6 at 13:33












$begingroup$
Agree. Capitalised 1st letter is not always common, unless sometimes the number is part of a currency conversion, where some accountants prefer it.
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:37




$begingroup$
Agree. Capitalised 1st letter is not always common, unless sometimes the number is part of a currency conversion, where some accountants prefer it.
$endgroup$
– Mohsen Alyafei
Jul 6 at 13:37













2












$begingroup$

The word "hundred" doesn't belong in the Tens array:



  • It is the only element in the array that starts with a space

  • It is only accessed at one place in the code

  • It is never accessed from the place in the code where the other elements are accessed

Therefore it is better to either:



  • Declare it as a single string variable

  • Just use the string " hundred" literally, directly in the one place where it is needed

Another strange thing is that the dash variable contains a dash in most cases but sometimes also contains nothing at all. In the latter case its variable name dash doesn't accurately describe what the variable contains. Rename it to separator or sep instead.






share|improve this answer









$endgroup$












  • $begingroup$
    Thanks Roland for your observations and advice. The reason I put the string " hundred" in the array too was because it was the only string that oddly appeared as part of the code. The var "dash" could also be named correctly "hyphen".
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 16:10










  • $begingroup$
    It doesn't matter whether it's hyphen or dash. The confusing part is only when it is called that way but contains something entirely different, in this case an empty string.
    $endgroup$
    – Roland Illig
    Jul 6 at 18:40










  • $begingroup$
    True. It contains an empty string "". The Empty String is a valid element in concatenation operation. It is a valid string data type with zero length. It simplifies coding and exists in all programming languages. What code alternative do you suggest? Thanks
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 19:05










  • $begingroup$
    The empty string is great. It just shouldn't be stored in a variable whose name is dash or hyphen, since then the name of the variable does not match what the variable contains.
    $endgroup$
    – Roland Illig
    Jul 6 at 19:41










  • $begingroup$
    I did in fact change the var name to "Separator" to avoid confusion as suggested by another commentor above. Please see below updates.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 19:51















2












$begingroup$

The word "hundred" doesn't belong in the Tens array:



  • It is the only element in the array that starts with a space

  • It is only accessed at one place in the code

  • It is never accessed from the place in the code where the other elements are accessed

Therefore it is better to either:



  • Declare it as a single string variable

  • Just use the string " hundred" literally, directly in the one place where it is needed

Another strange thing is that the dash variable contains a dash in most cases but sometimes also contains nothing at all. In the latter case its variable name dash doesn't accurately describe what the variable contains. Rename it to separator or sep instead.






share|improve this answer









$endgroup$












  • $begingroup$
    Thanks Roland for your observations and advice. The reason I put the string " hundred" in the array too was because it was the only string that oddly appeared as part of the code. The var "dash" could also be named correctly "hyphen".
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 16:10










  • $begingroup$
    It doesn't matter whether it's hyphen or dash. The confusing part is only when it is called that way but contains something entirely different, in this case an empty string.
    $endgroup$
    – Roland Illig
    Jul 6 at 18:40










  • $begingroup$
    True. It contains an empty string "". The Empty String is a valid element in concatenation operation. It is a valid string data type with zero length. It simplifies coding and exists in all programming languages. What code alternative do you suggest? Thanks
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 19:05










  • $begingroup$
    The empty string is great. It just shouldn't be stored in a variable whose name is dash or hyphen, since then the name of the variable does not match what the variable contains.
    $endgroup$
    – Roland Illig
    Jul 6 at 19:41










  • $begingroup$
    I did in fact change the var name to "Separator" to avoid confusion as suggested by another commentor above. Please see below updates.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 19:51













2












2








2





$begingroup$

The word "hundred" doesn't belong in the Tens array:



  • It is the only element in the array that starts with a space

  • It is only accessed at one place in the code

  • It is never accessed from the place in the code where the other elements are accessed

Therefore it is better to either:



  • Declare it as a single string variable

  • Just use the string " hundred" literally, directly in the one place where it is needed

Another strange thing is that the dash variable contains a dash in most cases but sometimes also contains nothing at all. In the latter case its variable name dash doesn't accurately describe what the variable contains. Rename it to separator or sep instead.






share|improve this answer









$endgroup$



The word "hundred" doesn't belong in the Tens array:



  • It is the only element in the array that starts with a space

  • It is only accessed at one place in the code

  • It is never accessed from the place in the code where the other elements are accessed

Therefore it is better to either:



  • Declare it as a single string variable

  • Just use the string " hundred" literally, directly in the one place where it is needed

Another strange thing is that the dash variable contains a dash in most cases but sometimes also contains nothing at all. In the latter case its variable name dash doesn't accurately describe what the variable contains. Rename it to separator or sep instead.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jul 6 at 15:18









Roland IlligRoland Illig

14.1k1 gold badge23 silver badges56 bronze badges




14.1k1 gold badge23 silver badges56 bronze badges











  • $begingroup$
    Thanks Roland for your observations and advice. The reason I put the string " hundred" in the array too was because it was the only string that oddly appeared as part of the code. The var "dash" could also be named correctly "hyphen".
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 16:10










  • $begingroup$
    It doesn't matter whether it's hyphen or dash. The confusing part is only when it is called that way but contains something entirely different, in this case an empty string.
    $endgroup$
    – Roland Illig
    Jul 6 at 18:40










  • $begingroup$
    True. It contains an empty string "". The Empty String is a valid element in concatenation operation. It is a valid string data type with zero length. It simplifies coding and exists in all programming languages. What code alternative do you suggest? Thanks
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 19:05










  • $begingroup$
    The empty string is great. It just shouldn't be stored in a variable whose name is dash or hyphen, since then the name of the variable does not match what the variable contains.
    $endgroup$
    – Roland Illig
    Jul 6 at 19:41










  • $begingroup$
    I did in fact change the var name to "Separator" to avoid confusion as suggested by another commentor above. Please see below updates.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 19:51
















  • $begingroup$
    Thanks Roland for your observations and advice. The reason I put the string " hundred" in the array too was because it was the only string that oddly appeared as part of the code. The var "dash" could also be named correctly "hyphen".
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 16:10










  • $begingroup$
    It doesn't matter whether it's hyphen or dash. The confusing part is only when it is called that way but contains something entirely different, in this case an empty string.
    $endgroup$
    – Roland Illig
    Jul 6 at 18:40










  • $begingroup$
    True. It contains an empty string "". The Empty String is a valid element in concatenation operation. It is a valid string data type with zero length. It simplifies coding and exists in all programming languages. What code alternative do you suggest? Thanks
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 19:05










  • $begingroup$
    The empty string is great. It just shouldn't be stored in a variable whose name is dash or hyphen, since then the name of the variable does not match what the variable contains.
    $endgroup$
    – Roland Illig
    Jul 6 at 19:41










  • $begingroup$
    I did in fact change the var name to "Separator" to avoid confusion as suggested by another commentor above. Please see below updates.
    $endgroup$
    – Mohsen Alyafei
    Jul 6 at 19:51















$begingroup$
Thanks Roland for your observations and advice. The reason I put the string " hundred" in the array too was because it was the only string that oddly appeared as part of the code. The var "dash" could also be named correctly "hyphen".
$endgroup$
– Mohsen Alyafei
Jul 6 at 16:10




$begingroup$
Thanks Roland for your observations and advice. The reason I put the string " hundred" in the array too was because it was the only string that oddly appeared as part of the code. The var "dash" could also be named correctly "hyphen".
$endgroup$
– Mohsen Alyafei
Jul 6 at 16:10












$begingroup$
It doesn't matter whether it's hyphen or dash. The confusing part is only when it is called that way but contains something entirely different, in this case an empty string.
$endgroup$
– Roland Illig
Jul 6 at 18:40




$begingroup$
It doesn't matter whether it's hyphen or dash. The confusing part is only when it is called that way but contains something entirely different, in this case an empty string.
$endgroup$
– Roland Illig
Jul 6 at 18:40












$begingroup$
True. It contains an empty string "". The Empty String is a valid element in concatenation operation. It is a valid string data type with zero length. It simplifies coding and exists in all programming languages. What code alternative do you suggest? Thanks
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:05




$begingroup$
True. It contains an empty string "". The Empty String is a valid element in concatenation operation. It is a valid string data type with zero length. It simplifies coding and exists in all programming languages. What code alternative do you suggest? Thanks
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:05












$begingroup$
The empty string is great. It just shouldn't be stored in a variable whose name is dash or hyphen, since then the name of the variable does not match what the variable contains.
$endgroup$
– Roland Illig
Jul 6 at 19:41




$begingroup$
The empty string is great. It just shouldn't be stored in a variable whose name is dash or hyphen, since then the name of the variable does not match what the variable contains.
$endgroup$
– Roland Illig
Jul 6 at 19:41












$begingroup$
I did in fact change the var name to "Separator" to avoid confusion as suggested by another commentor above. Please see below updates.
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:51




$begingroup$
I did in fact change the var name to "Separator" to avoid confusion as suggested by another commentor above. Please see below updates.
$endgroup$
– Mohsen Alyafei
Jul 6 at 19:51

















draft saved

draft discarded
















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f223615%2fvery-simple-spell-number-to-words-001-to-999-in-vba%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form