在同一行中搜索2个不同的字符串

时间:2015-07-22 16:11:55

标签: vba excel-vba excel

我需要在电子表格中找到2个字符串。

省略的示例:如果两个字符串都在不同的行

接受的示例:如果两个字符串位于同一行并在列(H)中提供指向该字符串组合的超链接

这是我的代码段:

'Searching for the first string-------------------------------------------
str1 = Application.InputBox(Prompt:="Search value 1:", Title:="Search all workbooks in a folder", Type:=2)

If str1 = vbNullString Then 
    Exit Sub

'Searching for the second string-------------------------------------------
Str2 = Application.InputBox(Prompt:="Search value 2:", Title:="Search all workbooks in a folder", Type:=2)

If Str2 = vbNullString Then 
    Exit Sub

'Code for accesing Spreadsheet...
For Each sht In ActiveWorkbook.Worksheets

    Set c = sht.Cells.Find(str1, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)
    Set e = sht.Cells.Find(Str2, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)
    Set k = sht.Cells.Find(str1, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)
    Set f = sht.Cells.Find(Str2, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)

    If Not c Is Nothing Then

        firstAddress = c.Address   'Used to loop through the workSheet
        secondAddress = e.Address  'Used to loop through the workSheet

        Do     

           lRow = ws.Range("A" & Rows.Count).End(xlUp).Row
           lRow2 = ws.Range("A" & Rows.Count).End(xlUp).Row

           ws.Range("A1").Offset(lRow, 0).Value = Folderpath
           ws.Range("B1").Offset(lRow, 0).Value = Value
           ws.Range("C1").Offset(lRow, 0).Value = sht.Name

           'This code provides a link to 1-st string in the 
           ws.Range("D1").Offset(lRow, 0).Value = c.Address
           ws.Hyperlinks.Add Anchor:=ws.Range("E1").Offset(lRow, 0), Address:=Folderpath & Value, SubAddress:=sht.Name & "!" & c.Address, TextToDisplay:="Link value 1", ScreenTip:="Str 1"

           Set c = sht.Cells.FindNext(c)

        Loop While Not c Is Nothing And c.Address <> firstAddress 

Next sht

enter image description here

0 个答案:

没有答案
相关问题