For循环中的if语句出现问题

时间:2020-10-19 10:19:09

标签: excel vba for-loop if-statement

我正在尝试在For循环中插入If语句,但一直出错。 代码是:

For i = 3 To lastRow
    If Range("Q" & i).Value = "Data confirmed" Then Range("Q" & i).Value = ""
    Else
    sh.Cells(i, "Q").Hyperlinks.Add Anchor:=sh.Cells(i, "Q"), Address:="", _
    SubAddress:="", TextToDisplay:="Click To Save"
    End If
    
Next i

当单元格包含值“ Data Confirmed”时,不应包含任何超链接,并且值应为“” 如果单元格中没有“已确认数据”,则应弹出超链接(请参阅“ ELSE”之后的代码)

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

尝试尽可能简化并运行代码。绕过不同的列将更好地说明这一点。这个:

Sub TestMe()
    
    Dim i As Long

    For i = 3 To 10
        With Sheet1
            If .Range("A" & i) = "Data confirmed" Then
                .Range("B" & i).Value = ""
            Else
                .Cells(i, "C").Hyperlinks.Add Anchor:=.Cells(i, "D"), Address:="", _
                    SubAddress:="", TextToDisplay:="Click To Save"
            End If
        End With
    Next i
End Sub

产生此:

enter image description here