如何查找数组是否包含字符串 [英] How to find if an array contains a string
问题描述
可能的重复:
如何在 MS Access VBA 数组中搜索字符串一个>
我目前正在处理 Excel 宏,但找不到类似的方法if array.contains(mystring)
I am currently working on an Excel macro, and I could not find a way to do like
if array.contains(mystring)
我写了以下内容,它给了我消息Invaild Qualifier"并在If
I wrote the following, and it gives me the message "Invaild Qualifier" and highlights the Mainfram
right after If
Dim Mainfram(4) As String
Mainfram(0) = "apple"
Mainfram(1) = "pear"
Mainfram(2) = "orange"
Mainfram(3) = "fruit"
For Each cel In Selection
If Mainfram.Contains(cel.Text) Then
Row(cel.Row).Style = "Accent1"
End If
Next cel
选择的是一列
有人帮忙吗?
JP我尝试了你的建议,它说需要对象.并突出显示If IsInArray(cell.Text, Mainfram) Then这是我的完整代码
Hi, JP I tried your suggestion, and it said Object required. And Highlightd the If IsInArray(cell.Text, Mainfram) Then Heres my full code
Sub changeRowColor()
Columns("B:B").Select
Dim cel As Excel.Range
Dim Mainfram(4) As String
Mainfram(0) = "apple"
Mainfram(1) = "pear"
Mainfram(2) = "orange"
Mainfram(3) = "Banana"
For Each cel In Selection
If IsInArray(cell.Value, Mainfram) Then
Rows(cel.Row).Style = "Accent1"
End If
Next cel
End Sub
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function
没关系,我发现了那个愚蠢的错误......还是谢谢你
Nevermind, I found that stupid Error... Thank you anyways
推荐答案
使用 我的回答 中的代码到一个非常类似问题:
Using the code from my answer to a very similar question:
Sub DoSomething()
Dim Mainfram(4) As String
Dim cell As Excel.Range
Mainfram(0) = "apple"
Mainfram(1) = "pear"
Mainfram(2) = "orange"
Mainfram(3) = "fruit"
For Each cell In Selection
If IsInArray(cell.Value, MainFram) Then
Row(cell.Row).Style = "Accent1"
End If
Next cell
End Sub
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function
这篇关于如何查找数组是否包含字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!