VBA Mutiple If statements are driving me crazy

NessPJ

Active Member
Joined
May 10, 2011
Messages
413
Office Version
  1. 365
Hi guys,

I'm trying to make validate a value from a textbox on my userform through VBA...but no matter what syntax i try as soon as i try to add
more then one If statement (to check for several arguments) the check seems to fail and just allow any value to be entered in the textbox.

What am i doing wrong?

This is the current syntax i am using:

Code:
Private Sub OKButton_Click()


Dim Weeknr As String


Application.ScreenUpdating = False


Weeknr = Database.DBWeeknr.Value


   If Not IsNumeric(Weeknr) Then
        If Weeknr < 1 And Weeknr > 53 Then
            MsgBox "Voer een geldig Weeknummer in!", vbDefaultButton1, "Geen Weeknummer ingevoerd"
            Database.DBWeeknr.SetFocus
        Exit Sub
        End If
   End If

MsgBox "Test Passed"

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
66,930
Office Version
  1. 365
Platform
  1. Windows
If Weeknr < 1 And Weeknr > 53 Then
This is impossible and will always be False.
How can anything be both less than 1 AND greater than 53 at the same time?
Did you mean to use an OR here instead of AND?
 
Upvote 0

NessPJ

Active Member
Joined
May 10, 2011
Messages
413
Office Version
  1. 365
Hello Joe4,

My bad that is a definite typo. So i was thinking that statement should be:
Code:
    If Not IsNumeric(Weeknr) Then       
          If Weeknr > 0 Or Weeknr < 54 Then
            MsgBox "Voer een geldig Weeknummer in!", vbDefaultButton1, "Geen Weeknummer ingevoerd"
            Database.DBWeeknr.SetFocus
            Exit Sub
        End If
   End If

The only thing i'm trying to validate is that the inserted value should be Larger than 0 and Smaller than 54 (its validating for a valid Week number).
Obviously something else in my syntax is wrong, because that's not doing the trick. :)
 
Last edited:
Upvote 0

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
66,930
Office Version
  1. 365
Platform
  1. Windows
You are really mixing up your ORs, ANDs, and greater than and less than operations.

If you think of it in a logical sense, I think it will make more sense.

Think of it as if you were trying to guess someone's age and were asking them questions.

In your first formula, you would be asking: "Are you younger than 0 AND older than 53?".
That will always be false, because the two conditions you are checking never intersect (which is what AND does - BOTH conditions must be met).

In your second formula, you would be asking: "Are you greater than 0 OR younger than 54?".
That probably isn't helpful, because in an OR statement, only one of them has to be met in order for it to be true.
In this scenario, it would accept everything (i.e. 1000 is greater than zero, so it would be true; -9999 is less than 54, so that would also be true).

Whenever you want to see if something is between two values you use AND, and greater than the smaller value and less than the larger value.
So you should have:
Code:
If weeknr > 0 And Weeknr < 54 Then
Which translates to IF the value is greater than 0 AND less than 54.

Make sense?
 
Upvote 0

NessPJ

Active Member
Joined
May 10, 2011
Messages
413
Office Version
  1. 365
That makes perfect sense (i guess looking at code all day sometimes makes you blind to obvious logic). Thanks for your explanation.

I tried to use the code you gave me, but when i test it with the value 0 or 99 it still seems to pass as TRUE. Any idea how that happens?

Code:
   If Not IsNumeric(Weeknr) Then
        If Weeknr > 0 And Weeknr < 54 Then
            MsgBox "Voer een geldig Weeknummer in!", vbDefaultButton1, "Geen Weeknummer ingevoerd"
            Database.DBWeeknr.SetFocus
            Exit Sub
        End If
   End If
 
Upvote 0

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
66,930
Office Version
  1. 365
Platform
  1. Windows
I didn't notice your first line before:
Code:
If Not IsNumeric(Weeknr) Then
Since numbers like 0 and 99 return TRUE with the IsNumeric function, putting a NOT in front of it makes it false.
This means that numbers will never enter inside your IF statement and get to the next IF statement that checks the range!
I think you want/need to the remove the word "Not" from that IF statement to make your code work properly.
 
Upvote 0

NessPJ

