Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 基于搜索条件提取行_Excel_Vba_Copy_Extract - Fatal编程技术网

Excel 基于搜索条件提取行

Excel 基于搜索条件提取行,excel,vba,copy,extract,Excel,Vba,Copy,Extract,我的问题是,我试图从一个非常大的数据表中提取一些信息。提取的信息基于表单中输入的某些搜索条件。搜索表单统计此条件存在的次数,但是我需要将各个行提取到第二张表中 我遇到的困难是理解如何实际构造提取代码。我需要被指向正确的方向。如果代码可以计算出有多少次出现,我当然可以得到这些出现的行号并提取信息,我只是想弄清楚它 这是我的搜索代码(此代码用于根据要求的条件获取出现次数) 有没有一种简单的方法可以通过循环来实现这一点?我知道循环不是处理事情的最佳方式,但我正在寻找任何有效的方法,我可以调整以满足我的

我的问题是,我试图从一个非常大的数据表中提取一些信息。提取的信息基于表单中输入的某些搜索条件。搜索表单统计此条件存在的次数,但是我需要将各个行提取到第二张表中

我遇到的困难是理解如何实际构造提取代码。我需要被指向正确的方向。如果代码可以计算出有多少次出现,我当然可以得到这些出现的行号并提取信息,我只是想弄清楚它

这是我的搜索代码(此代码用于根据要求的条件获取出现次数)

有没有一种简单的方法可以通过循环来实现这一点?我知道循环不是处理事情的最佳方式,但我正在寻找任何有效的方法,我可以调整以满足我的需要

如果你能提前帮忙的话,谢谢你,这是一个巨大的电子表格

------------------------------- *以接受的答案更新:* -------------------------------

Public Sub Count_Extract_Click()

'Collect Information To Be Extracted
Set ws = Worksheets("database")
Set ps = Worksheets("Extracted Rows")

   ps.Range("A3:AM60000").Clear


For i = RowNum To RowNumEnd
   If ws.Cells(i, CR1).Value = V_1 Then

   ws.Range("A" & i & ":AM" & i).Copy

   ps.Activate


   'find first empty row in database
emR = ps.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1


ps.Range("A" & emR & ":AM" & emR).PasteSpecial

   End If
Next i

End If

End Sub

我真的无法尝试,但也许你可以。保留行
V_1=Me.Criteria_1_Variable.Value
,但将下一行2替换为:

CR1_Result = 0 'Initiates counter at 0
Dim CR1_Lines(1000) As Long 'Declares an array of 1001 (indexes 0-1000) Longs (big integers) 

For x = RowNum To RowNumEnd 'Loops through all the rows of CR1

    If ws.Cells(x, CR1) = V_1 Then 'Match!

        'Double array size if capacity is reached
        If CR1_Result = UBound(CR1_Lines) Then
            ReDim Presrve CR1_Lines(UBound(CR1_Lines) * 2)
        End If

        'Store that line number in the array
        CR1_Lines(CR1_Result) = x 

        'Increment count of matches
        CR1_Result = CR1_Result + 1 

    End If

Next x 'Next row!
然后,可以使用以下代码循环该数组:

For i = 0 to UBound(CR1_Lines)
    'Do something! (Why not just an annoying pop-up box with the content!)
     MsgBox CR1_Lines(i)
Next i
编辑:我刚读到电子表格非常可怕,每次发现新的匹配项时重新标注尺寸可能很简单,但这会导致性能下降。我直接在上面的代码中做了一些更改,使其更加有效


编辑#2:我简化了代码,因此除了复制粘贴之外,您无需做任何事情(请原谅我没有假设RowNum和RownumMend具有有效数据)。它的工作原理应该和公认的答案完全一样,但它在发布之前就已经发布了,并且实际显示了如何提取行号。我知道,如果您只需要一个带有行号的弹出框,我会对已经收到的upvote感到满意。

我真的无法尝试,但也许您可以。保留行
V_1=Me.Criteria_1_Variable.Value
,但将下一行2替换为:

CR1_Result = 0 'Initiates counter at 0
Dim CR1_Lines(1000) As Long 'Declares an array of 1001 (indexes 0-1000) Longs (big integers) 

For x = RowNum To RowNumEnd 'Loops through all the rows of CR1

    If ws.Cells(x, CR1) = V_1 Then 'Match!

        'Double array size if capacity is reached
        If CR1_Result = UBound(CR1_Lines) Then
            ReDim Presrve CR1_Lines(UBound(CR1_Lines) * 2)
        End If

        'Store that line number in the array
        CR1_Lines(CR1_Result) = x 

        'Increment count of matches
        CR1_Result = CR1_Result + 1 

    End If

Next x 'Next row!
然后,可以使用以下代码循环该数组:

For i = 0 to UBound(CR1_Lines)
    'Do something! (Why not just an annoying pop-up box with the content!)
     MsgBox CR1_Lines(i)
Next i
编辑:我刚读到电子表格非常可怕,每次发现新的匹配项时重新标注尺寸可能很简单,但这会导致性能下降。我直接在上面的代码中做了一些更改,使其更加有效


编辑#2:我简化了代码,因此除了复制粘贴之外,您无需做任何事情(请原谅我没有假设RowNum和RownumMend具有有效数据)。它的工作原理应该和公认的答案完全一样,但它在发布之前就已经发布了,并且实际显示了如何提取行号。我知道,如果您只需要一个带有行号的弹出框,我会对已经收到的upvote感到满意。

我真的无法尝试,但也许您可以。保留行
V_1=Me.Criteria_1_Variable.Value
,但将下一行2替换为:

CR1_Result = 0 'Initiates counter at 0
Dim CR1_Lines(1000) As Long 'Declares an array of 1001 (indexes 0-1000) Longs (big integers) 

For x = RowNum To RowNumEnd 'Loops through all the rows of CR1

    If ws.Cells(x, CR1) = V_1 Then 'Match!

        'Double array size if capacity is reached
        If CR1_Result = UBound(CR1_Lines) Then
            ReDim Presrve CR1_Lines(UBound(CR1_Lines) * 2)
        End If

        'Store that line number in the array
        CR1_Lines(CR1_Result) = x 

        'Increment count of matches
        CR1_Result = CR1_Result + 1 

    End If

Next x 'Next row!
然后,可以使用以下代码循环该数组:

For i = 0 to UBound(CR1_Lines)
    'Do something! (Why not just an annoying pop-up box with the content!)
     MsgBox CR1_Lines(i)
Next i
编辑:我刚读到电子表格非常可怕,每次发现新的匹配项时重新标注尺寸可能很简单,但这会导致性能下降。我直接在上面的代码中做了一些更改,使其更加有效


编辑#2:我简化了代码,因此除了复制粘贴之外,您无需做任何事情(请原谅我没有假设RowNum和RownumMend具有有效数据)。它的工作原理应该和公认的答案完全一样,但它在发布之前就已经发布了,并且实际显示了如何提取行号。我知道,如果您只需要一个带有行号的弹出框,我会对已经收到的upvote感到满意。

我真的无法尝试,但也许您可以。保留行
V_1=Me.Criteria_1_Variable.Value
,但将下一行2替换为:

CR1_Result = 0 'Initiates counter at 0
Dim CR1_Lines(1000) As Long 'Declares an array of 1001 (indexes 0-1000) Longs (big integers) 

For x = RowNum To RowNumEnd 'Loops through all the rows of CR1

    If ws.Cells(x, CR1) = V_1 Then 'Match!

        'Double array size if capacity is reached
        If CR1_Result = UBound(CR1_Lines) Then
            ReDim Presrve CR1_Lines(UBound(CR1_Lines) * 2)
        End If

        'Store that line number in the array
        CR1_Lines(CR1_Result) = x 

        'Increment count of matches
        CR1_Result = CR1_Result + 1 

    End If

Next x 'Next row!
然后,可以使用以下代码循环该数组:

For i = 0 to UBound(CR1_Lines)
    'Do something! (Why not just an annoying pop-up box with the content!)
     MsgBox CR1_Lines(i)
Next i
编辑:我刚读到电子表格非常可怕,每次发现新的匹配项时重新标注尺寸可能很简单,但这会导致性能下降。我直接在上面的代码中做了一些更改,使其更加有效


编辑#2:我简化了代码,因此除了复制粘贴之外,您无需做任何事情(请原谅我没有假设RowNum和RownumMend具有有效数据)。它的工作原理应该和公认的答案完全一样,但它在发布之前就已经发布了,并且实际显示了如何提取行号。我知道您所需要的只是一个带有行号的弹出框,并且会对已经收到的upvote感到满意。

您应该能够设置一个For循环来检查找到的范围内的每个值,并将其复制到(另一个单元格、数组,任何您喜欢的地方)


我还没有测试过这个,但它应该可以工作。如果您有任何错误,请告诉我。

您应该能够设置一个For循环来检查您找到的范围内的每个值,并将其复制到(另一个单元格、数组,任何您喜欢的地方)


我还没有测试过这个,但它应该可以工作。如果您有任何错误,请告诉我。

您应该能够设置一个For循环来检查您找到的范围内的每个值,并将其复制到(另一个单元格、数组,任何您喜欢的地方)


我还没有测试过这个,但它应该可以工作。如果您有任何错误,请告诉我。

您应该能够设置一个For循环来检查您找到的范围内的每个值,并将其复制到(另一个单元格、数组,任何您喜欢的地方)


我还没有测试过这个,但它应该可以工作。如果您有任何错误,请告诉我。

谢谢,工作完美无瑕。我必须在例程之外声明我的变量,以便它们可以共享信息,但除此之外,这是一个直接的复制和粘贴。非常好,谢谢你,汉克斯,工作完美无瑕。我必须申报我的v