嗨,感謝您的幫助。使用.find的問題
我製作了一個小型宏,它將原始格式的大學課程轉換爲一些描述的時間表...它使用.find找到了一天,然後使用.find開始和結束時間,然後將數據繪製到適當的單元格中。一切都很好,直到遇到5個字符長的時間,例如15.15; 12.25 14.35。它對一切都很滿意。我想這可能是我如何聲明變量的問題,或者是.find和廣泛的故障排除之後的問題,我沒有更接近解決這個問題。根據5個字符時間是否在課程的開始或結束時間,問題在起始停止行或開始行中出現。以下是有問題的代碼:
Sub MakeSchedule()
Dim x As Long
Dim y As Long
Dim tot As Long
Dim xday As Long
Dim xdetails As Long
Dim xstart As Long
Dim xstoptime As Long
Dim day As String
Dim details As String
Dim start As Single
Dim stoptime As Single
Dim daycol As Long
Dim startrow As Long
Dim stoptimerow As Long
xday = 3
xstart = 4
xstoptime = 5
xdetails = 6
Range("I1").Select
tot = ActiveCell.End(xlDown).Row - 1
For y = 1 To tot
day = ActiveCell.Offset(y, xday).Value
daycol = Range("B1:F1").Find(day, LookIn:=xlValues, lookat:=xlWhole).Column
start = ActiveCell.Offset(y, xstart).Value
stoptime = ActiveCell.Offset(y, xstoptime).Value
details = ActiveCell.Offset(y, xdetails).Value
startrow = Range("A1:A134").Find(start, LookIn:=xlValues, lookat:=xlWhole).Row
stoptimerow = Range("A1:A134").Find(stoptime, LookIn:=xlValues, lookat:=xlWhole).Row
Range(Cells(startrow, daycol), Cells(stoptimerow, daycol)).Value = details
Next
End Sub
你可以試試'9.15'的時間,看看是否也會產生錯誤?我認爲你幾乎肯定有浮點精度問題 – barrowc
嘿,謝謝你的迴應!不幸的是,我只是試了一下,9.15也產生了錯誤。這是否意味着它不是浮點舍入錯誤? stoptime變量被定義爲Single;那不處理浮點? –
嘗試格式化您的單元格爲'時間' – jsotola