Active Member
Joined
May 10, 2011
Messages
413
Office Version
  1. 365
Thanks again for your help. I still could not get it to work, so after that i decided to change the validation and now it works (input has to be a number and larger than 0 but smaller than 54).

It doesn't feel as efficient as i think it could be, but here is the code i am using now:
Code:
Private Sub OKButton_Click()

Application.ScreenUpdating = False

Dim Weeknr As String

Weeknr = Database.DBWeeknr.Value


    If Weeknr = "" Then GoTo Einde
    
    If Not IsNumeric(Weeknr) Then GoTo Afkeur Else
    If Weeknr > 0 And Weeknr < 54 Then
            GoTo WeeknrGevalideerd
        Else
Afkeur:
            With Database.DBWeeknr
                .SelStart = 0
                .SelLength = Len(.Text)
                End With
            Database.DBWeeknr.SetFocus
            MsgBox "Voer een geldig Weeknummer in!", vbDefaultButton1, "Geen Weeknummer ingevoerd"
            Exit Sub
    End If


WeeknrGevalideerd:

'### Rest of my routine ###



Einde:
    
Application.ScreenUpdating = True


Unload Me

End Sub
 
Last edited:
Upvote 0

ask2tsp

Well-known Member
Joined
Feb 18, 2015
Messages
506
Office Version
  1. 365
Platform
  1. Windows
I strongly advise you to abandon Goto's from your code!
See how this can be done and how this makes more sense.
Code:
Private Sub OKButton_Click()

    Application.ScreenUpdating = False
    
    Dim Weeknr As String
    Dim weeknrOk As Boolean '<--- extra var
    
    Weeknr = Database.DbWeeknr.Value

    If Weeknr > "" Then 'only process if something entered
    
        If IsNumeric(Weeknr) Then
            If Weeknr > 0 And Weeknr < 54 Then
                weeknrOk = True 'ok if all these conditions are true
            End If
        End If
            
        If Not weeknrOk Then
            With Database.DbWeeknr
                .SelStart = 0
                .SelLength = Len(.Text)
                End With
            Database.DbWeeknr.SetFocus
            MsgBox "Voer een geldig Weeknummer in!", vbDefaultButton1, "Geen Weeknummer ingevoerd"
            Exit Sub
        End If

        '### Rest of my routine ###

    End If ' Weeknr > ""
    
    Application.ScreenUpdating = True
    Unload Me
End Sub
 
Upvote 0

NessPJ

Active Member
Joined
May 10, 2011
Messages
413
Office Version
  1. 365
Hi ask2tsp,

Thanks alot for your help and explanation!
I used your method to also include an optional Daynumber, next to the Weeknumber.
I then search for input (a range of cells) on one of my sheets to determine where the information is located. My searchfunctions might not be as universal as they could be now...but i'm happy with the end result. :)

Code:
Option Explicit




Private Sub UserForm_Activate()


Database.DBWeeknr.SetFocus


Database.DBDagnr.BackColor = &H80000004


End Sub




Private Sub OKButton_Click()


Dim Dagnr As String, Weeknr As String, WeeknrBeginRij As String, WeeknrEindeRij As String, WeekSelectie As String, DagnrBeginRij As String, DagnrEindeRij As String, DagSelectie As String
Dim WeeknrOK As Boolean, DagnrOK As Boolean


Application.ScreenUpdating = False


Sheets("Masterdata").Unprotect ("1234")
Sheets("Data Invoer").Unprotect ("1234")
Sheets("Database").Unprotect ("1234")


'=========
'= Start =
'=========


    Weeknr = Database.DBWeeknr.Value


If Weeknr > "" Then
    WeeknrOK = False
    
    If IsNumeric(Weeknr) Then
        If Weeknr > 0 And Weeknr < 54 Then
            WeeknrOK = True
        End If
    End If
