【发布时间】:2015-07-10 06:47:01
【问题描述】:
我正在开发一个具有 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
有谁知道为什么会发生这种情况?
【问题讨论】:
-
检查单元格的格式,当输入的是“日期/时间”时,单元格的格式可以是“数字”,把你的时间变成数字表示