在列中搜索带前缀的单词,并将带有前缀的整个单词复制到同一工作表上的另一列

时间:2017-02-13 12:44:19

标签: excel vba excel-vba

我下载一个包含多个Row和无尽列的excel文件。

在特定的行中,我们有数据,其中每个Cell都包含特定产品的详细信息,按Alt + Enter分隔。

我必须通过复制粘贴来提取2-3个此类描述的数据,以便将产品与巨大的列表分开。

例如: -

_A_______B____________C____D___E___F___G______H_________________________________
   |  Product     | Range |A | B | C |D |description|....
________________________________________________________________________________
1  |  Apple       | R 1   |A1| B1| C1|D1| Description1
                                          Description2
                                          Description3
                                          Description4
________________________________________________________________________________
2  |  ball        | R 1   |A1| B1| C1|D1| Description1
                                          Description2
                                          Description3
                                          Description4

从上面的示例中,My Requirment将使用Say DLL的前缀复制描述详细信息:123456或LLM:654321并将其复制到下一行。

这将有助于将产品与特定描述分开。

2 个答案:

答案 0 :(得分:0)

如果您正在寻找:

APPLE A B C D E F G H 2460 APPLE:2460

                                    6521    APPLE : 6521
                                    4532    APPLE : 4532
                                    3021    APPLE : 3021
                                    1234    APPLE : 1234

BALL 6521 BALL:6521                                         4532球:4532                                         3021球:3021                                         1234 BALL:1234

然后创建一个列并使用此公式。您也可以使用自己的前缀

= IF(ISBLANK(a2),LEFT(a1,(SEARCH(“:”,a1))&“”& l2),a2&“:”& l2)

答案 1 :(得分:0)

基于我猜你需要的东西:这可能会让你开始:

Sub Example1()
Dim rowArray() As Variant, rowArrayCounter As Long
Dim myStringArray, itemThatIwant As String, rowItIsIn As Long

'   The following code will find all instances of "LLM: 654321" in column "F"
'   It places the row number of each one into an array called rowArray()
'   and places the value of that item into a variable called itemThatIwant

'   The "split" function assumes that you enttered each list into a single cell
'   by using alt-enter to put them on individual lines within that cell.  If so, then 
'   the split delimiter would be chr(10), as below.  Otherwise it will probably be one space
'   but you will need to find the correct delimiter for this to work.

rowArrayCounter = 1
ReDim rowArray(1 To 1)
With Worksheets(1).Range("F1:F250")
    Set c = .Find("LLM: 654321", LookIn:=xlValues)' this text is what you change
    If Not c Is Nothing Then
        firstAddress = c.Address
        Do
            myString = Split(c.Value, Chr(10)) ' split cell list into separate items
            For i = LBound(myString) To UBound(myString)
                If Left(myString(i), 11) = "LLM: 654321" Then
                    itemThatIwant = myString(i)
                    rowItIsIn = c.Row
                    ReDim Preserve rowArray(1 To rowArrayCounter)
                    rowArray(rowArrayCounter) = c.Row
                      ' do your events with data
                      ' the entire item (if found) is in the variable itemThatIwant
                    Exit For
                End If
            Next i
            Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> firstAddress
    End If
End With


End Sub