Excel nema zadanu funkciju koja prikazuje brojeve kao engleske riječi na radnom listu, ali tu mogućnost možete dodati lijepljenjem sljedećeg koda funkcije SpellNumber u modul VBA (Visual Basic for Applications). Ova funkcija omogućuje pretvaranje dolara i centa u riječi s formulom, pa bi se 22,50 čitalo kao Twenty-Two dolara i pedeset centi. To može biti vrlo korisno ako koristite Excel kao predložak za ispis provjera.
Ako želite pretvoriti numeričke vrijednosti u tekstni oblik, a da ih ne prikazujete kao riječi, koristite funkciju TEXT .
Napomena: Microsoft nudi primjere programiranja samo za ilustraciju, bez jamstva izraženog ili impliciranog. To uključuje, ali nije ograničeno na, implicirana jamstva prikladnosti ili prikladnosti za određenu svrhu. U ovom se članku pretpostavlja da ste upoznati s VBA programskim jezikom i alatima koji se koriste za stvaranje i ispravljanje pogrešaka. Inženjeri Microsoftove podrške mogu objasniti funkcionalnost određenog postupka. Međutim, oni neće izmijeniti te primjere da bi pružili dodatne funkcije ili sastavljali postupke za zadovoljavanje vaših posebnih zahtjeva.
Stvaranje funkcije SpellNumber radi pretvaranja brojeva u riječi
-
Pomoću tipkovnog prečaca Alt + F11 otvorite Visual Basic Editor (VBE).
Napomena: Programu Visual Basic Editor možete pristupiti i tako da na vrpci prikazujete karticu Programiranje.
-
Kliknite karticu Umetanje , a zatim Modul.
-
Kopirajte sljedeće retke koda.
Napomena: Poznat kao korisnički definirana funkcija (UDF), ovaj kod automatizira zadatak pretvaranja brojeva u tekst na cijelom radnom listu.
Option Explicit 'Main Function Function SpellNumber(ByVal MyNumber) Dim Dollars, Cents, Temp Dim DecimalPlace, Count ReDim Place(9) As String Place(2) = " Thousand " Place(3) = " Million " Place(4) = " Billion " Place(5) = " Trillion " ' String representation of amount. MyNumber = Trim(Str(MyNumber)) ' Position of decimal place 0 if none. DecimalPlace = InStr(MyNumber, ".") ' Convert cents and set MyNumber to dollar amount. If DecimalPlace > 0 Then Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & _ "00", 2)) MyNumber = Trim(Left(MyNumber, DecimalPlace - 1)) End If Count = 1 Do While MyNumber <> "" Temp = GetHundreds(Right(MyNumber, 3)) If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars If Len(MyNumber) > 3 Then MyNumber = Left(MyNumber, Len(MyNumber) - 3) Else MyNumber = "" End If Count = Count + 1 Loop Select Case Dollars Case "" Dollars = "No Dollars" Case "One" Dollars = "One Dollar" Case Else Dollars = Dollars & " Dollars" End Select Select Case Cents Case "" Cents = " and No Cents" Case "One" Cents = " and One Cent" Case Else Cents = " and " & Cents & " Cents" End Select SpellNumber = Dollars & Cents End Function ' Converts a number from 100-999 into text Function GetHundreds(ByVal MyNumber) Dim Result As String If Val(MyNumber) = 0 Then Exit Function MyNumber = Right("000" & MyNumber, 3) ' Convert the hundreds place. If Mid(MyNumber, 1, 1) <> "0" Then Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred " End If ' Convert the tens and ones place. If Mid(MyNumber, 2, 1) <> "0" Then Result = Result & GetTens(Mid(MyNumber, 2)) Else Result = Result & GetDigit(Mid(MyNumber, 3)) End If GetHundreds = Result End Function ' Converts a number from 10 to 99 into text. Function GetTens(TensText) Dim Result As String Result = "" ' Null out the temporary function value. If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19... Select Case Val(TensText) Case 10: Result = "Ten" Case 11: Result = "Eleven" Case 12: Result = "Twelve" Case 13: Result = "Thirteen" Case 14: Result = "Fourteen" Case 15: Result = "Fifteen" Case 16: Result = "Sixteen" Case 17: Result = "Seventeen" Case 18: Result = "Eighteen" Case 19: Result = "Nineteen" Case Else End Select Else ' If value between 20-99... Select Case Val(Left(TensText, 1)) Case 2: Result = "Twenty " Case 3: Result = "Thirty " Case 4: Result = "Forty " Case 5: Result = "Fifty " Case 6: Result = "Sixty " Case 7: Result = "Seventy " Case 8: Result = "Eighty " Case 9: Result = "Ninety " Case Else End Select Result = Result & GetDigit _ (Right(TensText, 1)) ' Retrieve ones place. End If GetTens = Result End Function ' Converts a number from 1 to 9 into text. Function GetDigit(Digit) Select Case Val(Digit) Case 1: GetDigit = "One" Case 2: GetDigit = "Two" Case 3: GetDigit = "Three" Case 4: GetDigit = "Four" Case 5: GetDigit = "Five" Case 6: GetDigit = "Six" Case 7: GetDigit = "Seven" Case 8: GetDigit = "Eight" Case 9: GetDigit = "Nine" Case Else: GetDigit = "" End Select End Function
-
Zalijepite retke koda u okvir Modul1 ( kod).
-
Pritisnite Alt + Q da biste se vratili u Excel. Funkcija SpellNumber sada je spremna za korištenje.
Napomena: Ta funkcija funkcionira samo za trenutnu radnu knjigu. Da biste tu funkciju koristili u drugoj radnoj knjizi, morate ponoviti korake za kopiranje i lijepljenje koda u tu radnu knjigu.
Korištenje funkcije SpellNumber u pojedinačnim ćelijama
-
U ćeliju u koju želite prikazati pisani broj upišite formulu =SpellNumber(A1), pri čemu je A1 ćelija koja sadrži broj koji želite pretvoriti. Možete i ručno upisati vrijednost kao što je =SpellNumber(22,50).
-
Pritisnite Enter da biste potvrdili formulu.
Spremanje radne knjige funkcije SpellNumber
Excel ne može spremiti radnu knjigu s funkcijama makronaredbi u standardnom obliku radne knjige bez makronaredbi (.xlsx). Ako kliknete Datoteka >Spremi. Otvorit će se dijaloški okvir VB projekta. Kliknite Ne.
Datoteku možete spremiti kao radnu knjigu programa Excel Macro-Enabled (.xlsm) da biste datoteku zadržali u trenutnom obliku.
-
Kliknite Datoteka pa Spremi kao.
-
Kliknite padajući izbornik Spremi u obliku, a zatim odaberite Excel Macro-Enabled radnu knjigu.
-
Kliknite Spremi.