[英]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.