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

David G

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. For the sake of simplicity let's say parts are listed in column A, with each supplier occupying a column after that. 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. 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. 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) . I may have a leak of some sort? The file size remains 7mb throughout. 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.

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. I have used the Find before and it is the first time this happens to me. 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.

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. 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). 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. The leak therefore comes from the find function itself.

CustodianOfCode

Another way to do it without using the find function might be something like this. Firstly, put the part IDs and their line numbers into a scripting dictionary. 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. I expect this would be faster than running a cells.find over a range every time you do a lookup.

For syntax of looking up items in the dictionary, refer to Looping through a Scripting.Dictionary using index/item number

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I find a value in a row and return the column number with VBA?

From Dev

Return highest number value in column cell plus a related row cell data

From Dev

Find row-wise minimum and return value and its corresponding column name

From Dev

Finding maximum value in a column and return row number

From Dev

Find value in range of strings and return row number

From Dev

Find closest value from column and return number from adjacent column (within lists)

From Dev

Select column and find the value from other cell

From Dev

Find word in part of cell text, return column number: Excel VBA

From Dev

Finding row number of last cell with value not equal to 0 in specific column

From Dev

Google Apps Script - Find Row number based on a cell value

From Dev

Excel formula - If value was found in row return value from next cell

From Dev

VBA: How to find search value from Sheet "DMR" and then from found search value row copy cell at column A and cell at Column D into Sheet "Search"

From Dev

VBA: How to find search value from Sheet "DMR" and then from found search value row copy cell at column A and cell at Column D into Sheet "Search"

From Dev

How to find table cell value based on condition provided in column and row?

From Dev

excel vba: find the row of a cell with a certain value and put that row number in another cell

From Dev

Find out Excel column character from name of cell in first row

From Dev

Check if Cell value exists in Row, and return a value in the same Column but different Row

From Dev

Return row number(s) for a particular value in a column in a dataframe

From Dev

Need to find value in one column and then return value from another column

From Dev

Excel select a value from a cell having row number calculated

From Dev

Excel select a value from a cell having row number calculated

From Dev

Find first number greater than x and return another value in that row

From Dev

Excel VBA macro - find the cell location from which cells in two columns match the row and column in another workbook then paste a value

From Dev

Finding the cell value from another sheet basing on row/column index

From Dev

Finding the cell value from another sheet basing on row/column index

From Dev

Excel VBA - How to find a value in a column and return the row it is on

From Dev

openpyxl find cell or row by value

From Dev

A cell contains the number of row and I want to get the value of that row on specific column

From Dev

Find a row number based on data from a string in another column

Related Related

  1. 1

    How do I find a value in a row and return the column number with VBA?

  2. 2

    Return highest number value in column cell plus a related row cell data

  3. 3

    Find row-wise minimum and return value and its corresponding column name

  4. 4

    Finding maximum value in a column and return row number

  5. 5

    Find value in range of strings and return row number

  6. 6

    Find closest value from column and return number from adjacent column (within lists)

  7. 7

    Select column and find the value from other cell

  8. 8

    Find word in part of cell text, return column number: Excel VBA

  9. 9

    Finding row number of last cell with value not equal to 0 in specific column

  10. 10

    Google Apps Script - Find Row number based on a cell value

  11. 11

    Excel formula - If value was found in row return value from next cell

  12. 12

    VBA: How to find search value from Sheet "DMR" and then from found search value row copy cell at column A and cell at Column D into Sheet "Search"

  13. 13

    VBA: How to find search value from Sheet "DMR" and then from found search value row copy cell at column A and cell at Column D into Sheet "Search"

  14. 14

    How to find table cell value based on condition provided in column and row?

  15. 15

    excel vba: find the row of a cell with a certain value and put that row number in another cell

  16. 16

    Find out Excel column character from name of cell in first row

  17. 17

    Check if Cell value exists in Row, and return a value in the same Column but different Row

  18. 18

    Return row number(s) for a particular value in a column in a dataframe

  19. 19

    Need to find value in one column and then return value from another column

  20. 20

    Excel select a value from a cell having row number calculated

  21. 21

    Excel select a value from a cell having row number calculated

  22. 22

    Find first number greater than x and return another value in that row

  23. 23

    Excel VBA macro - find the cell location from which cells in two columns match the row and column in another workbook then paste a value

  24. 24

    Finding the cell value from another sheet basing on row/column index

  25. 25

    Finding the cell value from another sheet basing on row/column index

  26. 26

    Excel VBA - How to find a value in a column and return the row it is on

  27. 27

    openpyxl find cell or row by value

  28. 28

    A cell contains the number of row and I want to get the value of that row on specific column

  29. 29

    Find a row number based on data from a string in another column

HotTag

Archive