简体   繁体   English

搜索文本字符串,复制找到的文本并粘贴到新单元格中

[英]Search for text string, copy the found text and paste into a new cell

I currently have 1 column, whereby in 1 particular cell I have 3/4 text strings separated by line breaks.我目前有 1 列,因此在 1 个特定单元格中,我有 3/4 个文本字符串,由换行符分隔。

ABC12345 ABC12345

DEF12345 DEF12345

XYZ12345 XYZ12345

QWE12345 QWE12345

I basically just want to search that cell (A1) for the text starting ABC and copy paste into cell A2.我基本上只想在该单元格 (A1) 中搜索以 ABC 开头的文本并将粘贴复制到单元格 A2 中。

Then search for the DEF text and copy and paste into cell A3.然后搜索 DEF 文本并复制并粘贴到单元格 A3 中。

The text will change each time I run the VBA but the first 3 characters will always remain the same (ABC) which is why I want to use that as the search basis.每次运行 VBA 时,文本都会更改,但前 3 个字符将始终保持不变(ABC),这就是我想将其用作搜索基础的原因。

Sub Hi()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim Target, Arr
Dim i As Long, x As Long

Target = Array("ABC", "DEF")
Arr = Split(ws.Range("A1"), Chr(10))

For i = LBound(Target) To UBound(Target)
    For x = LBound(Arr) To UBound(Arr)
        If Left(Arr(x), 3) = Target(i) Then
            ws.Range("A" & i + 2) = Arr(x)
        End If
    Next x
Next i

End Sub

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

相关问题 VBA Excel通配符字符串搜索,复制并粘贴到新单元格 - VBA Excel wildcard string search, copy and paste to new cell Excel - 在一行中搜索特定文本,然后将该值复制并粘贴到特定单元格中 - Excel - search for specific text in a row then copy and paste that value into a specific cell 从文本框中复制文本并粘贴到新工作表的单元格中 - Copy text from textbox and paste into cell of new sheet 将单元格数据复制为字符串并粘贴到新单元格中 - Copy cell data as string and paste into new cell 在项目名称中搜索文本字符串,并将分配的编号粘贴到其他单元格中 - Search for text string in item name and paste assigned number in other cell VBA EXCEL 在列中的文本字符串与不同工作表上的不同列中搜索匹配项。 如果找到匹配项,复制并粘贴 - VBA EXCEL Search for matches in a string of text in a column vs a different column on a different sheet. If match found, Copy & Paste Excel VBA-搜索范围-如果单元格包含文本,则复制单元格-粘贴偏移量2,1 - Excel VBA - Search Range - If Cell Contains Text Then Copy Cell - Paste Offset 2,1 当单元格值是特定文本时复制并粘贴 - Copy and paste when cell value is a certain text VBA:按单元格中的特定文本复制和粘贴 - VBA: copy and paste by specific text in a cell 根据旁边单元格中的文本复制和粘贴数据 - Copy and paste data based on text in cell next to it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM