需要查找function来search一张纸的一列

我需要一个find函数来在我的工作表的B列中search“Quote Sheet”来find“Profit Adjustment”,如果区分大小写的话,那将会很好。 下面是我正在使用的代码,但我无法得到正确的范围或措辞。 任何帮助表示赞赏。

Dim rFound As Range Set rFound = Range("B10:B1000") Cells.Find(What:="Profit Adjustment", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True).Activate ActiveCell.Offset(0, 8).Select Selection.Copy Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False 

 Sub samPle() Dim rFound As Range, cellToFind As Range Set rFound = Sheets("Quote Sheet").Range("B10:B1000") Set cellToFind = Cells.Find(What:="Profit Adjustment", MatchCase:=True) If Not cellToFind Is Nothing Then cellToFind.Activate ActiveCell.Offset(0, 8).Copy ActiveCell.Offset(0, 8) End If End Sub 

我会重新写你的例子是这样的:

 Sub copy_paste_example() Dim c As Range With ActiveWorkbook.Sheets("Quote Sheet") Set c = .Columns("B").Find(What:="Profit Adjustment", _ LookIn:=xlFormulas, LookAt:=xlPart, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, MatchCase:=True) On Error GoTo NotFoundErr: c.Offset(0, 8).Value = c.Value End With Exit Sub NotFoundErr: Debug.Print "value not found" End Sub 

笔记:

  1. 你从来没有使用过的范围对象,所以我删除它。
  2. ActivateSelect不需要,甚至可能会减慢你的代码。
  3. 请记住, Find返回一个Range对象,这个对象稍后可以在代码中使用。

我不清楚你是否只想findProfit Adjustment的第一个发生,或者如果你关心所有事件。 如果要查找Column B中包含Profit Adjustment 所有行,则下面的macros将按原样运行。 如果您只想查找第一个匹配项 ,则只需取消注释“ Exit For的行Exit For

代码如下:

 Sub FindValueInColumn() Dim rowCount As Integer, currentRow As Integer, columnToSearch As Integer Dim searchString As String Dim quoteSheet As Worksheet Set quoteSheet = ActiveWorkbook.Sheets("Quote Sheet") searchString = "Profit Adjustment" 'string to look for columnToSearch = 2 '2 represents column B rowCount = quoteSheet.Cells(Rows.Count, columnToSearch).End(xlUp).Row For currentRow = 1 To rowCount If Not Cells(currentRow, columnToSearch).Find(What:=searchString, MatchCase:=True) Is Nothing Then Cells(currentRow, 8).Value = Cells(currentRow, columnToSearch).Value 'uncomment the below "Exit For" line if you only care about the first occurrence 'Exit For End If Next End Sub 

search前: 在这里输入图像描述

search后:

在这里输入图像说明