2016-03-07 117 views
2

我在尝试理解下面的代码无法正常工作的原因时变得有点疯狂。查找特定字符串值的范围内的所有单元格

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

问题是'LCH ISSUE DESCRIPTION'在col 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 
+0

我仍然对你试图找到哪个值以及一旦你找到它想要做什么感到困惑。 – iShaymus

+0

我想要查找的值位于货币列和“lch发行人描述”行的“笛卡尔积”中。该单元格包含我分割并放入'mac_data'表格中的不同列的各种信息 – yp10

回答

2

我能够复制我认为你的问题是在我自己的版本上,我认为你的工作簿是。复制的错误使每个单元格都复制到第一个和最后一个找到的“LCH问题描述”之间的“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 

希望这对你的作品!

+0

谢谢,这对我来说也是完美的! 我可以问为什么在这种情况下,我们只需要复制FIND函数而不是FINDNEXT?是因为我们在两张表之间移动数据? 我要求阅读网络上的几个线程我只找到FIND + FINDNEXT的组合 – yp10

相关问题