确定哪一列.findfind结果

我正在使用.find来search整个工作簿,并显示超链接到结果的结果。 但是,由于search到的单词可以在任何列中find,我需要知道该单词在哪个列中find,以使search结果显示正确。

这是我今天的代码,我使用了一个稍微修改的例子,我发现:

Sub Set_Hyper() ' Object variables Dim wks As Excel.Worksheet Dim rCell As Excel.Range Dim fFirst As String ' {i} will act as our counter Dim i As Long ' Use an input box to type in the search criteria Dim MyVal As String MyVal = ActiveSheet.Range("D9") Application.ScreenUpdating = False Application.DisplayAlerts = False i = 19 ' Begin looping: ' We are checking all the Worksheets in the Workbook For Each wks In ActiveWorkbook.Worksheets If wks.Name <> "Start" Then ' We are checking all cells, we don't need the SpecialCells method ' the Find method is fast enough With wks.Range("A:B") ' Using the find method is faster: ' Here we are checking column "A" that only have {myVal} explicitly Set rCell = .Find(MyVal, , , xlPart, xlByColumns, xlNext, False) ' If something is found, then we keep going If Not rCell Is Nothing Then ' Store the first address fFirst = rCell.Address Do ' Link to each cell with an occurence of {MyVal} rCell.Hyperlinks.Add Cells(i, 4), "", "'" & wks.Name & "'!" & rCell.Address, TextToDisplay:=rCell.Value wks.Range("B" & rCell.Row & ":R" & rCell.Row).Copy Destination:=Cells(i, 5) Set rCell = .FindNext(rCell) i = i + 1 'Increment our counter End If Loop While Not rCell Is Nothing And rCell.Address <> fFirst End If End With End If Next wks ' Explicitly clear memory Set rCell = Nothing ' Reset application settings Application.ScreenUpdating = True Application.DisplayAlerts = True End Sub 

我在想,我想添加这样的东西:

  If rCell.Column() = A Then ' Link to each cell with an occurence of {MyVal} rCell.Hyperlinks.Add Cells(i, 4), "", "'" & wks.Name & "'!" & rCell.Address, TextToDisplay:=rCell.Value wks.Range("B" & rCell.Row & ":R" & rCell.Row).Copy Destination:=Cells(i, 5) Set rCell = .FindNext(rCell) i = i + 1 'Increment our counter End If If rCell.Column() = B Then ' Link to each cell with an occurence of {MyVal} rCell.Hyperlinks.Add Cells(i, 4), "", "'" & wks.Name & "'!" & rCell(0, -1).Address, TextToDisplay:=rCell(0, -1).Value wks.Range("B" & rCell.Row & ":R" & rCell.Row).Copy Destination:=Cells(i, 5) Set rCell = .FindNext(rCell) i = i + 1 'Increment our counter End If 

问题是,它不能按我想要的方式工作。 我尝试过以某种方式修改它,但是要么它只是跳过整个如果部分或我根本没有得到任何结果。

我不能使用这种方式比较列,或者是什么问题?

Column A使用类似的东西,其中列由其位置(1)而不是字母(A)定义 。 正如你正在search一个两列的范围A:B那么

  If rCell.Column = 1 Then `do code for A Else `do code for B End If 

根据您粘贴的代码示例,您可以直接根据列号直接进行偏移:

  ' Link to each cell with an occurence of {MyVal} rcell.Hyperlinks.Add Cells(i, 4), "", "'" & wks.Name & "'!" & rcell.Offset(, 1 - rcell.Column).Address, TextToDisplay:=rcell.Offset(, 1 - rcell.Column).Value wks.Range("B" & rcell.Row & ":R" & rcell.Row).Copy Destination:=Cells(i, 5) Set rcell = .FindNext(rcell) i = i + 1 'Increment our counter End If