Excel VBA单元格值显示为整数
我正在使用Excel的VBA函数编写程序,但是遇到一个非常奇怪的逻辑错误。
For SheetIndex = 1 To 6 With ActiveSheet For ColIndex = 2 To 6 For DateIndex = 0 To MinLimit datethingy = .Cells(1, ColIndex).Value If (.Cells(1, ColIndex).Value = Date_Array(DateIndex, 1)) Then For RowIndex = 2 To 11 ' Compare every time slot value here to every time slot value in the array datethingy = Trim(CStr(.Cells(RowIndex, 1).Value)) 'ERROR OCCURS HERE If (Trim(CStr(.Cells(RowIndex, 1).Value)) = Date_Array(DateIndex, 2)) Then .Cells(RowIndex, ColIndex).Value = "Checked" End If Next End If Next Next End With SheetIndex = SheetIndex + 1 Application.Worksheets(SheetIndex).Activate Next 所以在上面的代码中,我将遍历一系列单元格值并比较数组中已经具有的值。但是,当我从单元格中提取值而不是“ 8:30”时,它显示为“0.354166666…7”。我不知道为什么会这样,我什至确保将它与字符串而不是int或其他东西进行比较。

这是我设置表格单元格的值。

.Cells(2, 1).Value = "8:30" .Cells(3, 1).Value = "9:00" .Cells(4, 1).Value = "10:15" .Cells(5, 1).Value = "11:30" .Cells(6, 1).Value = "12:30" .Cells(7, 1).Value = "13:30" .Cells(8, 1).Value = "14:45" .Cells(9, 1).Value = "16:00" .Cells(10, 1).Value = "17:15" .Cells(11, 1).Value = "18:15" With Range("A2", "A11") .HorizontalAlignment = xlCenter .Font.Bold = True .ColumnWidth = 15 End With ActiveSheet.Cells.Rows.AutoFit 有人知道为什么会这样吗?

Excel将时间和日期存储为数字。每个整数都是一天,小时,分钟和秒细分为一天的小数部分。 8:30只是没有整数的时间,8.5/24 =0.3541667。
您可以使用此代码进行测试,这可能为您提供格式化输入的方法。在单元格A1中输入8:30
Sub test() Debug.Print sheet1.Range("A1").Value = "8:30" 'returns false Debug.Print Format(sheet1.Range("A1").Value, "h:mm") = "8:30" 'returns true Debug.Print Sheet1.Range("A1").Text = "8:30" 'return true End Sub
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shumachanpin/article-341847-1.html
好费电啊
感觉是美国品牌评选