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

vba excel 2003复制范围查找并粘贴选定范围,excel,vba,Excel,Vba,我正在尝试在excel工作表中编写宏。在该宏中,我是否需要复制单元格编号B04并在工作表3上搜索,但我遇到的问题是,当我要更改单元格宏的内容时,也在搜索新内容 End Sub按照代码的流程进行操作,并对代码进行一些解释,以帮助您将代码修改为以下内容。还要了解如何在代码中显式引用单独的工作表,而不是依赖ActiveSheet Sub findSomeText() 'the find method returns a Range so define a variable to catch this

我正在尝试在excel工作表中编写宏。在该宏中,我是否需要复制单元格编号B04并在工作表3上搜索,但我遇到的问题是,当我要更改单元格宏的内容时,也在搜索新内容


End Sub

按照代码的流程进行操作,并对代码进行一些解释,以帮助您将代码修改为以下内容。还要了解如何在代码中显式引用单独的工作表,而不是依赖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

你能把你的问题改写一下吗?我很困惑。当您说复制单元格B04时,代码中复制了D6。你能把它分解成几个步骤吗,就像你是手工做的一样,一步一步,然后用它修改你的问题?保留代码,这很有帮助。例如,第1步:检查表3.6。步骤2:从中获取值。第三步:搜索表4中的值,或类似的内容。
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