简体   繁体   English

我无法使此代码正常工作,这是“类型不匹配错误”

[英]I cannot make this code work, it is a “type mismatch error”

Is there anything wrong with this code. 这段代码有什么问题吗? I need to look through a column and if the value is less than a reference value (it is a timer) than copy adjacent cell and put in "A8". 我需要查看一列,如果该值小于参考值(它是一个计时器),则需要复制相邻的单元格并放入“ A8”中。

Thanks. 谢谢。

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

you could try testing for a numeric value in the cell before testing for less than: 您可以尝试在单元格中测试数字值,然后再测试小于以下值:

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

BTW, the comments above by David and Kazjaw are quite right, every iteration of the loop you would potentionally overwrite cell A8! 顺便说一句,David和Kazjaw的上述评论是正确的,循环的每次迭代都可能会覆盖A8单元!

You could exit the loop as soon as the test returns true like this: 测试一旦返回true,您可以退出循环,如下所示:

Exit for

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM