这里是:我的第一篇文章,我不是程序员,但我希望你们能帮忙!
我有一个300个单元格的列表,它们都引用了单元格文本中包含的书籍的特定章节。每个单元格的内容各不相同,但我需要提取的内容始终采用以下格式:
“......可以在A01章:蓝调的历史中找到。”
“......可以在D27章:蓝调的众多面孔中找到。”
我想只提取章节编号文本“A01”或“D27”等,并将其复制到与找到文本的位置相邻的单元格中。
在单元格中,我要复制的章节编号前面总是以“章节”和空格开头,后面跟着一个冒号。
我一直在论坛上搜索了几个小时,你们都已经帮我弄清楚如何使用VB来查找,复制和粘贴精确的文本匹配到另一个单元格,我确实找到了这个答案,看起来很有前途,但我无法弄清楚如何修改细节以使其适合我!
Copy part of cells that varies in both length and content using VBA
感谢你们给我的任何帮助!!
答案 0 :(得分:2)
如果您的文字在单元格A1中,则将此公式粘贴到B1:
=MID(A1,SEARCH("Chapter ",A1)+8,3)
然后你可以将B1复制到B2:B100以便在那里提取。
答案 1 :(得分:0)
这是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
答案 2 :(得分:0)
虽然其他人所说的是真的(你真的不需要VBA宏),但这是有效的。即使你的章节长度超过三个字符,这也会有用(变量章号文字)长度)。
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