vba excel 2003复制范围查找并粘贴所选范围

时间:2014-12-15 20:25:43

标签: excel excel-vba vba

我正在尝试在Excel工作表中编写一个宏。在这个宏中,我是否获取单元格编号(B04)的副本并在工作表(3)上搜索,但我遇到的问题是当我想要更改单元格宏的内容时也在搜索新的含量


Range("D6").Select
Selection.Copy
Sheets("3").Select
Cells.Find(What:="Yasser Arafat Ateya ELsayed EL", After:=ActiveCell, _
    LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range("B6").Select
Application.CutCopyMode = False
Selection.ShowDetail = True

End Sub

1 个答案:

答案 0 :(得分:0)

继续使用代码流程,并通过一些解释来帮助您,将代码修改为类似的代码。还要了解如何在代码中明确引用单独的工作表,而不是依赖于ActiveSheet。

Sub findSomeText()
'the find method returns a Range so define a variable to catch this
Dim fndrng As Range
'to search for different strings use a String variable
Dim srchStr As String

'get the srchStr (in this case from D6 in the ActiveSheet)
srchStr = ActiveSheet.Range("D6").Value
'use Sheets("3") - you don't need to select it
'note that this means a Sheet with a (Tab)NAME of "3", not necessarily the third sheet
    With Sheets("3")
        'explicitly set the ActiveCell to Find from
        Range("A1").Activate
        'apply the Find method, note use LookIn:=xlValues; use of srchStr and .Cells
        Set fndrng = .Cells.Find(What:=srchStr, After:=ActiveCell, _
            LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    End With
        'check if a match has been found
        If Not fndrng Is Nothing Then
            'your code if srchStr has been found goes here e.g. found value into cell B6
            Range("B6").Value = fndrng.Value
            ' perhaps use MsgBox "Found" to test?
        Else
            'your code if srchStr has NOT been found goes here
            MsgBox "Not Found"
        End If

End Sub