如何实现InStr搜索功能?

时间:2019-03-28 17:39:21

标签: vba

我有一个工具可以遍历数据集的行并显示单元格内容(作业信息),如果该行的其余部分与我在A列中列出的条件匹配。 除了在脚本中匹配.value之外,我如何实现类似InStr的东西,这样它就不需要匹配整个单元格大小写,而是可以是句子或文本字符串中的变量? 目前,我希望它包含InStr功能,但我无法弄清楚:

   For Each cSkills In rSkills

            If c.Value = cSkills.Value Then i = i + 1

        Next cSkills

     其余脚本:

Sub Find_1_Skill()


Dim c As Range, r As Range
Dim row As Integer, i As Integer
Dim rSkills As Range, cSkills As Range
Dim JobCodeMatch As Integer, JobTitleMatch As Integer, CLevelMatch As Integer
Dim JobCode As String, JobTitle As String, CareerLevel As String

row = 2
JobCodeMatch = 2
JobTitleMatch = 2
CLevelMatch = 2

Set rSkills = Application.Selection
Set rSkills = Application.InputBox("Select Job Competencies in Column A", TitleID, rSkills.Address, Type:=8)

Do While row < 2400

    i = 0

    JobCode = Cells(row, 6).Value
    JobTitle = Cells(row, 7).Value
    CareerLevel = Cells(row, 14).Value

    Set r = Range(Cells(row, 6), Cells(row, 336))

    For Each c In r

        For Each cSkills In rSkills

            If c.Value = cSkills.Value Then i = i + 1

        Next cSkills

    Next c

    If i = 1 Then

        Cells(JobCodeMatch, 3) = JobCode
        Cells(JobTitleMatch, 4) = JobTitle
        Cells(CLevelMatch, 5) = CareerLevel

        JobCodeMatch = JobCodeMatch + 1
        JobTitleMatch = JobTitleMatch + 1
        CLevelMatch = CLevelMatch + 1

    End If

    row = row + 1

Loop

End Sub

worksheet

1 个答案:

答案 0 :(得分:1)

您可以使用Find个匹配项来实现FindNext xlPart方法

Public Sub demo()
    Dim rSkills As Range, cSkills As Range, searchRng As Range
    Dim firstSkill As String
    Dim JobCode As String, JobTitle As String, CareerLevel As String
    Dim c

    With SrcSheet
        Set searchRng = .Range(.Cells(2, 6), .Cells(.Cells(.Rows.Count, 6).End(xlUp).Row, 336))
    End With

    Set rSkills = Application.Selection
    Set rSkills = Application.InputBox("Select Job Competencies in Column A", , rSkills.Address, Type:=8)

    For Each c In rSkills
        Set cSkills = searchRng.Find(c.Value2, lookat:=xlPart)
        If Not cSkills Is Nothing Then
            firstSkill = cSkills.Address

            Do
                With SrcSheet
                    JobCode = .Cells(cSkills.Row, 6).Value2
                    JobTitle = .Cells(cSkills.Row, 7).Value2
                    CareerLevel = .Cells(cSkills.Row, 14).Value2
                End With

                ' Use Ctrl+G to view debug window
                Debug.Print cSkills.Address, cSkills.Value2

                Set cSkills = searchRng.FindNext(cSkills)
            Loop Until cSkills Is Nothing Or firstSkill = cSkills.Address
        End If
    Next c
End Sub
相关问题