我试图理解为什么以下代码无法正常工作,我有点疯狂。
基本上我有一张包含不同列中货币的表单,并且在col B中有一系列描述。我正在做的是使用FIND功能查找' GBP'栏目和LCH问题说明'行。我需要两个列中的列号和行号,就像在单元格中一样(' LCH ISSUE DESCRIP.row,GBP.column)我有我需要的信息,我使用SPLIT函数将其放在一个单独的选项卡中。
问题在于LCH问题描述'在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 :(得分:2)
我能够复制我认为你的问题在我自己的版本上我认为你的工作簿是什么。复制的错误使每个单元格复制到第一个和最后一个找到的“LCH ISSUE DESCRIPTION”之间的“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
希望这适合你!