简体   繁体   English

在单元格中找到单词

[英]Find word into cell

Excel - VBA I was wondering how to find a word into a Excel range of rows using VBA. Excel-VBA我想知道如何使用VBA在Excel的行范围内查找单词。 Ex. 例如 "word to be found", this is not just the cell value but a word into a string. “要查找的单词”,这不仅是单元格值,还是单词到字符串中。 For instance, the way to find the word "network" into the string "Need help to map network printer". 例如,在字符串“需要帮助来映射网络打印机”中找到单词“ network”的方法。

Sub SearchForSfb()

   Dim LSearchRow As Integer
   Dim LCopyToRow As Integer

   On Error GoTo Err_Execute

   'Start search in row 1
   LSearchRow = 1

   'Start copying data to row 2 in Open (row counter variable)
   LCopyToRow = 2

   While Len(Range("E" & CStr(LSearchRow)).Value) > 0

      'If value in column E = "word to be found", copy entire row to Open
      If Range("E" & CStr(LSearchRow)).Value = "word to be found" Then

         'Select row in Data to copy
         Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
         Selection.Copy

         'Paste row into SFB in next row
         Sheets("SFB").Select
         Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
         Sheets("SFB").Paste

         'Move counter to next row
         LCopyToRow = LCopyToRow + 1

         'Go back to Data to continue searching
         Sheets("Data").Select

      End If

      LSearchRow = LSearchRow + 1

   Wend

   'Position on cell A3
   Application.CutCopyMode = False
   Range("A3").Select

   MsgBox "All matching data has been copied."

   Exit Sub

Err_Execute:
   MsgBox "An error occurred."

End Sub

Use a simple loop 使用一个简单的循环

Sub Button1_Click()
    Dim ws As Worksheet
    Dim sh As Worksheet
    Dim lstRw As Long
    Dim rng As Range
    Dim s As String
    Dim c As Range

    s = "* network *"

    Set ws = Sheets("Data")
    Set sh = Sheets("SFB")

    With ws
        lstRw = .Cells(.Rows.Count, "E").End(xlUp).Row
        Set rng = .Range("E2:E" & lstRw)
    End With

    For Each c In rng.Cells
        If c.Value Like s Then
            c.EntireRow.Copy sh.Cells(sh.Rows.Count, "A").End(xlUp).Offset(1)
        End If
    Next c


End Sub

Or you can use a filter macro 或者您可以使用过滤器宏

Sub FiltExample()
    Dim ws As Worksheet, sh As Worksheet
    Dim rws As Long, rng As Range
    Set ws = Sheets("Data")
    Set sh = Sheets("SFB")

    Application.ScreenUpdating = 0

    With ws
        rws = .Cells(.Rows.Count, "E").End(xlUp).Row
        .Range("E:E").AutoFilter Field:=1, Criteria1:="=*network*"
        Set rng = .Range("A2:Z" & rws).SpecialCells(xlCellTypeVisible)
        rng.EntireRow.Copy sh.Cells(sh.Rows.Count, "A").End(xlUp).Offset(1)
        .AutoFilterMode = False
    End With

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找单元格中单词出现的次数 - Find number of occurrence of a word in a cell 在单元格中找到一个单词,然后用新单词替换整个单元格? - Find a Word within Cell, then Replace Entire Cell with New Word? Excel:在单元格中找到一个单词,然后返回前一个单词和找到的单词 - Excel: find a word in a cell, then return the preceding word and the found word excel:从单词列表中查找单元格中的单词,然后返回该单词 - excel: Find a word in a cell from a list of word then return that word 在excel中查找单元格中重复单词的频率和位置 - Find the frequency and location of a recurring word in a cell in excel 在Excel单元格中查找单词和其他字符 - Find word plus additional characters in Excel cell 仅查找和替换单元格中单词的第一个实例 - Find and Replace only the first instance of a word in a cell VBA - 替换为在特定单元格中查找一个词有效,我如何找到一个词或另一个词? - VBA - Replace with find one word in a specific cell works, how do I find one word or another? 激活/选择一个word文档并找到该文档中活动单元格的值 - Activate/select a word-document and find the value of active cell in that document 在单元格中找到一个特定的单词,并将其旁边的行复制到另一个工作表 - Find a specific word in a cell and copy the rows next to it to another worksheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM