VBA,如果字符串包含某个字母 [英] VBA, if a string contains a certain letter
问题描述
我通常不使用VBA
,因此我无法弄清楚.我正在尝试确定我的书架上的字符串中是否包含某个字母.
I do not usually work with VBA
and I cannot figure this out. I am trying to determine whether a certain letter is contained within a string on my spreadhseet.
Private Sub CommandButton1_Click()
Dim myString As String
RowCount = WorksheetFunction.CountA(Range("A:A"))
MsgBox RowCount
For i = 2 To RowCount
myString = Trim(Cells(i, 1).Value)
If myString.Contains("A") Then
oldStr = Cells(i, 15).Value
newStr = Left(oldStr, oldStr.IndexOf("A"))
End If
Next
End Sub
此代码应遍历值列表,如果遇到字母A则将其删除以及其后的所有内容.我在IF
语句无效限定符"中遇到问题.无论单元格中的字符串是否包含字母A,如何使IF
语句输出?
This code should go through a list of values and if it encounters the letter A to remove it and everything that comes after it. I am getting problems at my IF
statement, Invalid Qualifier. How would I be able to make my IF
statement output whether or not the String in the cell contains the letter A?
非常感谢
推荐答案
尝试使用InStr函数,该函数返回找到字符的字符串中的索引.如果InStr返回0,则找不到该字符串.
Try using the InStr function which returns the index in the string at which the character was found. If InStr returns 0, the string was not found.
If InStr(myString, "A") > 0 Then
对于分配给newStr的行上的错误,也将oldStr.IndexOf转换为该InStr函数.
For the error on the line assigning to newStr, convert oldStr.IndexOf to that InStr function also.
Left(oldStr, InStr(oldStr, "A"))
这篇关于VBA,如果字符串包含某个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!