资源描述:
《数字转换文字.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、此文章已于23:50:132016/3/12发布到醉资源博客数字转换为文字帐户醉资源博客Ifyou’veeverneededtodisplayanumberwrittenoutastext,youprobablydiscoveredthatExceldoesnotoffersuchafunction.WhenExcelfailstodeliver,it’softenpossibletocorrectthedeficiencybyusingVBA.Here’saVBAfunction,namedSPELLDOLLARS,thatyoucanuseinworksheetformulas.Excel
2、examplesHerearesomeexamplesofSPELLDOLLARS.TheVBAcodeTousethiscode,pressAlt+F11toactivateVisualBasicEditor.ThenchooseInsert→ModuletoinsertanewVBAmodule.Copythecodeandpasteitintothenewmodule.FunctionSPELLDOLLARS(cell)AsVariant‘Returnsavalue,spelledoutinwordsDimDollarsAsString,CentsAsStringDimTextLenAs
3、Long,PosAsLongDimTempAsStringDimiHundredsAsLong,iTensAsLong,iOnesAsLongDimOnesAsVariant,TeensAsVariant,TensAsVariantDimUnits(2To5)AsStringDimbHitAsBoolean,NegFlagAsBoolean‘Isitanon-numberoremptycell?IfNotIsNumeric(cell)Orcell=““ThenSPELLDOLLARS=CVErr(xlErrValue)ExitFunctionEndIf‘Isitnegative?Ifcell<
4、0ThenNegFlag=Truecell=Abs(cell)EndIfDollars=Format(cell,“###0.00”)TextLen=Len(Dollars)-3‘Isittoolarge?IfTextLen>15ThenSPELLDOLLARS=CVErr(xlErrNum)ExitFunctionEndIf‘DothecentspartCents=Right(Dollars,2)&“/100Dollars”Ifcell<1ThenSPELLDOLLARS=CentsExitFunctionEndIfDollars=Left(Dollars,TextLen)Ones=Array
5、(““,“One”,“Two”,“Three”,“Four”,_“Five”,“Six”,“Seven”,“Eight”,“Nine”)Teens=Array(“Ten”,“Eleven”,“Twelve”,“Thirteen”,“Fourteen”,_“Fifteen”,“Sixteen”,“Seventeen”,“Eighteen”,“Nineteen”)Tens=Array(““,““,“Twenty”,“Thirty”,“Forty”,“Fifty”,_“Sixty”,“Seventy”,“Eighty”,“Ninety”)Units(2)=“Thousand”Units(3)=“Mi
6、llion”Units(4)=“Billion”Units(5)=“Trillion”Temp=““ForPos=15To3Step-3IfTextLen>=Pos-2ThenbHit=FalseIfTextLen>=PosTheniHundreds=Asc(Mid$(Dollars,TextLen-Pos+1,1))-48IfiHundreds>0ThenTemp=Temp&““&Ones(iHundreds)&“Hundred”bHit=TrueEndIfEndIfiTens=0iOnes=0IfTextLen>=Pos-1TheniTens=Asc(Mid$(Dollars,TextLe
7、n-Pos+2,1))-48EndIfIfTextLen>=Pos-2TheniOnes=Asc(Mid$(Dollars,TextLen-Pos+3,1))-48EndIfIfiTens=1ThenTemp=Temp&““&Teens(iOnes)bHit=TrueElseIfiTens>=2ThenTemp=Temp&““&Tens(iTens)bHit=TrueEndIfIfiOnes>0T