有没有一种方法可以在VBA数组的字符串中进行搜索?

时间:2020-09-15 12:38:12

标签: arrays excel vba

我正在尝试找到一种方法,一旦将其复制到数组中,就可以在数组的每个元素内搜索字符串。

在某些数组元素中该字符串绝对,但是,根据我当前的代码,从未找到该字符串。

Code with Array

任何解决此问题的帮助将不胜感激!

我已经将所有代码复制到了底部,尽管其中一些当前是占位符

    Sub ClassFinder()
    
    Dim LRowA, LRowG, LRowX As Integer
    Dim Period, Teacher As String
    Dim RowA, ColA, site As Integer
    Dim CellA, lftBrack, rgtBrack, Class, Room As String
    Dim FTeachers(), TeachNames(), arr() As String
    
    LRowA = Cells(Rows.Count, "A").End(xlUp).Row
    LRowG = Cells(Rows.Count, "G").End(xlUp).Row
    
    If LRowA <> LRowG Then
        Period = WorksheetFunction.Proper(Cells(LRowA, 3).Value2) & ":" & Cells(LRowA, 4).Value2
        Teacher = Cells(LRowA, 1).Value2
        RowA = Sheets("Staff Timetables").Range("A:A").Find(Teacher).Row
        ColA = Sheets("Staff Timetables").Range("A1:ZZ1").Find(Period).Column
        
        CellA = Sheets("Staff Timetables").Cells(RowA, ColA).Value2
        lftBrack = InStr(CellA, "(")
        rgtBrack = InStr(CellA, ")")
        
        Class = Left(CellA, 3)
        Room = Mid(CellA, lftBrack + 2, rgtBrack - lftBrack - 2)
        
        If Room = "A*" Or Room = "B0*" Or Room = "B1*" Or Room = "OCK2*" Or Room = "DO*" Then
            site = 2
        Else:
            site = 1
    End If
        
    LRowX = Sheets("Staff Timetables").Cells(Rows.Count, ColA).End(xlUp).Row
    TeachNames = Range(Sheets("Staff Timetables").Cells(1, 1), Sheets("Staff Timetables").Cells(LRowX, 1)).Value2
        
    If ColA < 11 Then
        FTeacher = Range(Sheets("Staff Timetables").Cells(1, 2), Sheets("Staff Timetables").Cells(LRowX, 9)).Value2
    ElseIf ColA < 20 Then
        FTeacher = Range(Sheets("Staff Timetables").Cells(, 11), Sheets("Staff Timetables").Cells(LRowX, 19)).Value2
    ElseIf ColA < 29 Then
        FTeacher = Range(Sheets("Staff Timetables").Cells(1, 20), Sheets("Staff Timetables").Cells(LRowX, 28)).Value2
    ElseIf ColA < 38 Then
        FTeacher = Range(Sheets("Staff Timetables").Cells(1, 29), Sheets("Staff Timetables").Cells(LRowX, 37)).Value2
    ElseIf ColA < 47 Then
        FTeacher = Range(Sheets("Staff Timetables").Cells(1, 38), Sheets("Staff Timetables").Cells(LRowX, 46)).Value2
    End If
    
    For i = LBound(FTeacher) To UBound(FTeacher)
        For j = LBound(FTeacher, 2) To UBound(FTeacher, 2)
            If FTeacher(i, j) Like ("*(#" & Room & ")") Then
                GoTo x
    x:
            
            End If
        Next j
    Next i
        
    End If
    
    End Sub

1 个答案:

答案 0 :(得分:1)

#是与任何一位数字匹配的特殊字符。

如果要字面上匹配#,则需要将其括在方括号中。另外请注意,您还有另外一组应放在括号外的引号。

If FTeacher(i, j) Like "*([#]" & Room & ")" Then

有关更多详细信息,请参见Like文档。