End If
                
    If Not WeeknrOK Then
        With Database.DBWeeknr
            .SelStart = 0
            .SelLength = Len(.Text)
            End With
            Database.DBWeeknr.SetFocus
            MsgBox "Voer een geldig Weeknummer in!", vbDefaultButton1, "Geen Weeknummer ingevoerd"
            Exit Sub
    End If
    
    If WeeknrOK Then
    
    WeeknrEindeRij = Zoeken(Weeknr, "Data Invoer", "AB4001", "AB")


    If WeeknrEindeRij = "" Then MsgBox "Er zijn geen gegevens gevonden aan de hand van het ingegeven Weeknummer!", vbDefaultButton1, "Geen gegevens gevonden"


    WeeknrBeginRij = ZoekenBNO(Weeknr, "Data Invoer", "AB12", "AB4001", "AB")


    WeekSelectie = "AB" & WeeknrBeginRij & ":" & "AB" & WeeknrEindeRij




    MsgBox "De gehele week zou nu zijn gezocht. Selectie: " & WeekSelectie
    
    
    Dagnr = Database.DBDagnr.Value


    If Dagnr > "" Then
        
    If IsNumeric(Dagnr) Then
        If Dagnr > 0 And Dagnr < 8 Then
            DagnrOK = True
        End If
    End If


    If Not DagnrOK Then
        With Database.DBDagnr
            .SelStart = 0
            .SelLength = Len(.Text)
            End With
            Database.DBDagnr.SetFocus
            MsgBox "Voer een geldig Dagnummer in (1 t/m 7)! Indien het Dagnummer leeg wordt gelaten zal de gehele week worden bijgezocht", vbDefaultButton1, "Geen geldig Dagnummer ingevoerd"
            Exit Sub
        End If
        
    If DagnrOK Then
            
    DagnrEindeRij = ZoekenDAGONB(Dagnr, "Data Invoer", "AC" & WeeknrBeginRij, "AC" & WeeknrEindeRij, "AC")


    If DagnrEindeRij = "" Then MsgBox "Er zijn geen gegevens gevonden aan de hand van het ingegeven Dagnummer!", vbDefaultButton1, "Geen gegevens gevonden"


    DagnrBeginRij = ZoekenDAGBNO(Dagnr, "Data Invoer", "AC" & WeeknrBeginRij, "AC" & WeeknrEindeRij, "AC")


    DagSelectie = "AC" & DagnrBeginRij & ":" & "AC" & DagnrEindeRij




    MsgBox "Het dagnummer zou nu ook zijn bijgezocht. DagSelectie: " & DagSelectie
    
    End If
        
    End If   'DagnrOK If


End If      'WeeknrOK If
        


'=========
'= Einde =
'=========


Einde:


    Sheets("Masterdata").Protect DrawingObjects:=True, Contents:=True, AllowUsingPivotTables:=True, Scenarios:=True _
    , AllowFiltering:=True, Password:="1234"
    
    Sheets("Data Invoer").Protect DrawingObjects:=True, Contents:=True, AllowUsingPivotTables:=True, Scenarios:=True _
    , AllowFiltering:=True, Password:="1234"
    
    Sheets("Database").Protect DrawingObjects:=True, Contents:=True, AllowUsingPivotTables:=True, Scenarios:=True _
    , AllowFiltering:=True, Password:="1234"
    
Application.ScreenUpdating = True


Unload Me


End Sub




Private Sub CancelButton_Click()


Unload Me


End Sub




Private Sub DBWeeknr_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)


    If KeyCode = 13 Then
         OKButton_Click
    End If
    
End Sub




Private Sub DBDagnr_Enter()


Database.DBDagnr.BackColor = &H80000005


End Sub




Private Sub DBDagnr_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)


    If KeyCode = 13 Then
         OKButton_Click
    End If
    
End Sub


Public Function Zoeken(ZoekWaarde, ZoekSheet, LaatsteCellVoorZoeken, KolomVoorZoeken As String)


'De volgende routine creeërt een functie en zoekt een ingegeven string van onderen naar boven in de tabel (zodat de meest recente wordt gevonden).
'Vervolgens wordt het Regelnummer (Rownumber) teruggemeld als gevonden waarde.


Dim StringRowNumber As Integer, LaatsteCellZoekVeld, code As String


    LaatsteCellZoekVeld = ThisWorkbook.Sheets(ZoekSheet).Range(LaatsteCellVoorZoeken, ThisWorkbook.Sheets(ZoekSheet).Range(LaatsteCellVoorZoeken).End(xlUp)).Row
      
    If ZoekWaarde = "" Then GoTo StopMetZoeken
    
    For StringRowNumber = LaatsteCellZoekVeld To 1 Step -1
         code = ThisWorkbook.Sheets(ZoekSheet).Range(KolomVoorZoeken & StringRowNumber)
         If code = ZoekWaarde Then
         Zoeken = StringRowNumber
            Exit For
         End If
    Next
  
StopMetZoeken:


End Function


Public Function ZoekenBNO(ZoekWaarde, ZoekSheet, EersteCellVoorZoeken, LaatsteCellVoorZoeken, KolomVoorZoeken As String)


'De volgende routine creeërt een functie en zoekt een ingegeven string van boven naar onderen in de tabel (zodat de eerste wordt gevonden).
'Vervolgens wordt het Regelnummer (Rownumber) teruggemeld als gevonden waarde.


Dim StringRowNumber As Integer, EersteCellZoekVeld As String, LaatsteCellZoekVeld As String, code As String


    EersteCellZoekVeld = ThisWorkbook.Sheets(ZoekSheet).Range(EersteCellVoorZoeken).Row
    
    LaatsteCellZoekVeld = ThisWorkbook.Sheets(ZoekSheet).Range(LaatsteCellVoorZoeken, ThisWorkbook.Sheets(ZoekSheet).Range(LaatsteCellVoorZoeken).End(xlUp)).Row
      
    If ZoekWaarde = "" Then GoTo StopMetZoeken
    
    For StringRowNumber = EersteCellZoekVeld To LaatsteCellZoekVeld Step 1
         code = ThisWorkbook.Sheets(ZoekSheet).Range(KolomVoorZoeken & StringRowNumber)
         If code = ZoekWaarde Then
         ZoekenBNO = StringRowNumber
            Exit For
         End If
    Next
  
StopMetZoeken:


End Function


Public Function ZoekenDAGONB(ZoekWaarde, ZoekSheet, EersteCellVoorZoeken, LaatsteCellVoorZoeken, KolomVoorZoeken As String)


'De volgende routine creeërt een functie en zoekt een ingegeven string van onderen naar boven in de tabel (zodat de eerste wordt gevonden).
'Vervolgens wordt het Regelnummer (Rownumber) teruggemeld als gevonden waarde.


Dim StringRowNumber As Integer, EersteCellZoekVeld As String, LaatsteCellZoekVeld As String, code As String


    EersteCellZoekVeld = ThisWorkbook.Sheets(ZoekSheet).Range(EersteCellVoorZoeken).Row
    
    LaatsteCellZoekVeld = ThisWorkbook.Sheets(ZoekSheet).Range(LaatsteCellVoorZoeken).Row
      
    If ZoekWaarde = "" Then GoTo StopMetZoeken
    
    For StringRowNumber = LaatsteCellZoekVeld To EersteCellZoekVeld Step -1
         code = ThisWorkbook.Sheets(ZoekSheet).Range(KolomVoorZoeken & StringRowNumber)
         If code = ZoekWaarde Then
         ZoekenDAGONB = StringRowNumber
            Exit For
         End If
    Next
  
StopMetZoeken:


End Function


Public Function ZoekenDAGBNO(ZoekWaarde, ZoekSheet, EersteCellVoorZoeken, LaatsteCellVoorZoeken, KolomVoorZoeken As String)


'De volgende routine creeërt een functie en zoekt een ingegeven string van boven naar onderen in de tabel (zodat de eerste wordt gevonden).
'Vervolgens wordt het Regelnummer (Rownumber) teruggemeld als gevonden waarde.


Dim StringRowNumber As Integer, EersteCellZoekVeld As String, LaatsteCellZoekVeld As String, code As String


    EersteCellZoekVeld = ThisWorkbook.Sheets(ZoekSheet).Range(EersteCellVoorZoeken).Row
    
    LaatsteCellZoekVeld = ThisWorkbook.Sheets(ZoekSheet).Range(LaatsteCellVoorZoeken).Row
      
    If ZoekWaarde = "" Then GoTo StopMetZoeken
    
    For StringRowNumber = EersteCellZoekVeld To LaatsteCellZoekVeld Step 1
         code = ThisWorkbook.Sheets(ZoekSheet).Range(KolomVoorZoeken & StringRowNumber)
         If code = ZoekWaarde Then
         ZoekenDAGBNO = StringRowNumber
            Exit For
         End If
    Next
  
StopMetZoeken:


End Function
 
Last edited:
Upvote 0

ask2tsp

Well-known Member
Joined
Feb 18, 2015
Messages
506
Office Version
  1. 365
Platform
  1. Windows
NessPj, I changed your code a bit, see comments in the code. Hope you learn a few things about writing vba code.
Things to improve:
why write a function if you only use it once?
Forward (top to bottom) searches can be done with the range.find method
Code:
Option Explicit

Private Sub UserForm_Activate()
    Database.DbWeeknr.SetFocus
    Database.DBDagnr.BackColor = &H80000004
End Sub

Private Sub OKButton_Click()

    'All 'nr' vars in the next dim are numbers, but declared as string!
    'Dim Dagnr As String, Weeknr As String, WeeknrBeginRij As String, WeeknrEindeRij As String, _
        WeekSelectie As String, DagnrBeginRij As String, DagnrEindeRij As String, DagSelectie As String
    
    Dim Dagnr As Integer, Weeknr As Integer, WeeknrBeginRij As Long, WeeknrEindeRij As Long, _
        WeekSelectie As String, DagnrBeginRij As Long, DagnrEindeRij As Long, DagSelectie As String
        
    Dim WeeknrOK As Boolean, DagnrOK As Boolean
    
    'Application.ScreenUpdating = False  'the screen is not updated by this sub
    
    'do this later (when you start changing things)
    'Sheets("Masterdata").Unprotect ("1234")
    'Sheets("Data Invoer").Unprotect ("1234")
    'Sheets("Database").Unprotect ("1234")

'=========
'= Start =      a bit useless comment
'=========

    If Database.DbWeeknr.Value > "" Then
       'WeeknrOK = False    '1. it is False at the start
                            '2. if you want this, don't do it here _
                                do it before the if
                                
        If IsNumeric(Database.DbWeeknr.Value) Then
            Weeknr = CInt(Database.DbWeeknr.Value)
            If Weeknr > 0 And Weeknr < 54 Then
                WeeknrOK = True
            End If
        End If
    End If
                
    If Not WeeknrOK Then
        With Database.DbWeeknr
            .SelStart = 0
            .SelLength = Len(.Text)
        End With
        Database.DbWeeknr.SetFocus
        MsgBox "Voer een geldig Weeknummer in!", vbDefaultButton1, "Geen Weeknummer ingevoerd"
        Exit Sub
    End If
    
    'If WeeknrOK Then    'at this point this is true, so not necessary
    Sheets("Masterdata").Unprotect ("1234")
    Sheets("Data Invoer").Unprotect ("1234")
    Sheets("Database").Unprotect ("1234")
    
    WeeknrEindeRij = Zoeken(Format(Weeknr, "0"), "Data Invoer", "AB4001", "AB")

    If WeeknrEindeRij = 0 Then
        MsgBox "Er zijn geen gegevens gevonden aan de hand van het ingegeven Weeknummer!", vbDefaultButton1, "Geen gegevens gevonden"
    Else
        WeeknrBeginRij = ZoekenBNO(Weeknr, "Data Invoer", "AB12", "AB4001", "AB")
    
        WeekSelectie = "AB" & WeeknrBeginRij & ":" & "AB" & WeeknrEindeRij
    
        MsgBox "De gehele week zou nu zijn gezocht. Selectie: " & WeekSelectie
        
        If Database.DBDagnr.Value > "" Then
        
            If IsNumeric(Database.DBDagnr.Value) Then
                Dagnr = CInt(Database.DBDagnr.Value)
                If Dagnr > 0 And Dagnr < 8 Then
                    DagnrOK = True
                End If
            End If
    
            If Not DagnrOK Then
                With Database.DBDagnr
                    .SelStart = 0
                    .SelLength = Len(.Text)
                End With
                Database.DBDagnr.SetFocus
                MsgBox "Voer een geldig Dagnummer in (1 t/m 7)! Indien het Dagnummer leeg wordt gelaten zal de gehele week worden bijgezocht", vbDefaultButton1, "Geen geldig Dagnummer ingevoerd"
                Exit Sub
            End If
        
            'If DagnrOK Then '<--- useless (always true here)
                    
            DagnrEindeRij = ZoekenDAGONB(Dagnr, "Data Invoer", "AC" & WeeknrBeginRij, "AC" & WeeknrEindeRij, "AC")
        
            If DagnrEindeRij = "" Then MsgBox "Er zijn geen gegevens gevonden aan de hand van het ingegeven Dagnummer!", vbDefaultButton1, "Geen gegevens gevonden"
        
            DagnrBeginRij = ZoekenDAGBNO(Dagnr, "Data Invoer", "AC" & WeeknrBeginRij, "AC" & WeeknrEindeRij, "AC")
        
            DagSelectie = "AC" & DagnrBeginRij & ":" & "AC" & DagnrEindeRij
        
            MsgBox "Het dagnummer zou nu ook zijn bijgezocht. DagSelectie: " & DagSelectie
        End If 'WeeknrEindeRij
        
    End If 'Dagnr > ""
        
    'End If   'DagnrOK If

