我不能使这个代码工作,这是一个“types不匹配的错误”

这段代码有什么问题吗? 我需要查看一个列,如果该值小于一个参考值(它是一个计时器)比复制相邻单元格,并放在“A8”。

谢谢。

Sub GetData() Dim i As Integer For i = 4 To 31 If Cells(i, 38) < Cells(32, 5) Then Cells(1, 8) = Cells(i, 39) End If Next i End Sub 

或者,也可以对所有提供的选项在现有的if之前添加此额外的If statement

  If IsError(Cells(i, 39)) = False And IsError(Cells(32, 5))= False Then 

您可以尝试在testing之前在单元格中testing一个数字值:

 Sub GetData() Dim i As Integer For i = 4 To 31 if isnumeric(cells(i,38)) then If Cells(i, 38) < Cells(32, 5) Then Cells(1, 8) = Cells(i, 39) ' Exit For ' UN-COMMENT to exit loop End If else msgbox "Cell '" & cells(i,38).address & "' may have an error",vbexclamation+vbokonly end if Next End Sub 

顺便说一句,大卫和Kazjaw上面的评论是非常正确的,循环的每一个迭代你可能会覆盖单元格A8!

只要testing返回true就可以退出循环,如下所示:

Exit for

为避免不匹配,请尝试与单元格的文本进行比较:

If Cells(i,38).Text < Cells(32,5)...