Excel VBA:如何从“工作表”中查找搜索值;DMR";然后从找到的搜索值行中,将A列的单元格和D列的单元格复制到工作表中;搜索“;

Excel VBA:如何从“工作表”中查找搜索值;DMR";然后从找到的搜索值行中,将A列的单元格和D列的单元格复制到工作表中;搜索“;,excel,vba,Excel,Vba,这是我第一次在任何VBA编程网站上寻求帮助。我对VBA编程非常陌生(10年前有过一些经验),我正在尝试为工作创建一个文档交叉引用工具,用户可以轻松搜索文档编号,并查看该文档编号在其他文档中的引用位置。我正在使用Excel 2010 在过去的3天里,我浏览了网站,阅读了同事借给我的Excel VBA programming for dummies(我),这是我目前编写的代码,它成功地找到了所需的查询框,但我似乎无法让搜索查询正常工作,或者复制粘贴命令正常工作 我正在尽最大努力尊重本网站的规则,并展

这是我第一次在任何VBA编程网站上寻求帮助。我对VBA编程非常陌生(10年前有过一些经验),我正在尝试为工作创建一个文档交叉引用工具,用户可以轻松搜索文档编号,并查看该文档编号在其他文档中的引用位置。我正在使用Excel 2010

在过去的3天里,我浏览了网站,阅读了同事借给我的Excel VBA programming for dummies(我),这是我目前编写的代码,它成功地找到了所需的查询框,但我似乎无法让搜索查询正常工作,或者复制粘贴命令正常工作

我正在尽最大努力尊重本网站的规则,并展示我在编写此代码时所做的努力,而不只是让其他人来完成所有工作,但我显然需要帮助:

Private Sub CommandButton1_Click()
Dim rngCell As Range
Dim ws As Worksheet
Dim lngLstRow As Long
Dim lngLstCol As Long
Dim strSearch As String
Dim r As Long
Dim x As Variant

strSearch = InputBox("Please enter 5 digit document number to search for (e.g. 00002):", "Search Value")

Sheets("DMR").Select
'Loop through sheet DMR and search for "search value". The search value may be in several rows, but will only appear once in a row.
For r = 1 To endRow
x = Range("G3:EP7002").Value 'yes-there are 7002 rows of data all starting at column G and potentially ending at column EP. There are many blank cells.
If Cells(r, x).Value = "Search Value" Then

'Copy the cells at column A and D of found search value row in Sheet "DMR"
Range(Cells(r, "A"), Cells(r, "D")).Select
Selection.Copy

'Switch to sheet "SEARCH" & paste two cells from sheet "DMR" into sheet "SEARCH" cells A5:B5
Sheets("SEARCH").Select
Range(r, "A5:B5").Select
ActiveSheet.Paste

'Next time you find a match in sheet "DMR", it will be pasted in the next row on sheet "SEARCH"
pasteRowIndex = pasteRowIndex + 1

'Switch back to sheet DMR & continue to search for your criteria
Sheets("DMR").Select
    End If
Next r
End Sub
如果有什么我可以提供的,或某种方式传达的信息,我正试图获得更清楚,请不要犹豫问

非常感谢你的耐心


Veronica

由于您只是在搜索是否存在某个值,因此可以使用“查找”功能缩短该代码:


我想这正是你想要的。如果字符串在“DMR”工作表中找到,比如第9行,它将把A9:D9复制到“搜索”工作表中的下一个空行。如果这不是您想要的,请告诉我。

这将在循环中搜索所需的范围(G3:EP7002)以查找所有实例,并将从A5:B5开始将其放入工作表(搜索)。它缺少对user3578951的错误检查,但我让您自己来解决这个问题^_^

Private Sub CommandButton1_Click()

Dim dmr As Worksheet
Dim strSearch As String
Dim f As Variant
Dim fAddress As String
Dim fRow As Long
Dim cellA As Variant
Dim cellB As Variant

Set dmr = Worksheets("DMR")
pasteRowIndex = 5
strSearch = InputBox("Please enter 5 digit document number to search for (e.g. 00002):", "Search Value")

With dmr.Range("G3:EP7002")
    Set f = .Find(strSearch, LookIn:=xlValues)
    If Not f Is Nothing Then
        fAddress = f.Address
        Do
            fRow = f.Row
            cellA = dmr.Cells(fRow, 1).Value
            cellD = dmr.Cells(fRow, 4).Value
            Sheets("SEARCH").Cells(pasteRowIndex, 1) = cellA
            Sheets("SEARCH").Cells(pasteRowIndex, 2) = cellD
            pasteRowIndex = pasteRowIndex + 1
            Set f = .FindNext(f)
        Loop While Not f Is Nothing And f.Address <> fAddress
    End If
End With

End Sub
Private子命令按钮1\u单击()
将dmr设置为工作表
作为字符串的Dim stresearch
作为变体的dimf
朦胧的衣服如细绳
朦胧如长
作为变异体的暗淡细胞
Dim cellB作为变体
设置dmr=工作表(“dmr”)
pasteRowIndex=5
strSearch=InputBox(“请输入要搜索的5位文档编号(例如00002):”,“搜索值”)
具有dmr.范围(“G3:EP7002”)
Set f=.Find(strSearch,LookIn:=xlValues)
如果不是的话,那么f什么都不是
fAddress=f.地址
做
fRow=f.行
cellA=dmr.单元格(fRow,1).值
cellD=dmr.单元格(fRow,4).值
表格(“搜索”)。单元格(粘贴行索引,1)=单元格A
表格(“搜索”)。单元格(粘贴行索引,2)=单元格
pasteRowIndex=pasteRowIndex+1
集合f=.FindNext(f)
循环而非f为Nothing,f为Address
如果结束
以
端接头

