Nested IFs to pad numbers with zeroes [duplicate]

不羁岁月 提交于 2019-12-11 09:37:28

问题


I am looking to pad a columns with zero's so that I can later concatenate it's value. It needs to 4 characters, so I am trying to use a Nested If Statement.

=IF(LEN(G2)=3,"0" & G2),IF(LEN(G2)=2,"00" & G2,G2)

I only get '#Value'. Where am I going wrong?


回答1:


Don't reinvent the wheel: there is already a built-in function do to this.

=TEXT(G2,"0000")

Now, to answer your question specifically, the reason you got your error was that your IF functions weren't properly nested due to misplaced right brackets ).

Was:

=IF(LEN(G2)=3,"0" & G2),IF(LEN(G2)=2,"00" & G2,G2)
                      ^

Should be:

=IF(LEN(G2)=3,"0" & G2,IF(LEN(G2)=2,"00" & G2,G2))
                                                 ^

Still, this won't give you the expected results. Just go with the built-in TEXT function.




回答2:


Don't need nested IF's. Just concatenate 4 "0"s with your text, and then rip off the right 4 digits.

=RIGHT(CONCATENATE(REPT("0",4), G2),4)


来源:https://stackoverflow.com/questions/28943527/nested-ifs-to-pad-numbers-with-zeroes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!