简体   繁体   English

使用 excel 中的 vba 从“查找和替换”框或搜索框中复制和导出数据

[英]Copy & Export Data from 'Find and Replace' box or searchbox using vba in excel

Actually I dont have any spesific data to referred, only just wonder if it can be done to export the data to another workbook (whether open or closed) by click on selected row in searchbox from active workbook.实际上我没有参考任何特定的数据,只是想知道是否可以通过单击活动工作簿中搜索框中的选定行将数据导出到另一个工作簿(无论是打开的还是关闭的)。

Let's says, the data found in active workbook searchbox is in Row 7, then when click on it (in searchbox) so the data will be exported to another workbook.假设在活动工作簿搜索框中找到的数据位于第 7 行,然后单击它(在搜索框中),数据将导出到另一个工作簿。 Not necessery to export all the data, maybe need it in Cell D7, F7 & K7 only to export.不需要导出所有数据,可能只需要在 Cell D7、F7 和 K7 中导出。 And target workbook will filled with the exported data to B4(D7), G8(F7) & L9(K7).目标工作簿将填充导出到 B4(D7)、G8(F7) 和 L9(K7) 的数据。

Can it be done using VBA codes?可以使用 VBA 代码来完成吗? Maybe have to created Userform as a 'Find and Replace' box?也许必须将用户窗体创建为“查找和替换”框? Maybe there is another way can make it done?也许还有另一种方法可以做到这一点? Thank you very much in advance if you accept this challenge and wish you good luck.如果您接受此挑战并祝您好运,请提前非常感谢您。

Post code here:邮政编码在这里:

Yes, it can be done using VBA.是的,可以使用 VBA 来完成。 This can get you started.这可以让你开始。

Example例子

This example finds all cells in the range A1:A500 on worksheet one that contain the value 2, and changes it to 5.此示例在第一个工作表上查找范围 A1:A500 中包含值 2 的所有单元格,并将其更改为 5。

With Worksheets(1).Range("a1:a500") 
    Set c = .Find(2, lookin:=xlValues) 
    If Not c Is Nothing Then 
        firstAddress = c.Address 
        Do 
            c.Value = 5 
            Set c = .FindNext(c) 
        Loop While Not c Is Nothing
    End If 
End With

Look here for source and more: https://docs.microsoft.com/en-us/office/vba/api/excel.range.find在此处查找来源和更多信息: https://docs.microsoft.com/en-us/office/vba/api/excel.range.find

Private Sub ListBox_Results_Click()
Dim strAddress As String
Dim strSheet As String
Dim strCell As String
Dim l As Long
Dim lLastRow As Long
Const sRESULTS As String = "Results Sheet" 
For l = 0 To ListBox_Results.ListCount
If ListBox_Results.Selected(l) = True Then
strAddress = ListBox_Results.List(l, 1)
strSheet = Replace(Mid(strAddress, 1, InStr(1, strAddress, "!") - 1), "'", "")
Worksheets(strSheet).Select
Worksheets(strSheet).Range(strAddress).Select
 With Worksheets(sRESULTS) lLastRow =.Range("A" &.Rows.Count).End(xlUp).Row + 1.Range("A" & lLastRow).Value = Worksheets(strSheet).Range(strAddress).Value.Range("B" & lLastRow).Value = strAddress.Range("C" & lLastRow).Value = Now
End With
GoTo EndLoop
End If
Next l
EndLoop:
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM