Excel中的VBA返回types不匹配

我试图创build一个macros,它将根据AB中的内容修改S,W和AH列中的内容,例如,如果AB1 =否,则S1 = C-MEM,AH = N / A,W被清除。

出于某种原因,我的if语句的第一行出现了“types不匹配”错误,并且无法弄清楚为什么或者如何解决这个错误 – 甚至在阅读了其他关于类似问题的文章之后。

Sub test() Dim lastrow As Long Dim i As Long lastrow = Range("AB" & Rows.Count).End(xlUp).Row For i = 2 To lastrow **-> If Range("AB" & i).Value = "No" Then Range("S" & i).Value = "C-MEM" Range("W" & i).Value = "" Range("AH" & i).Value = "N/A" End If Next i End Sub 

谢谢

您正试图testing错误是否= No

testing错误并跳过该循环中的逻辑:

 Sub test() Dim lastrow As Long Dim i As Long lastrow = Range("AB" & Rows.Count).End(xlUp).Row For i = 2 To lastrow If Not IsError(Range("AB" & i).Value) Then If Range("AB" & i).Value = "No" Then Range("S" & i).Value = "C-MEM" Range("W" & i).Value = "" Range("AH" & i).Value = "N/A" End If End If Next i End Sub