2016-03-07 69 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

相關問題