简体   繁体   English

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

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

I am getting a bit crazy in trying to understand why the below code is not working properly.我在试图理解为什么下面的代码不能正常工作时有点疯狂。

Basically I have a sheet with currencies in different columns and in col B a series of descriptions.基本上,我有一张表,其中包含不同列中的货币和列 B 中的一系列描述。 What I am doing is using the FIND function to look up 'GBP' column and 'LCH ISSUE DESCRIPTION' row.我正在做的是使用 FIND 函数来查找“GBP”列和“LCH 问题描述”行。 I need the column and row number of the two as in Cells('LCH ISSUE DESCRIP.row, GBP.column) I have the info I need and which I am putting in a separate tab using the SPLIT function.我需要两个的列号和行号,如 Cells('LCH ISSUE DESCRIP.row, GBP.column) 我有我需要的信息,我使用 SPLIT 函数将其放在单独的选项卡中。

The issue is that 'LCH ISSUE DESCRIPTION' appears multiple times in col B, which means I have to use FIND function in a loop.问题是“LCH 问题描述”在 col B 中出现多次,这意味着我必须在循环中使用 FIND 函数。 The code works fine for the first instance, but then instead of hitting the subsequent cell (row) containing the same value , it simply moves down one row.该代码在第一个实例中工作正常,但随后不会点击包含相同 value 的后续单元格(行),而是简单地向下移动一行。 Any idea what am I doing wrong?知道我做错了什么吗?

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

I was able to replicate what I think your issue is on my own version of what I think your workbook is.我能够在我自己认为您的工作簿的版本上复制我认为您的问题。 The replicated error made every cell copy over to the "mac_data" tab between the first and last found "LCH ISSUE DESCRIPTION", instead of just the matching cells.复制的错误使每个单元格复制到第一个和最后一个找到的“LCH 问题描述”之间的“mac_data”选项卡,而不仅仅是匹配的单元格。

I was able to fix it by changing Set issuerFound = Worksheets(shName).Range("B:B").FindNext(After:=issuerFound) to Set issuerFound = Worksheets(shName).Range("B:B").Find(What:=issuer, After:=issuerFound, LookAt:=xlWhole)我能够通过将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)

The full code looks like this:完整代码如下所示:

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

Hopefully this works for you!希望这对你有用!

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

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