'End If      'WeeknrOK If
'Einde:

    Sheets("Masterdata").Protect DrawingObjects:=True, Contents:=True, AllowUsingPivotTables:=True, Scenarios:=True _
    , AllowFiltering:=True, Password:="1234"
    
    Sheets("Data Invoer").Protect DrawingObjects:=True, Contents:=True, AllowUsingPivotTables:=True, Scenarios:=True _
    , AllowFiltering:=True, Password:="1234"
    
    Sheets("Database").Protect DrawingObjects:=True, Contents:=True, AllowUsingPivotTables:=True, Scenarios:=True _
    , AllowFiltering:=True, Password:="1234"
    
    'Application.ScreenUpdating = True

    Unload Me

End Sub

Private Sub CancelButton_Click()
    Unload Me
End Sub

Private Sub DBWeeknr_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 13 Then
         OKButton_Click
    End If
End Sub

Private Sub DBDagnr_Enter()
    Database.DBDagnr.BackColor = &H80000005
End Sub

Private Sub DBDagnr_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 13 Then
         OKButton_Click
    End If
End Sub

'Public Function Zoeken(ZoekWaarde, ZoekSheet, LaatsteCellVoorZoeken, KolomVoorZoeken As String)

'It is good programming practise to declare the variable types of the arguments and return
'It shows what is expected there, making it easier to understand.

'maybe you think that Dim a,b,c as String means a,b and c are strings. That's not true.
'a and b have no vartype so they are typed as Variant

Public Function Zoeken(ZoekWaarde As String, ZoekSheet As String, _
                    LaatsteCellVoorZoeken As String, KolomVoorZoeken As String) As Long

'De volgende routine creeërt een functie en zoekt een ingegeven string van onderen naar boven in de tabel (zodat de meest recente wordt gevonden).
'Vervolgens wordt het Regelnummer (Rownumber) teruggemeld als gevonden waarde.

    Dim StringRowNumber As Integer, LaatsteCellZoekVeld As Long, code As String
    '   ------             -------
    'misleading name, plus make a habit of dim'ing row/column variables as Long.
    
    With ThisWorkbook.Sheets(ZoekSheet)
    LaatsteCellZoekVeld = .Range(LaatsteCellVoorZoeken, .Range(LaatsteCellVoorZoeken).End(xlUp)).Row
    End With
    
    'If ZoekWaarde = "" Then GoTo StopMetZoeken  '<--- don't use GOTO !!
    'don't say "if condition skip this", but "if negated condition do this"
    
    Zoeken = 0 'The 'not found' Return value
    
    If ZoekWaarde > "" Then '? why use this function with nothing to search for?
        For StringRowNumber = LaatsteCellZoekVeld To 1 Step -1
            code = ThisWorkbook.Sheets(ZoekSheet).Range(KolomVoorZoeken & StringRowNumber)
            If code = ZoekWaarde Then
                Zoeken = StringRowNumber
                Exit For
            End If
        Next
    End If
End Function


