【问题标题】:vba excel 2003 copy range find and paste the selected rangevba excel 2003 复制范围查找并粘贴所选范围
【发布时间】:2014-12-15 20:25:42
【问题描述】:

我正在尝试在 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

结束子

【问题讨论】:

  • 你能稍微改一下你的问题吗?我很困惑。当您说复制单元格 (B04) 时,您在代码中复制了“D6”。你能把它分解成几个步骤,就好像你是手动做的,一次一步,然后用它来修改你的问题?留下代码,它非常有帮助。就像,第 1 步:检查 sheet("3").Range("B6")。第2步:从中获取价值。第 3 步:在 Sheet("4") 中搜索它的值,或类似的东西。

标签: excel vba


【解决方案1】:

按照您的代码流程,并提供一些帮助您的解释,将您的代码修改为类似的内容。还要了解如何在代码中明确引用单独的工作表,而不是依赖 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    相关资源
    最近更新 更多