VBA如何使用字符串变量循环instr函数?

时间:2017-12-11 17:40:16

标签: excel vba excel-vba

我刚刚开始学习VBA,我试图让if和loop函数一起工作。 我基本上想要在A列中搜索@,如果有@ then = ok,如果不是="无效"。 我让它工作一行,但循环它为整个列。请提出建议。 PS。请放纵我丑陋的第一个计时码。

提前谢谢你, 恭

Sub help()

    Dim email As String

    email = InStr(email, "@")

    Do While email = InStr(email, "@")
        Cells(email, 1).Value = email
        If email = 0 Then
            Cells(email, 1).Offset(, 1).Value = "Not valid"
        Else
            Cells(email, 1).Offset(, 1).Value = "ok"
        End If 
    Loop

End Sub

enter image description here

4 个答案:

答案 0 :(得分:6)

您可以设置范围,然后循环显示该范围:

Sub help()
Dim email As String
Dim rng As Range, cel As Range 'New
Dim lastRow as Long 'New

lastRow = Range("A"& rows.count).End(xlUp).Row
Set rng = Range("A2:A" & lastRow) 'Adjust as necessary

For Each cel In rng
    If InStr(1, cel.Value, "@") > 0 Then
        cel.Offset(0, 1).Value = "Ok"
    Else
        cel.Offset(0, 1).Value = "Not Valid"
    End If
   ' OR as @JohnyL points out, you can do the above in line. 
   ' Just comment out/remove the above `If` statement and uncomment below
   ' cel.Offset(0, 1) = IIf(InStr(1, cel.Value, "@") > 0, "Ok", "Not Valid")
Next cel

End Sub

这是一个可能有效的超短宏,具体取决于数据的布局方式:

Sub t()
Dim rng As Range
Set rng = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
rng.Offset(0, 1).Formula = "=IF(ISERR(SEARCH(""@"",A2)),""Not Valid"",""Yes"")"
rng.Offset(0, 1).Value = rng.Offset(0, 1).Value
End Sub

或者,您可以创建用户定义的函数。在工作簿模块中输入此代码:

Function validate_email(cel As Range) As String
If InStr(1, cel.Value, "@") > 0 Then
    validate_email = "Valid"
Else
    validate_email = "Not Valid"
End If
End Function

在单元格中,说B20,只需=validate_email(A20),我就会检查你。这样可以在任何单元格上运行,而不必编辑宏的范围。

enter image description here

另外,请注意,不需要VBA ,您只需在B列中使用公式=IF(ISERR(SEARCH("@",A2)),"Not Valid","Yes")并向下拖动。

最后,正如我在评论中提到的,这并不能真正检查电子邮件的有效性。但是,对于你的问题,它是有效的。请参阅this pagethis one,或直接搜索VBA email validation以了解更多查看电子邮件地址是否合适的方法。

答案 1 :(得分:2)

如何以稍微不同的方式做到这一点:

Sub foo()
Dim email As String
Dim lastrow As Long
lastrow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row 'change the Sheet1 to whatever
For i = 2 To lastrow 'loop through from row 2 to Last
    email = InStr(Sheet1.Cells(i, 1).Value, "@") 'do the Instr
    If email = 0 Then Sheet1.Cells(i, 2).Value = "Not Valid"
    If email > 0 Then Sheet1.Cells(i, 2).Value = "Ok"
Next i
End Sub

答案 2 :(得分:2)

您正在使用以下代码:

Option Explicit

Sub help()

Dim LastRow As Long, i As Long

LastRow = Cells(Rows.Count, "A").End(xlUp).Row ' get last row with data in column A

For i = 1 To LastRow
    If InStr(Range("A" & i).Value2, "@") > 0 Then
        Range("B" & i).Value2 = "ok"
    Else
        Range("B" & i).Value2 = "Not Valid"
    End If
Next i

End Sub

答案 3 :(得分:1)

A19开始向下,这是一个可能的解决方案:

Option Explicit

Sub help()

    Dim email       As String
    Dim rngCell     As Range

    Set rngCell = Range("A19")

    Do While rngCell <> vbNullString
        If InStr(rngCell, "@") Then
            rngCell.Offset(, 1) = "Ok"
        Else
            rngCell.Offset(, 1) = "Not valid"
        End If

        Set rngCell = rngCell.Offset(1, 0)

    Loop

End Sub