Public Function ZoekenBNO(ZoekWaarde, ZoekSheet, EersteCellVoorZoeken, LaatsteCellVoorZoeken, KolomVoorZoeken As String)


'De volgende routine creeërt een functie en zoekt een ingegeven string van boven naar onderen in de tabel (zodat de eerste wordt gevonden).
'Vervolgens wordt het Regelnummer (Rownumber) teruggemeld als gevonden waarde.


Dim StringRowNumber As Integer, EersteCellZoekVeld As String, LaatsteCellZoekVeld As String, code As String


    EersteCellZoekVeld = ThisWorkbook.Sheets(ZoekSheet).Range(EersteCellVoorZoeken).Row
    
    LaatsteCellZoekVeld = ThisWorkbook.Sheets(ZoekSheet).Range(LaatsteCellVoorZoeken, ThisWorkbook.Sheets(ZoekSheet).Range(LaatsteCellVoorZoeken).End(xlUp)).Row
      
    If ZoekWaarde = "" Then GoTo StopMetZoeken
    
    For StringRowNumber = EersteCellZoekVeld To LaatsteCellZoekVeld Step 1
         code = ThisWorkbook.Sheets(ZoekSheet).Range(KolomVoorZoeken & StringRowNumber)
         If code = ZoekWaarde Then
         ZoekenBNO = StringRowNumber
            Exit For
         End If
    Next
  
StopMetZoeken:


End Function


Public Function ZoekenDAGONB(ZoekWaarde, ZoekSheet, EersteCellVoorZoeken, LaatsteCellVoorZoeken, KolomVoorZoeken As String)


'De volgende routine creeërt een functie en zoekt een ingegeven string van onderen naar boven in de tabel (zodat de eerste wordt gevonden).
'Vervolgens wordt het Regelnummer (Rownumber) teruggemeld als gevonden waarde.


Dim StringRowNumber As Integer, EersteCellZoekVeld As String, LaatsteCellZoekVeld As String, code As String


    EersteCellZoekVeld = ThisWorkbook.Sheets(ZoekSheet).Range(EersteCellVoorZoeken).Row
    
    LaatsteCellZoekVeld = ThisWorkbook.Sheets(ZoekSheet).Range(LaatsteCellVoorZoeken).Row
      
    If ZoekWaarde = "" Then GoTo StopMetZoeken
    
    For StringRowNumber = LaatsteCellZoekVeld To EersteCellZoekVeld Step -1
         code = ThisWorkbook.Sheets(ZoekSheet).Range(KolomVoorZoeken & StringRowNumber)
         If code = ZoekWaarde Then
         ZoekenDAGONB = StringRowNumber
            Exit For
         End If
    Next
  
StopMetZoeken:


End Function


Public Function ZoekenDAGBNO(ZoekWaarde, ZoekSheet, EersteCellVoorZoeken, LaatsteCellVoorZoeken, KolomVoorZoeken As String)


'De volgende routine creeërt een functie en zoekt een ingegeven string van boven naar onderen in de tabel (zodat de eerste wordt gevonden).
'Vervolgens wordt het Regelnummer (Rownumber) teruggemeld als gevonden waarde.


Dim StringRowNumber As Integer, EersteCellZoekVeld As String, LaatsteCellZoekVeld As String, code As String


    EersteCellZoekVeld = ThisWorkbook.Sheets(ZoekSheet).Range(EersteCellVoorZoeken).Row
    
    LaatsteCellZoekVeld = ThisWorkbook.Sheets(ZoekSheet).Range(LaatsteCellVoorZoeken).Row
      
    If ZoekWaarde = "" Then GoTo StopMetZoeken
    
    For StringRowNumber = EersteCellZoekVeld To LaatsteCellZoekVeld Step 1
         code = ThisWorkbook.Sheets(ZoekSheet).Range(KolomVoorZoeken & StringRowNumber)
         If code = ZoekWaarde Then
         ZoekenDAGBNO = StringRowNumber
            Exit For
         End If
    Next
  
StopMetZoeken:


End Function
 
Upvote 0

Forum statistics

Threads
1,190,825
Messages
5,983,106
Members
439,824
Latest member
nellyc

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
Top