string - 查看是否存在某个字符

标签 string vba excel

我想遍历一定数量的单元格,看看它们是否包含字母表中的任何字符。我创建了包含所有小写和大写字母的字符串字母表。
单元格中应开始包含信息的点位于第 4 行第 1 列。程序应从先前声明为 ClassList 的工作表中搜索这些单元格。它只会搜索 40 个单元格。

For a = 4 to 44
    For b = 1 to Len(alphabet)
         If ClassList.ActiveSheet.Cells(a,2) = Mid(alphabet, b, 1) then
              NoStudents = NoStudents + 1
              Exit For
         End If
    Next b
Next a 

我知道ClassList.ActiveSheet.Cells(a,2)工作正常,因为它已在其他部分正常工作。问题是 If 语句由于某种原因永远不会正确,即使我可以看到单元格中的第一个字符清楚地在字母表中!

最佳答案

您可以使用 Like无需循环字母即可查找字母:

For a = 4 To 44
    If ClassList.ActiveSheet.Cells(a, 2) Like "*[A-Za-z]*" Then
        NoStudents = NoStudents + 1
    End If
Next a

关于string - 查看是否存在某个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35793590/

相关文章:

excel - 在 Access VBA 中将子例程应用于日期范围

vba - 如果单元格是公式,则选择列标题

python - django excel xlwt

Excel删除双空格后的所有内容

Excel VBA 在工作表上生成 #N/A 错误

c++ - 用于将 std::string 转换为 QString 的包装器?

java - 如何从字符串中解析出字符?

ios - swift :Issue with increment Int inside String

c# - 使用正则表达式进行有限的字符串替换?

excel - 在 excel vba 中从 access 2007 运行参数查询时出错