首页 > 解决方案 > 如何返回字符串的一部分?

问题描述

我有这些细胞:

单元格中的字符串
在此处输入图像描述

我想将这些字符串的特定部分——“...bar”和“also csatlakozas”或“hatso csatlakozas”——返回到其他单元格。

标签: excelvbastring

解决方案


试试下面的代码,这应该让你开始:

Sub ExtractMatchingCells()
    'Define in which column data is located
    col = 1
    lastRow = Cells(Rows.Count, col).End(xlUp).Rows
    
    For i = 1 To lastRow
        ' Extract cell value to cell to the right
        If InStr(0, Cells(i, col), "bar (also csatlakozas)") > 0 Or InStr(0, Cells(i, col), "bar (hatso csatlakozas)") Then
            Cells(i, col + 1) = Cells(i, col)
        End If
    Next
End Sub

推荐阅读