Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the .Find() method in Excel VBA appear to only check the first 15 characters?

I have en Excel List with ID's and Data(About 8000 Rows and more). The ID's can be only digits oder Digits with charaters. Like "2222222222222223" or "222222222222222A" (the Cells with the ID are formatted as Text)

If i try to find the Entry Address with a ID, it will insert a now row, because it will not find them. What means that at the End i have Doubled all Entries.


So my Question is, how many characters does the find method compare?

With objSheet
     strAdresseExcel = .Cells.Find(What:=str, After:=.Cells(1, 1), _ 
     LookAt:=Excel.XlLookAt.xlWhole, MatchCase:=True, SearchFormat:=False).Address
    End with

So i created a dummy List with 8000 Rows where all the ID's had the Number "2222222222222222" and i found something out.

That Excel compares only the first 15 characters if there are only Digits in the String but i works fine if there is a Letter in the ID! You can see the issue in the Image below

Information to the Image:
Row A:
i worked with Conditional Formatting.
(On the Home tab, click Conditional Formatting, Highlight Cells Rules, Duplicate Values)
Works only when the ID has Digits or there is a difference in the first 15 Characters.

Row B:
I worked with a Formula. (=IF(COUNTIF(A:A;A2)>1;"Double";""))
How you can see, the same Problem happens here as well.

enter image description here

like image 349
Moosli Avatar asked Mar 16 '17 12:03

Moosli


People also ask

How does the Find function work in VBA?

FIND Function of VBA Excel. The FIND function of VBA excel searches a specific value in a specified range. It looks for the first instance of such value and if a match is found, the function returns the cell containing it. However, if a match is not found, the function does not return anything.

What does find return if nothing is found VBA?

Remarks. This method returns Nothing if no match is found. The Find method does not affect the selection or the active cell. The settings for LookIn, LookAt, SearchOrder, and MatchByte are saved each time you use this method.

How do I get the first character of a string in Excel VBA?

LEFT Function (Worksheet / VBA) The LEFT function returns the specified number of characters in a text string, starting from the first or left-most character. Use this function to extract a sub-string from the left part of a text string.


1 Answers

This seems to work for me, unless I'm missing something, which is entirely possible :)

It returns the first address where the cell matched and prints out the lengths of the strings searched. I have the data set setup in Sheet1 just as shown, with the numeric data being stored as Text (I added an apostrophe at the start of each value)

 Sub testSO()
    Dim objSheet   As Worksheet: Set objSheet = ThisWorkbook.Sheets("Sheet1")
    Dim strToFind  As String: strToFind = "22222222222222222222222"
    Dim strAddress As String

    strAddress = objSheet.Cells.Find(strToFind, objSheet.Cells(1, 1), _
    LookAt:=Excel.XlLookAt.xlWhole, MatchCase:=True, SearchFormat:=False).Address

    'Print out findings
    Debug.Print strAddress, Len(strToFind), Len(objSheet.Range(strAddress))
End Sub

Output is:

$A$5 23 23

like image 64
Ryan Wildry Avatar answered Nov 27 '22 15:11

Ryan Wildry