Excel 数字格式。将我的单元格视为数字,但保留其格式! (即尾随 0)
我有(看起来应该是)一个简单的问题。我需要能够告诉 Excel(在 vba 中)单元格的内容是数字,但我真的不想对其应用任何格式。我需要保留我的尾随零。我错过了一些非常简单的事情吗?
更新:
我从查询中获取 xml 数据并将其放入电子表格中。但是假设其中一个单元格有 589.950000
我需要为用户保留这些额外的零(不要问我为什么,他们只是想要精度),但 excel 将其转换为 589.95。我还需要他们能够在该单元格上进行电子表格计算(所以我无法将其格式化为文本)。
更新 2:
进一步澄清。假设我有 4 个值要放入一列中,
595.56000
15.00
90.00050
1919.120000000
是否有一种数字格式可以应用于该列来显示这些确切的数字?
I have (what seems like it should be) a simple problem. I need to be able to tell excel (in vba) that a cell's contents are numeric, but I don't really want to apply any formatting to it. I need my trailing zeros left how they are. Am I missing something incredibly simple?
Update:
I'm getting xml data from a query and am placing it into the spreadsheets. But lets say one of the cells has 589.950000
I need to keep those additional zeros on display for the user (don't ask me why, they just want the precision) but excel converts it to 589.95
. I also need for them to be able to do spreadsheet calculations on that cell (so I can't format it as text).
Update 2:
Further Clarification. Lets say I have 4 values that I'm going to place into a column
595.56000
15.00
90.00050
1919.120000000
is there one numeric format that I can apply to that column that will display those exact numbers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为你可以这样做:
这会将文本评估为数字
I think you can do:
Which will evalulate the text to a number
您的意思是您想将单元格的值用作数字值,但不想更改单元格的实际值?
在vba中,您可以使用
CInt(yourvar)
将字符串转换为数字。因此,您得到的值肯定是一个整数。[编辑]顺便说一句,如果您想将值显示或设置回具有所需格式的单元格,您可以使用
Format ( expression, [ format ] )
Excel vba 的函数[编辑2]
由于您无法预测将有多少个零,因此我看不到任何有效的数字格式。您可能最好按照兰斯·罗伯特的建议使用文本格式。要在文本单元格上使用公式,您可以使用
VALUE
公式将单元格转换为数字。如果您需要在某个范围内使用它,则可能必须使用数组公式,如下所示:{=SUM(IF(ISTEXT(A1:A4);VALUE(A1:A4);A1: A4))}
。如果您需要有关这些公式的帮助,请告诉我们。
问候,
麦克斯
You mean you want to use the value of the cell as a numerical one but you don't want to change the real val of the cell ?
In vba, you can use
CInt(yourvar)
to convert a string to number. Thus, the value you get will sure be an integer.[edit] Btw, if you want to display or set back the value to a cell with the format you want, you can use the
Format ( expression, [ format ] )
function of Excel vba[edit 2]
As you cannot predict how many zeros you will have, i can't see any number format that would work. You'd probably better use the Text format as Lance Robert suggested. To use formula on text cells, you can use the
VALUE
formula to convert the cell to use it as a number. If you need it on a range, you may have to use array formulas like this :{=SUM(IF(ISTEXT(A1:A4);VALUE(A1:A4);A1:A4))}
.Please let us know if you want some help on these formulas.
Regards,
Max
如果您知道所需的小数位数,则可以使用如下所示的内容:
我想您甚至可以想象一下并检查代码中尾随 0 的数量并根据需要调整格式。
更新:
这是一个 VBA 函数,它将数字作为字符串并返回适当的 NumberFormat 字符串。
If you know the number of decimal places you need, you can use something like the following:
I suppose you could even get fancy and check the number of trailing 0s in the code and adjust the formatting as required.
UPDATE:
Here's a VBA function that takes the number as a string and returns the appropriate NumberFormat string.
不敢相信我只是偶然发现了这个......
我知道它很旧,但不妨给出非常简单的答案:
自定义格式并简单地使用 @ 符号。将被视为整数并与单元格中输入的内容完全相同。
Can't believe I just stumbled on this...
I know it's old but might as well give very simple answer:
Custom format and simply use @ symbol. Will be treated as integer and left exactly as typed in to cell.
您无法使用一种自定义格式来做到这一点。
您可以做的是创建一个宏,为您执行输入并在将值放入其中时修改每个单元格格式。您可以使用宏录制器来了解从哪里开始。
You can't do this with one custom format.
What you can do is make a macro that does the input for you and modifies each cell format as it puts the value into it. You can use the Macro Recorder to get a handle on where to start with this.