简体   繁体   English

Excel VBA查找全部

[英]Excel VBA Find All

As usual, I've wasted hours working on a problem before finally giving in and asking for help, so any help would be greatly appreciated! 像往常一样,在最终屈服并寻求帮助之前,我浪费了很多时间解决问题,因此,非常感谢您的帮助!

I'm running a while loop on Sheet Test1 of my data in order to copy data to a specific row on Sheet Test2 where parameters match, but the way I'm currently doing this is by also looping through every row of Sheet 2 - both sheets of data are over 50,000 lines long, so although my method works perfectly for my 10 row tests it is taking several hours to work for the full data. 我在我的数据的表格Test1上运行一个while循环,以便将数据复制到参数匹配的表格Test2上的特定行,但是我目前这样做的方式是也循环遍历表格2的每一行-两者数据表的长度超过50,000行,因此,尽管我的方法对于10行测试非常有效,但要花费数小时才能处理完整的数据。 I was wondering if someone could simplify my code, ideally so that it could search for matching rows rather than comparing each one? 我想知道是否有人可以简化我的代码,理想情况下,以便它可以搜索匹配的行而不是比较每一行?

I have tried using Find but I couldn't get it to work with multiple parameters. 我尝试使用Find但是无法使它与多个参数一起使用。

This is my current code, I've omitted the part where I set the parameter settings to save space but they are present in my Worksheet: 这是我当前的代码,我省略了设置参数设置以节省空间的部分,但它们存在于我的工作表中:

While row1 <= lastRow1
    Param1 = Sheets("Test1").Cells.Range("A" & row1).Value
    Param2 = Sheets("Test1").Cells.Range("B" & row1).Value
    Param3 = Sheets("Test1").Cells.Range("D" & row1).Value
    Param4 = Sheets("Test1").Cells.Range("E" & row1).Value
    Param5 = Sheets("Test1").Cells.Range("F" & row1).Value
    Cell_to_copy = Sheets("Test1").Cells.Range("G" & row1).Value
*****THIS IS THE BIT I WANT TO OPTIMISE************
        For row2 = 2 To lastRow2
            KParam1 = Sheets("Test2").Cells.Range("A" & row2).Value
            KParam2 = Sheets("Test2").Cells.Range("B" & row2).Value
            KParam3 = Sheets("Test2").Cells.Range("I" & row2).Value
            KParam5 = Sheets("Test2").Cells.Range("G" & row2).Value
                If (InStr(1, KParam1, Param1) > 0) _
                And ((InStr(1, KParam2, Param2) > 0) Or (InStr(1, Param2, KParam2) > 0)) _
                And ((InStr(1, KParam3, Param3) > 0) Or (InStr(1, KParam3, Param4) > 0)) _
                And (InStr(1, KParam5, Param5) > 0) _
                Then
                    Sheets("Test2").Cells.Range("L" & row2).Value = Cell_to_copy
                End If
        Next row2
****************************************************
        row1 = row1 + 1
Wend

I'd like to change the middle section to instead perform a search function to locate rows that match the If query, and then copy the Cell_to_copy to Cell L on that row. 我想更改中间部分,以执行搜索功能来查找与If查询匹配的行,然后将Cell_to_copy复制到该行的Cell L上。

Is this possible? 这可能吗? I can concatenate the searchable values from Test2 into a single cell if it's absolutely necessary, but I'd rather not if it can be avoided as it will match some false positives. 如果绝对必要,我可以将来自Test2的可搜索值连接到单个单元格中,但是我宁愿不要避免,因为它会匹配一些误报。

Thanks in advance, 提前致谢,

Joe

ok, so the rule seems to be, if 好的,规则似乎是

Test1.column A is found inside Test2.column A
Test1.column B is found inside Test2.column B
Test1.column D is found inside Test2.column I
Test1.column E is found inside Test2.column I
Test1.column F is found inside Test2.column G

then copy Test1.column G to Test2.Column L 然后 Test1.column G 复制Test2.L

is that right? 那正确吗?

Why can't you do that using a formula on Sheet2? 为什么不能在Sheet2上使用公式来做到这一点?

maybe: Test2.Column L formula = =IF(FIND(A:A,Sheet1!A:A)>0,Sheet1!G:G) 也许:Test2.L列公式= =IF(FIND(A:A,Sheet1!A:A)>0,Sheet1!G:G)

that formula needs an OR with the other conditions to make it check the other columns - will try after lunch! 该公式需要与其他条件进行OR才能检查其他列-午餐后尝试!

if not, then ok, here I think we need to consider using ADO and treating the worksheet as a db table. 如果没有,那么确定,在这里,我认为我们需要考虑使用ADO并将工作表视为db表。

If you show what your source data looks like, and the result you need, I can try and write a sample for you, but by far the best option is for you to learn to fish using the below links. 如果您显示源数据的样子以及所需的结果,我可以尝试为您编写示例,但是到目前为止,最好的选择是让您学习使用以下链接进行钓鱼

see here for this: MSDN KB: How To Use ADO with Excel Data from Visual Basic or VBA 参见此处: MSDN KB:如何将ADO与Visual Basic或VBA中的Excel数据一起使用

and also read: Treat Excel As a Relational Data Source on the Excel User MVP website 并在Excel用户MVP网站上阅读: 将Excel视为关系数据源

and also read: Office Space: Using ADO to Query an Excel Spreadsheet 并阅读: Office Space:使用ADO查询Excel电子表格

basically the task is to read the filtered data that you need into an ADO Recordset, then use CopyFromRecordset method to dump the results into Sheet2 in one step 基本上,任务是将所需的过滤数据读取到ADO Recordset中,然后使用CopyFromRecordset方法将结果转储到Sheet2中

So you have some reading to do, let us know if you need an example or more help 因此,您需要阅读一些内容,如果您需要示例或更多帮助,请告诉我们

Philip 菲利普

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

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