[VBA]Is my search method bad?

NessPJ

Active Member
Joined
May 10, 2011
Messages
420
Office Version
  1. 365
Hello all,

I wrote the following piece of VBA to find the right "Searchvalue" and only continue if the Searchvalue was indeed found.
The routine seems to go through every value it finds with this method and if the first one it finds isn't correct, the IF statement will go to the Else condition right away.

Something is wrong in my logic here? :)

VBA Code:
Dim TabelnrBereikLR As Long
Dim Searchvalue As Long, Foundvalue As Long

'....start of my code....

TabelnrBereikLR = Sheets("Masterdata").Range("E65534").End(xlUp).Row

'Loop each cell in range
        For n = TabelnrBereikLR To 4 Step -1
       
        If RecenteInvoer = True Then
            Searchvalue = RecentTabelnr
        Else
            Searchvalue = Tabelnr
        End If
       
        'Debug.Print "Searchvalue = "; Searchvalue
       
        Foundvalue = Sheets("Masterdata").Range("B" & n).Value

            If Format(Foundvalue, "00000") = Format(Searchvalue, "00000") Then
           
            'Debug.Print "Found = "; Sheets("Masterdata").Range("B" & n).Value
           
            GoTo TabelnrGevalideerd
           
            Else
           
            MsgBox "Het gekozen Tabelnummer / Printopdrachtnummer is nog niet gevuld of is niet geldig. De handeling kan niet worden uitgevoerd.", vbCritical, "Bewerking afgebroken"
            GoTo EindeCancel
                   
            End If
   
        Next
       
        n = 0

TabelnrGevalideerd:

'...code to execute when the value was found...


Einde:      '// Einde van de routine

Sheets("Menu").Activate

PROTON (Password)

Application.ActivePrinter = StandaardPrinter

Application.ScreenUpdating = True
Application.DisplayAlerts = True

Exit Sub


EindeCancel:

Sheets("Menu").Activate

PROTON (Password)

Application.ActivePrinter = StandaardPrinter

Application.ScreenUpdating = True
Application.DisplayAlerts = True

Exit Sub

End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,215,549
Messages
6,125,473
Members
449,233
Latest member
Deardevil

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top