读取列中的单词并复制单元格

时间:2013-11-05 06:29:12

标签: vba excel-vba excel

我有一个代码在B列中查找单词“it”。然后,如果代码找到它,它会将单元格右边的单元格复制到另一个工作表。有谁知道如何修改它,所以它读取B列中的第二个“它”,而不是第一个?谢谢

Dim rcell As Range
Application.ScreenUpdating = False
For Each rcell In Range("B2:B" & ActiveSheet.UsedRange.Rows.count + 1)
If rcell.Value = "it" Then
    rcell.Offset(, 1).Copy Sheets("another sheet").Range("C" & Rows.count).End(3)(2)
    End If
Next rcell
Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:0)

试试这个:

Dim rcell As Range
Application.ScreenUpdating = False
For Each rcell In Range("B2:B" & ActiveSheet.UsedRange.Rows.count + 1)
Dim place%
'find first instance of "it"
place = InStr(1,rcell.Value, "it")
If place <> 0 Then
    'if there is one, check to see if there is a second instance
    'line below may have to use place+1 instead of just place
    If InStr(place,rcell.Value, "it") <> 0 Then
        rcell.Offset(, 1).Copy Sheets("another sheet").Range("C" & Rows.count).End(3)(2)
    End If
End If
Next rcell
Application.ScreenUpdating = True
End Sub