简体   繁体   中英

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".

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!

You could exit the loop as soon as the test returns true like this:

Exit for

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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