对我的请求的最终答复,这非常有效

Private Sub CommandButton1_Click()

Dim dmr As Worksheet
Dim strSearch As String
Dim f As Variant
Dim fAddress As String
Dim fRow As Long
Dim cellA As Variant
Dim cellB As Variant

Worksheets("SEARCH").Range("A5:B200").ClearContents

Set dmr = Worksheets("DMR")
pasteRowIndex = 5
strSearch = InputBox("Please enter 5 digit document number to search for (e.g. 00002):", "Search Value")

If strSearch = vbNullString Then
MsgBox ("User canceled, or did not enter a value.")
Exit Sub
End If

With dmr.Range("G3:EP7002")
    Set f = .Find(strSearch, LookIn:=xlValues)
    If Not f Is Nothing Then
        fAddress = f.Address
        Do
            fRow = f.Row
            cellA = dmr.Cells(fRow, 1).Value
            cellD = dmr.Cells(fRow, 4).Value
            Sheets("SEARCH").Cells(pasteRowIndex, 1) = cellA
            Sheets("SEARCH").Cells(pasteRowIndex, 2) = cellD
            pasteRowIndex = pasteRowIndex + 1
            Set f = .FindNext(f)
        Loop While Not f Is Nothing And f.Address <> fAddress
    End If

If f Is Nothing Then
MsgBox ("The document number you've entered either does not appear in this tool, or is not cross referenced in any other document.")
Exit Sub
End If
End With
End Sub
Private子命令按钮1\u单击()
将dmr设置为工作表
作为字符串的Dim stresearch
作为变体的dimf
朦胧的衣服如细绳
朦胧如长
作为变异体的暗淡细胞
Dim cellB作为变体
工作表(“搜索”).Range(“A5:B200”).ClearContent
设置dmr=工作表(“dmr”)
pasteRowIndex=5
strSearch=InputBox(“请输入要搜索的5位文档编号(例如00002):”,“搜索值”)
如果strSearch=vbNullString,则
MsgBox(“用户已取消,或未输入值。”)
出口接头
如果结束
具有dmr.范围(“G3:EP7002”)
Set f=.Find(strSearch,LookIn:=xlValues)
如果不是的话,那么f什么都不是
fAddress=f.地址
做
fRow=f.行
cellA=dmr.单元格(fRow,1).值
cellD=dmr.单元格(fRow,4).值
表格(“搜索”)。单元格(粘贴行索引,1)=单元格A
表格(“搜索”)。单元格(粘贴行索引,2)=单元格
pasteRowIndex=pasteRowIndex+1
集合f=.FindNext(f)
循环而非f为Nothing,f为Address
如果结束
如果f是零,那么
MsgBox(“您输入的文档编号未显示在此工具中,或者未在任何其他文档中交叉引用。”)
出口接头
如果结束
以
端接头

如果没有适当的缩进(例如,缩进for循环的主体),您的代码有点难以阅读,因此很难判断代码的开始和结束位置。另外--您似乎有几个值很神秘的变量(例如endrow)。这些是全局变量吗(通常不推荐)?另外--您似乎声明了一些根本没有使用的其他变量。除了您正在搜索某些东西之外,还不清楚您的代码试图做什么。如果你能更详细地描述你想要完成的事情,可能会更容易。非常感谢。在经历了这么多失败的尝试后,仅仅看到结果就给我带来了快乐!你是说表格搜索?这是我最后的VBA搜索,效果很好:答案左上角上下箭头下方应该有一个复选标记。
Private Sub CommandButton1_Click()

Dim dmr As Worksheet
Dim strSearch As String
Dim f As Variant
Dim fAddress As String
Dim fRow As Long
Dim cellA As Variant
Dim cellB As Variant

Worksheets("SEARCH").Range("A5:B200").ClearContents

Set dmr = Worksheets("DMR")
pasteRowIndex = 5
strSearch = InputBox("Please enter 5 digit document number to search for (e.g. 00002):", "Search Value")

If strSearch = vbNullString Then
MsgBox ("User canceled, or did not enter a value.")
Exit Sub
End If

With dmr.Range("G3:EP7002")
    Set f = .Find(strSearch, LookIn:=xlValues)
    If Not f Is Nothing Then
        fAddress = f.Address
        Do
            fRow = f.Row
            cellA = dmr.Cells(fRow, 1).Value
            cellD = dmr.Cells(fRow, 4).Value
            Sheets("SEARCH").Cells(pasteRowIndex, 1) = cellA
            Sheets("SEARCH").Cells(pasteRowIndex, 2) = cellD
            pasteRowIndex = pasteRowIndex + 1
            Set f = .FindNext(f)
        Loop While Not f Is Nothing And f.Address <> fAddress
    End If

If f Is Nothing Then
MsgBox ("The document number you've entered either does not appear in this tool, or is not cross referenced in any other document.")
Exit Sub
End If
End With
End Sub