如何在 VBA 中检索工作表中格式的 Excel 单元格值?
我需要在数字列表中保留前导零。数字像这样添加(在循环中,但这只是使用 (1, 1) 的示例:
Set cel = Sheet.worksh.Cells(1, 1)
cel.ColumnWidth = 10
cel.Value = e.Name
cel.NumberFormat = "0000"
其中 e.Name
是数字,类似于“0720”。这显示在工作表很好,但如果我这样做:
Msgbox Sheet.worksh.Cells(1, 1).Value
我得到“720”,我需要它是“0720”,我知道我可以使用 Len() 进行检查并以这种方式添加零,但我想知道是否有使用 Range 对象更直接的方法可以为我做到这一点。
I need to keep leading zeros on a list of numbers. The numbers are added like this (in a loop, but this is just an example using the (1, 1):
Set cel = Sheet.worksh.Cells(1, 1)
cel.ColumnWidth = 10
cel.Value = e.Name
cel.NumberFormat = "0000"
Where e.Name
is the number, something like "0720". This displays on the worksheet just fine, but if I do something like this:
Msgbox Sheet.worksh.Cells(1, 1).Value
I get "720". I need it to be "0720", I know I could check using Len()
and add the zeros that way, but I was wondering if there was a more direct approach with the Range object that would do this for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您将数字与其文本表示混淆了。
您需要
.Text
属性,而不是.Value
,但是 您可能会遇到问题。You are confusing a number with its textual representation.
You want the
.Text
property, not.Value
, but then, you might have problems with it.使用这个:
Use This:
更好的是,如果您不确定格式是什么:
Msgbox Format(Sheet.worksh.Cells(1,1).Value, Sheet.worksh.Cells(1,1).NumberFormat)
Even better, if you're not sure what the format is:
Msgbox Format(Sheet.worksh.Cells(1,1).Value, Sheet.worksh.Cells(1,1).NumberFormat)
虽然有点晚了,但您始终可以尝试分别调用值和数字格式,如下所示:
A little late to the party, but you could always try calling up the value and number format separately, like this: