【问题标题】:Find and Copy varying text to an adjacent cell查找不同的文本并将其复制到相邻的单元格
【发布时间】:2016-08-12 22:44:52
【问题描述】:

这里是:我的第一篇文章,我不是程序员,但我希望你们能提供帮助!

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

“……可以在第 A01 章:蓝调的历史中找到。”

“……可以在第 D27 章:蓝调的众多面孔中找到。”

我想只提取章节编号文本“A01”或“D27”等,并将其复制到与文本所在位置相邻的单元格中。

在单元格中,我要复制的章节编号总是以“Chapter”一词和一个空格开头,后面总是有一个冒号。

我已经在论坛上搜索了几个小时,你们都已经帮我弄清楚了如何使用 VB 来查找、复制和粘贴精确匹配的文本到另一个单元格,我确实找到了这个答案,看起来很有希望,但我不知道如何修改细节以使其适合我!

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

感谢您能给我的任何帮助!

【问题讨论】:

  • 你应该在那个范围内循环(见:excel-easy.com/vba/examples/loop-through-defined-range.html),如果 Instr(cell.value, "can be found in Chapter ") 0 那么你操作的是单元格字符串。 value 以提取 cell.value 的下 3 个字符并将它们写入 cell.offset(1,0)。字符串操作:excel-easy.com/vba/string-manipulation.html。一步一步地发布您的进度(编辑您的问题),我们可以帮助调试!
  • 我认为你并不需要 VBA。在您的字符串中找到“章节”的公式,然后删掉后面的相关文本应该做同样的事情。 MID(A1,FIND("章节",A1,1)+8,3)
  • 你们都很棒。这个解决方案,以及下面使用 SEARCH 的解决方案只给了我章节编号的前两个字符,所以我只是将 3 更改为 4,它工作得很好。谢谢!!!

标签: vba excel


【解决方案1】:

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

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

然后您可以将 B1 复制到 B2:B100 以在那里提取。

【讨论】:

    【解决方案2】:

    这是 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
    

    【讨论】:

      【解决方案3】:

      虽然其他人所说的是正确的(您实际上并不需要 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多