我目前正在研究Excel宏,但我找不到一种方法
if array.contains(mystring)
我写了以下内容,它给了我“Invaild Qualifier”的消息,并在Mainfram
之后突出显示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 我尝试了你的建议,并说它需要对象。并突出了 如果是IsInArray(cell.Text,Mainfram)那么 继承了我的完整代码
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
没关系,我发现那个愚蠢的错误...... 不管怎样,谢谢你
答案 0 :(得分:128)
使用my answer中的代码来解决一个非常相似的问题:
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
答案 1 :(得分:17)
使用JOIN
和INSTR
Sub Sample()
Dim Mainfram(4) As String, strg As String
Dim cel As Range
Dim Delim As String
Delim = "#"
Mainfram(0) = "apple"
Mainfram(1) = "pear"
Mainfram(2) = "orange"
Mainfram(3) = "fruit"
strg = Join(Mainfram, Delim)
strg = Delim & strg
For Each cel In Selection
If InStr(1, strg, Delim & cel.Value & Delim, vbTextCompare) Then _
Rows(cel.Row).Style = "Accent1"
Next cel
End Sub
答案 2 :(得分:4)
答案 3 :(得分:1)
我担心我不认为有这样做的捷径 - 如果只有人会为VB6写一个linq包装器!
你可以编写一个函数来循环遍历数组并检查每个条目 - 我认为你不会比这更清晰。
有一篇示例文章提供了一些详细信息:http://www.vb6.us/tutorials/searching-arrays-visual-basic-6