2016-08-12 52 views
0

這裏是:我的第一篇文章,我不是程序員,但我希望你們可以幫忙!查找和複製不同的文本到相鄰的單元格

我有一個300個單元格的列表,它們都引用了單元格文本中包含的書籍的特定章節。每個單元格的內容各不相同,但我需要提取的內容始終採用以下格式:

「......可以在第A01章:The Blues的章節中找到。」

「...可以在章節D27:藍調的許多面孔」。

我想僅提取章節號碼文本「A01」或「D27」等,並將其複製到與文本所在位置相鄰的單元格中。

在單元格中,我想要複製的章節編號總是在單詞「章節」和空格之前,並且後面總是跟一個冒號。

我一直在搜索論壇幾個小時,你們都已經幫助我弄清楚如何使用VB來查找,複製和粘貼確切的文本匹配到另一個單元格,並且我確實找到了這個答案,看起來很有前途,但我無法弄清楚如何修改細節以使其適用於我!

Copy part of cells that varies in both length and content using VBA

感謝任何幫助,您可以給我!

+2

您應該在該範圍內循環(請參閱:http://www.excel-easy.com/vba/examples/loop-through-defined-range.html),並且如果Instr(cell.value, 「可以在章節中找到)<> 0然後你操作cell.value的字符串來提取cell.value的下3個字符並將它們寫入cell.offset(1,0)。字符串操作:http://www.excel-easy.com/vba/string-manipulation.html。一步一個腳印,隨時發佈您的進度(編輯您的問題),我們可以幫助調試! –

+1

我認爲你並不需要VBA。在字符串中找到'章節'的公式,然後刪除相關的文本後面的內容應該做同樣的事情。 MID(A1,FIND(「Chapter」,A1,1)+8,3) –

+0

你們都很棒。這個解決方案,以及下面使用SEARCH的那個只給出了章節編號的前兩個字符,所以我只是將3更改爲4,並且它的工作非常完美。謝謝!!! – KellyR

回答

2

如果您的文本是在單元格A1,然後這個公式粘貼到B1:

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

然後,您可以B1複製到B2:B100,以藏漢提取那裏。

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 
0

雖然別人說的是真實的(你並不真的需要一個VBA宏本),這裏的東西,works.This將工作,即使你的章是超過三個字符(可變章數字文字長度)。

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 
相關問題