带有组合框的 Excel VBA 时间格式为十进制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4401596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:24:16  来源:igfitidea点击:

Excel VBA Time Format as decimal with Combobox

excelvbatimecomboboxformat

提问by Mika H?m?l?inen

Very basic, and very annoying, I have searched solution for many hours with no help...

非常基本,也很烦人,我在没有帮助的情况下搜索了很多小时的解决方案......

Problem: I'm populating Combobox from named range, range is list of times (formatted as time :-), Combo seems fine, drop-down shows my times as they should be, but when selected time is formatted as a decimal number...

Problem: I'm populating Combobox from named range, range is list of times (formatted as time :-), Combo seems fine, drop-down shows my times as they should be, but when selected time is formatted as a decimal number. ..

alt text

替代文字

Here is the code (ripped down to bare minimum):

下面是代码(精简到最低限度):

Private Sub UserForm_Initialize()
    ComboBoxTime.RowSource = "Help!Time" 
End Sub

"Help" is name of worksheet containing named range "Time"I have tried formatting different ways with no luck...

“帮助”是包含命名范围“时间”的工作表的名称我尝试过格式化不同的方式但没有运气......

ComboBoxTime = Format(ComboBoxTime, "hhmm")

Here is link to sample. http://www.equstom.fi/dateproblem.html

这是示例的链接。http://www.eqstom.fi/dateproblem.html

(And yes I need to populate from named range, instead for each loop, and I will set .value with code, Whole document is actually quite complex, but I included just The problem part...)

(是的,我需要从命名范围填充,而不是为每个循环填充,我将使用代码设置 .value,整个文档实际上非常复杂,但我只包含了问题部分......)

回答by Dr. belisarius

Try something like this:

尝试这样的事情:

Private Sub ComboBox1_Change()
With ComboBox1
  .Value = Format(.Value, "hh:mm:ss AMPM")

End With
End Sub 

HTH!

哼!

Edit This is what I see when leaving your combo. The time display works OK.

编辑这是我离开你的组合时看到的。时间显示工作正常。

alt text

替代文字

Edit 2

编辑 2

Found the error "invalid property":

发现错误“无效属性”:

You must set "Match Requiered" to FALSE in the combo box. If you consider that it should be "TRUE" you will have to validate by hand ...

您必须在组合框中将“Match Requiered”设置为 FALSE。如果您认为它应该是“TRUE”,则必须手动验证...

回答by Mika H?m?l?inen

The problem is named range I'm using, when values are formatted as time it won't work. I got it to work if values were Text! Problem has something to do with excel being in Finnish and VBA in english... I added second column next to range which copies text values to this named second range formatted as time. Quick and Dirty! (Thanks for your input Belisarius)

问题是我正在使用的命名范围,当值被格式化为时间时它不起作用。如果值为文本,我就可以使用它!问题与芬兰语中的 excel 和英语中的 VBA 有关系......我在范围旁边添加了第二列,该列将文本值复制到这个命名为时间格式的第二个范围。又快又脏!(感谢您的输入贝利撒留)