简体   繁体   English

搜索特定的文本,比较,然后在VBA中复制和粘贴

[英]Search Specific Text, Compare, and then Copy & Paste in VBA

In Excel, 在Excel中,

I have a bunch of information on Sheet1/column A and would like to search specific text (starting from the word N1*PE* up to the 9 numbers before "~N"), 我在Sheet1 / A栏上有大量信息,并且想搜索特定的文本(从单词N1 * PE *到“〜N”之前的9个数字),

And then compare (Sheet1/Column A) from the correct list (Sheet2/Column A) & then paste it on a separate worksheet (Sheet3/Column A). 然后从正确的列表(Sheet2 / A列)中比较(Sheet1 / A列),然后将其粘贴到单独的工作表(Sheet3 / A列)上。

Here's an example: 这是一个例子:

In Sheet 1 Column A : (I have WRONG information below) 在工作表1的A列中:(我下面有错误的信息)

EDI DEPARTMENT*TE*2658018518~N1*PE* ELMHUR EDI部* TE * 2658018518〜N1 * PE * ELMHUR

ST CENTER*XX*564824568~N4*GREAT NECK*NY*11023 ST CENTER * XX * 564824568〜N4 * GREAT NECK * NY * 11023

N1*PE COOPER XX*333333333~N4*NEW YORK*NY*10077-5281~REF*TJ*133988001~LX*7111~ N1 * PE 库珀 XX * 333333333〜N4 *纽约* NY * 10077-5281〜REF * TJ * 133988001〜LX * 7111〜

As you noticed, the word ELMHURST is broken. 您已经注意到,单词ELMHURST已损坏。

What I would like to accomplish is to replace the wrong text (on Sheet1/Column A) based from the sample list (On Sheet2/Column A) and paste it on Sheet3/Column A --> using the same format . 我要完成的工作是根据示例列表(在Sheet2 / Column A上)替换错误的文本(在Sheet1 / Column A上),然后使用相同格式将其粘贴到Sheet3 / Column A->上。

Here's the (CORRECT) Sample Information List (Sheet2/Column A): 这是(正确的)样本信息列表(Sheet2 / A列):

N1*PE ELMHURST CENTER XX*454545457 N1 * PE ELMHURST 中心 XX * 454545457

N1*PE COOPER XX*123457777 N1 * PE COOPER XX * 123457777

So, in the result should be: 因此,结果应为:

In Sheet3/Column A... 在Sheet3 / A栏...

EDI DEPARTMENT*TE*2658018518~N1*PE* ELMHUR EDI部* TE * 2658018518〜N1 * PE * ELMHUR

ST CENTER*XX*454545457~N4*GREAT NECK*NY*11023 ST中心* XX * 454545457〜N4 * GREEN NECK * NY * 11023

N1*PE COOPER XX*123457777~N4*NEW YORK*NY*10077-5281~REF*TJ*133988001~LX*7111~ N1 * PE COOPER XX * 123457777〜N4 * NEW YORK * NY * 10077-5281〜REF * TJ * 133988001〜LX * 7111〜

The Code below is incomplete. 以下代码不完整。 As it can only copy and paste on Sheet2 Column A. 因为它只能复制和粘贴在Sheet2列A上。

Option Explicit


Public Sub Transfer()

Dim lngRow As Long, lngWriteRow As Long, strTemp As String

Dim shtRaw As Worksheet, shtNew As Worksheet

'   Initialize

lngWriteRow = 1                     'The row we're writing to

Set shtRaw = Sheets("Sheet1")       'The raw data worksheet

Set shtNew = Sheets("Sheet2")       'The sheet with the concatenated text

For lngRow = 1 To shtRaw.UsedRange.Rows.Count

    If InStr(1, shtRaw.Cells(lngRow, 1), "N1*PE*", vbTextCompare) > 0 Then

'           Grab the end of this cell's text starting at N1*PE*

        strTemp = Mid(shtRaw.Cells(lngRow, 1), InStr(1, shtRaw.Cells
 (lngRow, 1), "N1*PE*", vbTextCompare))

'           Add the start of the next cell's text, up to the ~N


    strTemp = strTemp & Left(shtRaw.Cells(lngRow + 1, 1), InStr(1, shtRaw.Cells(lngRow + 1, 1), "~N", vbTextCompare))


