Need Elseif to change with each row

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
Hello All,
I have a code validation page setup. But on my sheet InGen I have a list of validation codes and their corresponding expiration dates. Now I could just do an elseif statement that matches the original if statement over and over again, but I was wondering if there is a far more elegant way of doing this?

Basically the codes run from row 4781 through 4791. So the first IF would be 4781, then the ElseIf would be 4782, the next ElseIf would be 4783, etc.

Is there a simple way of doing this?


Code:
Private Sub validate()If Range("D6").Value = Sheets("InGen").Range("XAB4781") Then
    If Now() > CDate(4 / 14 / 1912) And Now() < CDate(Sheets("InGen").Range("XAC4781")) Then
    MsgBox "Your code has been accepted." & vbNewLine & vbNewLine & "Your Software is valid until " & Sheets("InGen").Range("XAC4781"), vbInformation, "The Code Validation Robot Says:"
    Else:
    MsgBox "Your code is not valid." & vbNewLine & "This code expired on " & Sheets("InGen").Range("XAC4780"), vbInformation, "The Code Validation Robot Says:"
    End If


Else:
    MsgBox "Your code is invalid." & vbNewLine & "Please check the code and try again." & vbNewLine & "Contact support with any issues.", vbInformation, "The Code Validation Robot Says:"
End If
    
    
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi,
as suggestion, maybe a For Next Loop will do what you want?

something like following:

Rich (BB code):
Private Sub Validate()
    Dim ValidateCode As Integer
    Dim wsInGen As Worksheet
    Dim Title As String
    
    Title = "The Code Validation Robot Says:"
    
    Set wsInGen = ThisWorkbook.Worksheets("InGen")
    
    For ValidateCode = 4781 To 4791
        
        If Range("D6").Value = wsInGen.Range("XAB" & ValidateCode) Then
            
            If Now() > CDate(4 / 14 / 1912) And Now() < CDate(wsInGen.Range("XAC" & ValidateCode)) Then
                
                MsgBox "Your code has been accepted." & vbNewLine & vbNewLine & _
                "Your Software is valid until " & wsInGen.Range("XAC" & ValidateCode), vbInformation, Title
            Else
                
                MsgBox "Your code is not valid." & vbNewLine & _
                "This code expired on " & wsInGen.Range("XAC" & ValidateCode - 1), vbInformation, Title
                
            End If
            
            
        Else
            
            MsgBox "Your code is invalid." & vbNewLine & "Please check the code and try again." & vbNewLine & _
            "Contact support with any issues.", vbInformation, Title
            
        End If
        
        Next ValidateCode
        
End Sub


example is not intended as a complete or working solution but just to give you some ideas.
Note the range I have highlighted in RED - which is unqualified - To ensure your code performs as intended, you should qualify it to required worksheet.


Hope Helpful

Dave
 
Upvote 0
That's getting close. I'm tweaking it a bit. Adding Exit sub after the msgbox seemed to fix getting an invalid msgbox repeatedly after the first code was correct, however if I chose a code that was halfway down the list, I would get several invalid messages then the code would get accepted, then exit.
I feel like if I put an elseif in the loop, then I would only get one response instead of a msgbox for every row.

Any ideas?
 
Upvote 0
It was just a suggestion for you to play with to see if could adapt.

Without see copy of your workbook can only guess but perhaps just test for a valid code in Loop & report status according to what is found.

Example:

Code:
Private Sub Validate()
    Dim ValidateCode As Integer
    Dim wsInGen As Worksheet
    Dim Title As String
    
    Title = "The Code Validation Robot Says:"
    
    Set wsInGen = ThisWorkbook.Worksheets("InGen")
    
    For ValidateCode = 4781 To 4791
        
        If Range("D6").Value = wsInGen.Range("XAB" & ValidateCode) Then
            
            If Now() > CDate(4 / 14 / 1912) And Now() < CDate(wsInGen.Range("XAC" & ValidateCode)) Then
                
                MsgBox "Your code has been accepted." & vbNewLine & vbNewLine & _
                "Your Software is valid until " & wsInGen.Range("XAC" & ValidateCode), vbInformation, Title
            Exit Sub
            
        End If
        
        Next ValidateCode
        
         
            MsgBox "Your code is invalid." & vbNewLine & "Please check the code and try again." & vbNewLine & _
            "Contact support with any issues.", vbInformation, Title
        
End Sub



If you are just checking a number against a range of numbers you may not need to perform a loop test but as said, without seeing copy of your workbook, it's just guessing.

Anyway - I am about to head out of the door for weekend away - hope suggestions are of some help.

Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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