Home > Software design >  Excel VBA Find Result is Inconsistent
Excel VBA Find Result is Inconsistent

Time:11-01

Good Day

i am working with VBA Excel i am a complete newbie here, and i am having a issue with this piece of code

Sub setEVAFieldValue(pattern As String, fval As Variant)
    Dim lrange As Range
    Dim lrange2 As Range
    Dim lrange3 As Range
    Dim rowpos As Integer
    Dim colpos As Integer

    Set lrange = Worksheets("EVA").Cells.Find(pattern)
    If (lrange.Row > 0) Then
        rowpos = lrange.Row
        colpos = lrange.Column
        Worksheets("EVA").Cells(rowpos, colpos   2).value = fval
    End If
End Sub

This code normally works without any issues and find the cell with the pattern and add the values to the adjanced cell without any problems.

But for some reason, when another piece of code in the worksheet that does some calculations and shows a form with these calculations runs, the find does not work any more, and return a nothing for the range, even if i can see the cell in the worksheet with the pattern.

Does any one have a clue of what could be happening?

As Requested here is a example of a pattern i look for : "Beta"

and the call used : Call setEVAFieldValue("Beta", betaValue)

And here is the value in the cell : "5) Beta"

PS> The value in the cell is without the doble quotes

CodePudding user response:

From the Range.Find docs:

The settings for LookIn, LookAt, SearchOrder, and MatchByte are saved each time you use this method. If you don't specify values for these arguments the next time you call the method, the saved values are used. Setting these arguments changes the settings in the Find dialog box, and changing the settings in the Find dialog box changes the saved values that are used if you omit the arguments. To avoid problems, set these arguments explicitly each time you use this method.

Said otherwise, when using Find, always specify the What, LookIn, and LookAt parameters (and SearchOrder and MatchByte if they are relevant to the find you're performing).

  • Related