1

我需要一个查找功能来在我的名为“报价单”的工作表的 B 列中搜索以查找“利润调整”,如果它区分大小写,那就太好了。以下是我正在使用的代码,但我无法获得正确的范围或措辞。任何帮助表示赞赏。

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
4

3 回答 3

1

我并不清楚您是否只想找到 的第一次出现Profit Adjustment,或者您是否关心所有出现。如果要查找包含的所有行,则以下宏将按原样工作。如果您只想找到第一次出现,那么只需取消注释该行。Column BProfit AdjustmentExit 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

搜索前: 在此处输入图像描述

搜索后:

在此处输入图像描述

于 2013-02-22T18:06:53.423 回答
1
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
于 2013-02-22T17:29:24.463 回答
1

我会像这样重写你的例子:

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. 您从未使用过rfoundRange 对象,因此我将其删除。
  2. Activate并且Select不需要,甚至可能会减慢您的代码。
  3. 请记住,它Find会返回一个 Range 对象,稍后在您的代码中可能会有用。
于 2013-02-22T17:15:10.927 回答