再次出现一个问题,即使感觉像iam一样,我也无法通过搜索真正找到正确的答案。 我的目标是检查一列中每个单元格是否包含(除其他值之外)数组中的字母。
所以电池看起来像123A。 我的数组包含值A,C,D,X,Y,Z。 两者都只是示例值。 如果单元格包含任何字母,则我的If陈述应该为true,因此对于此示例,它应该为true。 对于值123B的单元格,它应该是错误的(数组中没有B)。
到目前为止,我已经找到了一个“ IsinArray”函数,该函数似乎正在运行,但是会检查特定的值,但是我需要的是---> "*" & IsinArray & "*"
我发现的功能如下:
Public Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0))
End Function
我的For语句遍历该列也有效(我想说^^)
非常感谢您,如果您有任何新的想法可以更好地做到这一点,Iam也会接受“创意”解决方案。
答案 0 :(得分:1)
尝试
Public Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
Dim s As String, i As Integer
Dim a As Variant
For i = 1 To Len(stringToBeFound)
s = Mid(stringToBeFound, i, 1)
For Each a In arr
If s = a Then
IsInArray = True
Exit Function
End If
Next a
Next i
End Function
答案 1 :(得分:0)
您可以四处扫动
Option Explicit
Public Sub Test()
Dim testArray(), cellValue As String, rng As Range
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1") '<== contains
testArray = Array("A", "C", "D", "X", "Y", "Z")
Debug.Print IsInArrayValue(testArray, rng)
End Sub
Public Function IsInArrayValue(ByVal testArray As Variant, ByVal rng As Range) As Variant
Dim i As Long, testString As String
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue = CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
如果用作UDF,则可以如上所述传递数组,或者如果数组不变,则可以移入函数本身。就个人而言,我更喜欢将数组作为参数传递给函数,因为它更灵活。我无法确定您要复制的行来自何处。发布为答案的注释使用了j变量,该变量似乎未出现在所示循环中,并且该行是从另一张纸复制的。因此,下面的内容不能直接起作用,而是为您提供了一个框架。
Public Function IsInArrayValue(ByVal rng As Range) As Variant
Dim i As Long, testString As String, testArray()
testArray = Array("A", "C", "D", "X", "Y", "Z")
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
致电:
以下评论看起来像是一个新问题,但您可能想要以下内容:
Dim loopRange As Range, rng As Range
With ThisWorkbook.Worksheets("Filter")
Set loopRange = .Range(.Cells(1, VarNutzerSpalte), .Cells(VarAnzahlZeilen, VarNutzerSpalte))
End With
For Each rng In loopRange
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
rng.EntireRow.Copy '<= use Union to gather range if all being pasted in a block somewhere
End If
Next
联合版本可能如下:
Dim loopRange As Range, rng As Range, unionRng As Range
With ThisWorkbook.Worksheets("Filter")
Set loopRange = .Range(.Cells(1, VarNutzerSpalte), .Cells(VarAnzahlZeilen, VarNutzerSpalte))
End With
For Each rng In loopRange
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
If Not unionRng Is Nothing Then
Set unionRng = Union(unionRng, rng)
End If
Set unionRng = rng '<= use Union to gather range if all being pasted in a block somewhere
End If
Next
If Not unionRng Is Nothing Then
unionRng.EntireRow.Copy 'destination for paste
End If
答案 2 :(得分:0)
很抱歉回答我自己的问题(当然,欢迎对此解决方案进行更正/反馈)
我这样尝试过,我猜它应该可以工作(不能真正测试,因为我的黑手党其他部分都可以工作) 这是不必要的复杂操作,也许很慢,但id表示它可以工作:
For i = 1 To VarAnzahlZeilen
Set rng = Worksheets("Filter").Range(Cells(i, VarNutzerSpalte), Cells(i, VarNutzerSpalte))
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
Worksheets("Import").Rows(j).Copy
Worksheets("Filter").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End If
Next i
QHarr使用此功能(仅更改阵列名称)
Public Function IsInArrayValue(ByVal testArray As Variant, ByVal rng As Range) As Variant
Dim i As Long, testString As String
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue = CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
非常感谢@QHarr和@ Dy.Lee!