繁体   English   中英

查找具有特定字符串值的范围内的所有单元格

[英]Find all cells in a range with a specific string value

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

基本上,我有一张表,其中包含不同列中的货币和列 B 中的一系列描述。 我正在做的是使用 FIND 函数来查找“GBP”列和“LCH 问题描述”行。 我需要两个的列号和行号,如 Cells('LCH ISSUE DESCRIP.row, GBP.column) 我有我需要的信息,我使用 SPLIT 函数将其放在单独的选项卡中。

问题是“LCH 问题描述”在 col B 中出现多次,这意味着我必须在循环中使用 FIND 函数。 该代码在第一个实例中工作正常,但随后不会点击包含相同 value 的后续单元格(行),而是简单地向下移动一行。 知道我做错了什么吗?

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

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

我能够通过将Set issuerFound = Worksheets(shName).Range("B:B").FindNext(After:=issuerFound)更改为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

希望这对你有用!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM