2017-08-31 58 views
0

嗨,感谢您的帮助。使用.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 
+1

你可以试试'9.15'的时间,看看是否也会产生错误?我认为你几乎肯定有浮点精度问题 – barrowc

+0

嘿,谢谢你的回应!不幸的是,我只是试了一下,9.15也产生了错误。这是否意味着它不是浮点舍入错误? stoptime变量被定义为Single;那不处理浮点? –

+0

尝试格式化您的单元格为'时间' – jsotola

回答

1

请勿在Find中使用Single数据类型。更好地使用Variant数据类型。

Sub FIND_Converts_Single_To_Double() 
Dim sSingle As Single, dDouble As Double, a As Double 
'Dim sSingle As Variant, dDouble As Double, a As Double 
a = 22.23 
sSingle = a 
dDouble = a 
Debug.Print sSingle = dDouble 
Debug.Print dDouble, sSingle, CDbl(sSingle) 
cells(1,1).value=a 
Set c1 = Range("A1:A134").Find(sSingle, , LookIn:=xlValues, lookat:=xlWhole) 
Set c2 = Range("A1:A134").Find(dDouble, , LookIn:=xlValues, lookat:=xlWhole) 
If c1 Is Nothing Then 
Debug.Print "Nothing" 
Else 
Debug.Print c1 
End If 
Debug.Print c2 
End Sub 
+0

非常感谢您的帮助!立即排序!我应该如何知道在哪种情况下使用哪种数据类型,以及如何轻松地告诉它是导致错误的数据类型?有一个我应该知道的链接可以解释这个吗?再次感谢。 –

+0

1)要知道使用哪种数据类型,可以使用Typename(activecell.Value),2)不要使用单一数据类型。 3)链接:http://vb6reference.tomswebdesign.net/conversion.html – Docmarti