繁体   English   中英

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

[英]VBA How do I loop an instr function using a string variable?

我刚刚开始学习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

在此输入图像描述

您可以设置范围,然后遍历该范围:

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) ,我就会检查你。 这样做的好处是可以在任何单元格上运行,而不必编辑宏的范围。

在此输入图像描述

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

最后,正如我在评论中提到的,这并不能真正检查电子邮件的有效性。 但是,对于你的问题,它是有效的。 请参见本页面本一个 ,或者干脆寻找VBA电子邮件验证更多的方式来检查,如果电子邮件地址是正确的。

怎么样做一个稍微不同的方式:

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

您正在使用以下代码:

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

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

暂无
暂无

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

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