'           Write the concatenated string to the other worksheet
            shtNew.Cells(lngWriteRow, 1) = strTemp

'           NEED TO DO SOMETHING HERE... COMPARE THE TEXT FROM THE LIST AND PASTE IT ON SHEET 3 COLUMN A            

'           Move down one row for the next time we write to the other sheet
        lngWriteRow = lngWriteRow + 1

    End If

Next lngRow

'Sort the NPIs

Sheets("Sheet2").Select

Range("A:A").Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlYes


'   Clean up memory

Set shtRaw = Nothing

Set shtNew = Nothing


End Sub

Many Thanks in advance... 提前谢谢了...

The below may work. 以下可能有效。 Your replacement values seem to have arbitrarily missing * and space characters compared to the originals (ie PE* ELM.. vs PEELM..). 与原始值相比,您的替换值似乎任意*和空格字符(即PE * ELM .. vs PEELM ..)。 That makes it hard to be certain how long each repaired row should be on Sheet3. 这使得很难确定每个修复的行应在Sheet3上保留多长时间。 I made an arbitrary decision it should be the same length as the first of the two cells I'm concatenating for the purpose of this demo, but that method might need some refinement after many rows have been processed. 为了这个演示目的,我做出了一个任意决定,该长度应该与我要连接的两个单元格中的第一个单元格的长度相同,但是在处理了许多行之后,该方法可能需要进行一些改进。 If you need the start characters to match existing you need to do something similar to what I did here and and find the positions of the following row start characters and divide the NewCombinedString accordingly. 如果您需要使起始字符与现有字符匹配,则需要执行与此处相同的操作,并找到接下来的行起始字符的位置,并相应地划分NewCombinedString。

Sub FixSheet1ColumnA()
    Dim i As Integer
    i = 1
    Do While Sheet1.Range("A" & i) <> ""
        'Combined adjoining rows to account for values which overlap between rows
        Dim Cell1Value As String
        Dim Cell2Value As String
        Dim CombinedString As String
        'The upper of the rows should come from whatever has been processed onto
        'Sheet3 except for the very first row which has to come from Sheet1
        If i = 1 Then Cell1Value = Sheet1.Range("A" & i) Else Cell1Value = Sheet3.Range("A" & i)
        Cell2Value = Sheet1.Range("A" & i + 1)
        CombinedString = Cell1Value & Cell2Value
        Dim SearchString As String
        'Strip the * and space characters out of the string to search it as there
        'seem to be random extras of these in Sheet1 column A
        SearchString = Replace(Replace(CombinedString, " ", ""), "*", "")
        'Cycle through Sheet2 column A to see if there are any matches for the
        'first n-9 digits of each value there, also removing * and space characters
        'for consistency
        Dim j As Integer
        j = 1
        Do While Sheet2.Range("A" & j) <> ""
            Dim ReplacementString As String
            ReplacementString = Sheet2.Range("A" & j)
            Dim FindString As String
            FindString = Replace(Replace(ReplacementString, " ", ""), "*", "")
            'determine if the first n-9 characters of the given Sheet2 value are found
            Dim SubStringPosition As Integer
            SubStringPosition = InStr(1, SearchString, Left(FindString, Len(FindString) - 9))
            If SubStringPosition <> 0 Then
                'Find the tilde that immediately precedes the string to be replaced
                Dim FirstTildePosition As Integer
                FirstTildePosition = InStr(SubStringPosition, CombinedString, "~")
                'Find the tilde that follows it
                Dim SecondTildePosition As Integer
                SecondTildePosition = InStr(FirstTildePosition + 1, CombinedString, "~")
                Dim NewCombinedString As String
                NewCombinedString = Left(CombinedString, FirstTildePosition) _
                    + ReplacementString _
                    + Right(CombinedString, Len(CombinedString) - SecondTildePosition + 1)
                Exit Do
            End If
            j = j + 1
        Loop
        'Populate the first part of potentially fixed CombinedString into Sheet3
        If i = 1 Then Sheet3.Range("A" & i) = Left(NewCombinedString, Len(Cell1Value))
        Sheet3.Range("A" & i + 1) = Right(NewCombinedString, Len(NewCombinedString) - Len(Cell1Value))
        i = i + 1
    Loop
End Sub

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

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