查找具有特定string值的范围内的所有单元格

我试图理解为什么下面的代码不能正常工作,我有点疯狂。

基本上我有一张表格,里面有不同栏目的货币,B列有一系列的描述。 我正在做的是使用FINDfunction查找“GBP”列和“LCH ISSUE DESCRIPTION”行。 我需要两个单元格中的列和行号('LCH ISSUE DESCRIP.row,GBP.column)我有我需要的信息,并将其放在单独的选项卡中,使用SPLIT函数。

问题是“LCH ISSUE DESCRIPTION”在列B中多次出现,这意味着我必须在循环中使用FIND函数。 代码工作正常的第一个实例,但是而不是击中包含相同值的后续单元格(行),它只是向下移动一行。 任何想法我做错了什么?

Sub get_macdata_1() Dim LastCell As Range, issuerFound As Range Dim shName As String, issuer As String, ccy As String, inputText As String, firstaddress As String Dim ccyColumn As Integer, issuerRow As Integer Dim i As Long, r As Long Dim splitText As Variant ccy = "GBP" issuer = "LCH ISSUE DESCRIPTION" shName = "December 2014" ccyColumn = Worksheets(shName).Cells.Find(What:=ccy, LookIn:=xlValues, LookAt:=xlWhole).Column With Worksheets(shName).Range("B:B") Set LastCell = .Cells(.Cells.Count) End With Set issuerFound = Worksheets(shName).Range("B:B").Find(What:=issuer, After:=LastCell, LookAt:=xlWhole) If Not issuerFound Is Nothing Then firstaddress = issuerFound.Address End If Do Until issuerFound Is Nothing issuerRow = issuerFound.Row inputText = Cells(issuerRow, ccyColumn).Value splitText = Split(inputText, " ") r = Worksheets("mac_data").Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row For i = 0 To UBound(splitText) Sheets("mac_data").Cells(r + 1, i + 1) = splitText(i) Next i 'Worksheets(shName).Activate Set issuerFound = Worksheets(shName).Range("B:B").FindNext(After:=issuerFound) If issuerFound.Address = firstaddress Then Exit Do End If Loop End Sub 

我能够复制我认为你的问题是在我自己的版本上,我认为你的工作簿是。 复制的错误使每个单元格都复制到第一个和最后一个find的“LCH ISSUE说明”之间的“mac_data”选项卡上,而不仅仅是匹配的单元格。

我可以通过更改Set issuerFound = Worksheets(shName).Range("B:B").FindNext(After:=issuerFound)Set issuerFound = Worksheets(shName).Range("B:B").Find(What:=issuer, After:=issuerFound, LookAt:=xlWhole)

完整的代码如下所示:

 Sub get_macdata_1() Dim LastCell As Range, issuerFound As Range Dim shName As String, issuer As String, ccy As String, inputText As String, firstaddress As String Dim ccyColumn As Integer, issuerRow As Integer Dim i As Long, r As Long Dim splitText As Variant ccy = "GBP" issuer = "LCH ISSUE DESCRIPTION" shName = "December 2014" ccyColumn = Worksheets(shName).Cells.Find(What:=ccy, LookIn:=xlValues, LookAt:=xlWhole).Column With Worksheets(shName).Range("B:B") Set LastCell = .Cells(.Cells.Count) End With Set issuerFound = Worksheets(shName).Range("B:B").Find(What:=issuer, After:=LastCell, LookAt:=xlWhole) If Not issuerFound Is Nothing Then firstaddress = issuerFound.Address End If Do Until issuerFound Is Nothing issuerRow = issuerFound.Row inputText = Cells(issuerRow, ccyColumn).Value splitText = Split(inputText, " ") r = Worksheets("mac_data").Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row For i = 0 To UBound(splitText) Sheets("mac_data").Cells(r + 1, i + 1) = splitText(i) Next i 'Worksheets(shName).Activate Set issuerFound = Worksheets(shName).Range("B:B").Find(What:=issuer, After:=issuerFound, LookAt:=xlWhole) If issuerFound.Address = firstaddress Then Exit Do End If Loop End Sub 

希望这对你有用!