简体   繁体   English

从列中查找值并快速返回其单元格的行号

[英]Find a value from a column and quickly return the row number of its cell

What I have 是)我有的

I have a file with part numbers and several suppliers for each part. 我有一个包含零件号的文件和每个零件的几个供应商。 There are 1500 parts with around 20 possible suppliers each. 共有1500个零件,每个零件有20个可能的供应商。 For the sake of simplicity let's say parts are listed in column A, with each supplier occupying a column after that. 为简单起见,我们假设部件列在A列中,每个供应商在此之后占据一列。 Values under the suppliers are entered manually but don't really matter. 供应商下的值是手动输入的,但并不重要。

In another sheet, I have a list of parts that is imported from an Access database. 在另一个工作表中,我有一个从Access数据库导入的部件列表。 The parts list is imported, but not the supplier info. 导入零件清单,但不导入供应商信息。 In both cases, each part appears only once. 在这两种情况下,每个部分只出现一次。

What I want to do 我想做的事

I simply want to match the supplier info from the first sheet with the parts in the imported list. 我只想将第一张表中的供应商信息与导入列表中的部件相匹配。 Right now, I have a function which goes through each part in the list with suppliers, copies the supplier information in an array, finds the part number in the imported part list (there is always a unique match) and copies the array next to it (with supplier info inside). 现在,我有一个功能,它通过供应商遍历列表中的每个部分,将供应商信息复制到一个数组中,在导入的部件列表中找到部件号(总是有唯一的匹配)并将数组复制到它旁边(内有供应商信息)。 It works. 有用。 Unfortunately, the find function slows down considerably each time it is used. 不幸的是,每次使用它时,find函数都会显着减慢。 I know it is the culprit through various tests, and I can't understand why it slows down (starts at 200 loop iterations per second, slows down to 1 per second and Excel crashes) . 我知道它是通过各种测试的罪魁祸首,我无法理解为什么它减慢速度(从每秒200次循环迭代开始,减慢到每秒1次并且Excel崩溃)。 I may have a leak of some sort? 我可能有某种泄漏? The file size remains 7mb throughout. 文件大小始终为7mb。 Here it is: 这里是:

Function LigneNum(numAHNS As String) As Integer
    Dim oRange As Range, aCell As Range
    Dim SearchString As String

    Set oRange = f_TableMatrice.Range("A1:A1500")
    SearchString = numAHNS

    Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

    If Not aCell Is Nothing Then
        'We have found the number by now:
            LigneNum = aCell.Row
        Exit Function
    Else
        MsgBox "Un numéro AHNS n'a pas été trouvé: " & SearchString
        Debug.Print SearchString & " not found!"
            LigneNum = 0
        Exit Function
    End If

End Function

The function simply returns the row number on which the value is found, or 0 if it doesn't find it which should never happen. 该函数只返回找到该值的行号,如果找不到应该永远不会发生的行号,则返回0。

What I need help with 我需要帮助的是什么

I'd like either to identify the cause of the slow down, or find a replacement for the Find method. 我想要找出减速的​​原因,或者找到Find方法的替代品。 I have used the Find before and it is the first time this happens to me. 我之前使用过Find,这是第一次发生在我身上。 It was initially taken from Siddarth Rout's website: http://www.siddharthrout.com/2011/07/14/find-and-findnext-in-excel-vba/ What is strange is that it doesn't start slow, it just becomes sluggish as it goes on. 它最初取自Siddarth Rout的网站: http//www.siddharthrout.com/2011/07/14/find-and-findnext-in-excel-vba/奇怪的是它起步不慢,它随着它的继续变得迟钝。

I think using Match could work, or maybe dumping the range to search (the part numbers) into an array and trying to match these with the imported parts number list could work. 我认为使用Match可以工作,或者可能会将搜索范围(部件号)转储到一个数组中,并尝试将这些与导入的部件号列表相匹配。 I am unsure how to do it, but my question is more about which one would be faster (as long as it remains under 15 seconds I don't really care, though, but looping over 1500 items 1500 times right out of the sheet is out of the question). 我不确定该怎么做,但我的问题更多的是关于哪一个会更快(只要它仍然不到15秒我不在乎,但是,从表单中循环超过1500项1500次是出于问题)。 Would anyone suggest match over the array solution / spending more hours fixing my code? 有人建议匹配阵列解决方案/花更多时间修复我的代码吗?

EDIT 编辑

Here is the loop it is being called from. 这是从它调用的循环。 I don't think it is problematic: 我不认为这是有问题的:

For Each cellToMatch In rngToMatch
        Debug.Print cellToMatch.Row
        'The cellsToMatch's values are the numbers I want, rngToMatch is the column where they are.

        For i = 2 To nbSup + 1
            infoSup(i - 2) = f_TableMatrice.Cells(cellToMatch.Row, i)
        Next
        'infoSup contains the required supplier data now
        'I call the find function here to find the row where the number appears in the imported sheet
        'To copy the array nbSup on that line
        LigneAHNS = LigneNum(cellToMatch.Value) 'This is the Find function
        If LigneAHNS = 0 Then Exit Sub
        'This loop just empties the array in the right line.
        For i = LBound(infoSup) To UBound(infoSup)
            f_symix.Cells(LigneAHNS, debutsuppliers + i) = infoSup(i)
        Next

    Next

If I replace LigneAHNS = LigneNum by LigneAHNS = 20, for example, the code executes extremely fast. 例如,如果我用LigneAHNS = 20替换LigneAHNS = LigneNum,代码执行速度非常快。 The leak therefore comes from the find function itself. 泄漏因此来自查找功能本身。

Another way to do it without using the find function might be something like this. 在不使用find函数的情况下执行此操作的另一种方法可能是这样的。 Firstly, put the part IDs and their line numbers into a scripting dictionary. 首先,将部件ID及其行号放入脚本字典中。 These are really quick to lookup from. 这些非常快速查找。 Like this: 像这样:

Dim Dict As New Scripting.Dictionary
Dim ColA As Variant
Lastrow=range("A50000").end(xlUp).Row
ColA = Range("A1:A" & LastRow).Value
For i = 1 To LastRow
    Dict.Add ColA(i, 1), i
Next i

To further optimise, you could declare the Dict as a public variable, populate it once, and refer to it many times in your lookups. 要进一步优化,您可以将Dict声明为公共变量,填充一次,并在查找中多次引用它。 I expect this would be faster than running a cells.find over a range every time you do a lookup. 我希望这会比每次执行查找时在一个范围内运行一个cells.find更快。

For syntax of looking up items in the dictionary, refer to Looping through a Scripting.Dictionary using index/item number 有关在字典中查找项目的语法,请参阅使用索引/项目编号循环访问Scripting.Dictionary

You could achieve this with only Excel cell formulas and no VB if you are willing to devote a separate column to each supplier on your main parts sheet. 如果您愿意为主零件表上的每个供应商分配一个单独的列,则只能使用Excel单元格公式而不使用VB来实现此目的。 You could then use conditional formatting to make it more visually appealing. 然后,您可以使用条件格式使其更具视觉吸引力。 I've tried it with 1500 rows and it's very quick. 我用1500行试过它,速度非常快。 Increasing it to 5000 rows becomes noticeably slower, but you say you have only 1500 rows for now, so it should be suitable. 将它增加到5000行变得明显变慢,但是你说你现在只有1500行,所以它应该是合适的。

  • On Sheet 1, define a part number column and a separate column for each supplier . 在工作表1上,为每个供应商定义部件号列和单独的列。
  • Create a separate sheet for each supplier with all part numbers available from that supplier listed in column A. Make sure the rows on the supplier sheets are ordered by part number. 为每个供应商创建一个单独的工作表,其中包含A列中所列供应商提供的所有零件编号。确保供应商工作表上的行按部件号排序。
  • Name each of the supplier sheets the same as the associated column heading shown on Sheet 1. 将每个供应商工作表命名为与工作表1中显示的关联列标题相同。
  • Assign the following formula in each cell beneath each supplier column heading on Sheet 1: 在工作表1上的每个供应商列标题下方的每个单元格中分配以下公式:

    =NOT(ISNA(VLOOKUP($A2,INDIRECT("'"&B$1&"'!A:A"),1,FALSE))) = NOT(ISNA(VLOOKUP($ A2,INDIRECT( “ ' ”&B $ 1“' A:A”),1,FALSE)))

The following screen cap shows this implemented along with conditional formatting to highlight which suppliers have which parts: 以下屏幕截图显示了这与条件格式一起实现,以突出显示哪些供应商具有哪些部分:

在此输入图像描述

If you wanted to show quantities available from suppliers, then you could always have a second column (B) on the supplier sheets containing last known quantities for each part and use VLOOKUP to retrieve column B instead of A. 如果您想显示供应商提供的数量,那么您可以在供应商表上始终包含第二列(B),其中包含每个部件的最后已知数量,并使用VLOOKUP检索B列而不是A.

暂无
暂无

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

相关问题 在列中查找特定的文本值,然后将其返回到自己的行至另一个单元格 - Find specific text value in column and return it to its own row to another cell 在行中查找最后一个条目,然后在该列的顶部行中返回单元格的值 - Find Last Entry in Row, then Return Value of Cell in Top Row of That Column 是否有单元格返回其自己的行号的公式? - Is there a formula for a cell to return its own row number? Excel:如何在包含字符串的列中查找单元格并在另一列行中返回单元格的值? - Excel: How to find cell in a column that contains a string and return value of a cell in another column of row? 在C列中找到“ 1”,从A列中的该行返回值 - Find '1' in column C, return value from that row in column A 返回列单元格中最大的数值加上相关的行单元格数据 - Return highest number value in column cell plus a related row cell data 如何在包含字符串的列中查找单元格并在另一行中返回单元格的值? - How to find cell in a column that contains a string and return value of a cell in another of row? VBA - 查找行号或返回下一个空单元格的行号 - VBA - Find row number or return row number of next empty cell 搜索列中的每个单元格以查找特定值,并从该值所在的行中的另一个单元格复制值 - Search each cell in a column to find a certain value and copy value from another cell in the row the value is found in 从另一个工作表获取单元格值; 该单元格具有已知的列号但未知的行号 - Getting a cell value from another sheet; the cell has a known column number but un-known row number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM