查找并将不同的文本复制到相邻的单元格

这里是:我的第一篇文章,我不是一个程序员,但我希望你们可以帮忙!

我有一个300个单元格的列表,它们都引用了单元格文本中包含的一本书的特定章节。 每个单元格的内容各不相同,但我需要提取的内容始终采用以下格式:

“……可以在第A01章:布鲁斯的历史中find。”

“…可以在D27章节find:蓝调的许多面孔”

我只想提取章节号码文本“A01”或“D27”等,并将其复制到发现文本的相邻单元格中。

在单元格中,我想要复制的章节号总是在单词“章节”和空格之后,并且总是跟着一个冒号。

我一直在search论坛几个小时,你们都已经帮助我弄清楚如何使用VB来查找,复制和粘贴确切的文本匹配到另一个单元格,我find了这个答案,看起来很有前途,但我不知道如何修改细节,使其为我工作!

使用VBA复制部分长度和内容都不相同的单元格

感谢你给与我的帮助!!

如果您的文本在单元格A1,然后将此公式粘贴到B1:

=MID(A1,SEARCH("Chapter ",A1)+8,3) 

然后,您可以将B1复制到B2:B100以从中提取。

这是VBA代码。 我假设你的数据在Range("A1:A300")

 Sub ExtractChapter() Dim Data As Range, RowData As Long, Text As String, Chapter As String Set Data = Range("A1:A300") 'Change this depends on your data located RowData = Data.Rows.Count For i = 1 To RowData Text = Data(i) Chapter = Mid$(Text, InStr(Text, ":") - 3, 3) Data(i).Offset(0, 1) = Chapter Next End Sub 

虽然别人的话是真实的(你并不真的需要一个VBAmacros),这是有用的东西。即使你的章节长度超过三个字符(可变章节号文本长度),这也能工作。

 Sub SEFindStrings() Dim searchRange As range 'wherever your search range is, modify. Mine started at B4 so I used that as the start 'cell and in my Cells method I used "B". Change so it fits your data Set searchRange = range("B4", Cells(Rows.count, "B").End(xlUp)) Dim myCell As range Dim locationStart As Integer Dim locationEnd As Integer Dim keyWords As String keyWords = "can be found in Chapter " 'Goes through, and if the cell isn't empty it finds the phrase 'can be found in Chapter ' 'and then gets the next three characters. For Each myCell In searchRange If myCell.Value <> "" Then locationStart = InStr(myCell.Value, keyWords) If locationStart <> 0 Then locationEnd = InStr(locationStart, myCell.Value, ":") - 1 myCell.Offset(0, 1).Value = (Mid(myCell.Value, _ (locationStart + Len(keyWords)), locationEnd - Len(keyWords))) End If End If Next myCell End Sub