在单元格中查找特定文本

时间:2018-04-12 21:57:35

标签: excel-vba vba excel

我是这个VBA for excel的新手,无论如何我试图编写一些代码来迭代数据集,直到它出现在特定的文本中,在我的例子中,我想找到这个名字" FRANKS"所以我写了一些代码来做到这一点并且它有效,现在我试图弄清楚如何停止在那个单元格上,复制它并将它附加到它旁边的单元格。长话短说,我有一份报告,每周运行一次,当某些员工的名字被拆分(文本到列)时,有些名字不会出现。所以我必须手动修复它们。所以我在这里尝试自动化它。 下面是我到目前为止编写的代码。

      row_number = 0

 Do
 DoEvents
 row_number = row_number + 1
 namedperson = Range("C" & row_number)
 Loop Until namedperson = "FRANKS"
 Columns("C & row_number").Activate

最后一行不起作用,但你知道我想做什么。 无论如何,在我拿到这个名字" FRANKS"它左边的列是我需要附加" FRANKS"到.....

提前致谢, 莱尼

3 个答案:

答案 0 :(得分:1)

无需循环。

Dim i As Long
With Worksheets("Sheet1") ' change to your sheet
    On Error Resume Next
        i = Application.WorksheetFunction.Match("Franks", .Range("C:C"), 0)
    On Error GoTo 0

    If i <> 0 Then
        MsgBox "Franks found at " & .Cells(i, 3).Address(0, 0)
    Else
        MsgBox "Franks not found in Column"
    End If
End With

答案 1 :(得分:0)

如果值不存在,循环直到找到某个值不是一个好主意。我建议循环你的范围,如果你找到了值,将它附加到左边的单元格。你没有说你是否想要在第一次出现“FRANKS”时停下来,所以我的代码会继续存在以防万一。有不同的方式来引用单元格,但这应该会给你一个想法。

row_count = 7  ' the number of rows in your range
For r = 1 To row_count
    If Cells(r, 3) = "FRANKS" Then
        Cells(r, 2) = Cells(r, 2) & "FRANKS"
    End If
Next r

答案 2 :(得分:0)

您可以使用Range.Find Method

lon_bounds = new_lon[:]  # copy
lon_bounds[lon_bounds > 180] -= 360
ax.set_extent([np.min(lon_bounds), np.max(lon_bounds), np.min(lat_post), np.max(lat_post)])

要在Sub Sample() Dim StringToSearch As String Dim aCell As Range StringToSearch = "FRANK" With Sheets("Sheet1").Columns(3) '<~~ Change as applicable Set aCell = .Find(What:=StringToSearch, _ LookIn:=xlValues, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False) If Not aCell Is Nothing Then MsgBox "Found at " & aCell.Address Else MsgBox "Nothing found" End If End With End Sub 中部分搜索Frank示例,请将frankenstein更改为LookAt:=xlWhole

对于区分大小写的搜索,请将LookAt:=xlPart更改为MatchCase:=False

相关问题