Tidy up VBA code.

MarkOW

New Member
Joined
Jun 26, 2018
Messages
27
Hi I have VBA code that I have written.......

It works but looks untidy.

Can someone offer advice on how to tidy it up with a loop so I can extend the ranges that are compared? (Extend to B3:30 & C3:C30)

Code:
Private Sub CommandButton1_Click()
'Security Pin
            


    If Worksheets("Datasheet").Range("C3").Value = CStr(Me.PIN2.Text) And Worksheets("Datasheet").Range("B3").Value = CStr(Me.ComboBox1.Value) Then
    GoTo ByPass


    
    ElseIf Worksheets("Datasheet").Range("C4").Value = CStr(Me.PIN2.Text) And Worksheets("Datasheet").Range("B4").Value = CStr(Me.ComboBox1.Value) Then
    GoTo ByPass


    
    ElseIf Worksheets("Datasheet").Range("C5").Value = CStr(Me.PIN2.Text) And Worksheets("Datasheet").Range("B5").Value = CStr(Me.ComboBox1.Value) Then
    GoTo ByPass


  
    ElseIf Worksheets("Datasheet").Range("C6").Value = CStr(Me.PIN2.Text) And Worksheets("Datasheet").Range("B6").Value = CStr(Me.ComboBox1.Value) Then
    GoTo ByPass


    
    ElseIf Worksheets("Datasheet").Range("C7").Value = CStr(Me.PIN2.Text) And Worksheets("Datasheet").Range("B7").Value = CStr(Me.ComboBox1.Value) Then
    GoTo ByPass


    
    ElseIf Worksheets("Datasheet").Range("C8").Value = CStr(Me.PIN2.Text) And Worksheets("Datasheet").Range("B8").Value = CStr(Me.ComboBox1.Value) Then
    GoTo ByPass


    ElseIf Worksheets("Datasheet").Range("C9").Value = CStr(Me.PIN2.Text) And Worksheets("Datasheet").Range("B9").Value = CStr(Me.ComboBox1.Value) Then
    GoTo ByPass


    
    ElseIf Worksheets("Datasheet").Range("C10").Value = CStr(Me.PIN2.Text) And Worksheets("Datasheet").Range("B10").Value = CStr(Me.ComboBox1.Value) Then
    GoTo ByPass


    
    ElseIf Worksheets("Datasheet").Range("C11").Value = CStr(Me.PIN2.Text) And Worksheets("Datasheet").Range("B11").Value = CStr(Me.ComboBox1.Value) Then
    GoTo ByPass


    
    ElseIf Worksheets("Datasheet").Range("C12").Value = CStr(Me.PIN2.Text) And Worksheets("Datasheet").Range("B12").Value = CStr(Me.ComboBox1.Value) Then
    GoTo ByPass
 
    
    ElseIf Worksheets("Datasheet").Range("C13").Value = CStr(Me.PIN2.Text) And Worksheets("Datasheet").Range("B13").Value = CStr(Me.ComboBox1.Value) Then
    GoTo ByPass


    
    ElseIf Worksheets("Datasheet").Range("C14").Value = CStr(Me.PIN2.Text) And Worksheets("Datasheet").Range("B14").Value = CStr(Me.ComboBox1.Value) Then
    GoTo ByPass
    
    ElseIf Worksheets("Datasheet").Range("C15").Value = CStr(Me.PIN2.Text) And Worksheets("Datasheet").Range("B15").Value = CStr(Me.ComboBox1.Value) Then
    GoTo ByPass
    Else
    
    
     MsgBox Prompt:="Incorrect PIN", Buttons:=vbCritical
     
     Me.PIN2.Value = ""


     Unload Me
     GoTo ReTry
     
ByPass:


     End If
     
    
    Call Signnow


ReTry:


    End Sub
thanks again

Mark

Office 365 Windows 10
 
Last edited:

mole999

Moderator
Joined
Oct 23, 2004
Messages
9,899
like this maybe
Code:
Private Sub CommandButton1_Click()
'Security Pin
  Dim n As interger

For n = 3 To 15 Step 1

    If Worksheets("Datasheet").Range("C" & n).Value = CStr(Me.PIN2.Text) And Worksheets("Datasheet").Range("B" & n).Value = CStr(Me.ComboBox1.Value) Then
    GoTo ByPass

    Else
    
    
     MsgBox Prompt:="Incorrect PIN", Buttons:=vbCritical
     
     Me.PIN2.Value = ""


     Unload Me
     GoTo ReTry
     
ByPass:


     End If
     
    
    Call Signnow


ReTry:


    End Sub
 

MarkOW

New Member
Joined
Jun 26, 2018
Messages
27
Hi, and thanks.

I am getting the error for without next. Where does the next go?

Thanks again
 

mole999

Moderator
Joined
Oct 23, 2004
Messages
9,899
Code:
Private Sub CommandButton1_Click()
'Security Pin
  Dim n As interger

For n = 3 To 15 Step 1

    If Worksheets("Datasheet").Range("C" & n).Value = CStr(Me.PIN2.Text) And Worksheets("Datasheet").Range("B" & n).Value = CStr(Me.ComboBox1.Value) Then
    GoTo ByPass
    
[COLOR=#ff0000]    Next n
[/COLOR]
    Else
    
    
     MsgBox Prompt:="Incorrect PIN", Buttons:=vbCritical
     
     Me.PIN2.Value = ""


     Unload Me
     GoTo ReTry
     
ByPass:


     End If
     
    
    Call Signnow


ReTry:


    End Sub
thats what happens when you can't test it :), item in red i think
 

MarkOW

New Member
Joined
Jun 26, 2018
Messages
27
Hi again ,

I tried that first but end up with error

Next without For.

Mark
 

mole999

Moderator
Joined
Oct 23, 2004
Messages
9,899
a different tweak
Code:
Private Sub CommandButton1_Click()
'Security Pin
  Dim n As Integer

    If Worksheets("Datasheet").Range("C3").Value = CStr(Me.PIN2.Text) And Worksheets("Datasheet").Range("B3").Value = CStr(Me.ComboBox1.Value) Then
    GoTo ByPass
    
For n = 4 To 15 Step 1
     
     ElseIf Worksheets("Datasheet").Range("C" & n).Value = CStr(Me.PIN2.Text) And Worksheets("Datasheet").Range("B" & n).Value = CStr(Me.ComboBox1.Value) Then
    GoTo ByPass
    
    Next n

    Else
    
     MsgBox Prompt:="Incorrect PIN", Buttons:=vbCritical
     
     Me.PIN2.Value = ""

     Unload Me
     GoTo ReTry
ByPass:
     End If
    Call Signnow
ReTry:
    End Sub
 

MarkOW

New Member
Joined
Jun 26, 2018
Messages
27
Hi Again

Another error I'm afraid.

Else without If
 

mole999

Moderator
Joined
Oct 23, 2004
Messages
9,899
currently out of ideas then, it will be something on that theme
 

MarkOW

New Member
Joined
Jun 26, 2018
Messages
27
Thanks for your help.

I have tried a lot of variations but hit a wall!

Mark
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,151
Office Version
365
Platform
Windows
How about
Code:
Private Sub CommandButton1_Click()
'Security Pin
   Dim i As Long
   Dim Flg As Boolean
   
   With Worksheets("Datasheet")
      For i = 3 To 15
         If .Range("C" & i).Value = CStr(Me.PIN2.Text) And .Range("B" & i).Value = CStr(Me.ComboBox1.Value) Then
            Flg = True
            Exit For
         End If
      Next i
   End With
   If Flg = False Then
      MsgBox prompt:="Incorrect PIN", Buttons:=vbCritical
      Me.PIN2.Value = ""
      Unload Me
      Exit Sub
   End If
      
   Call Signnow
End Sub
 

Forum statistics

Threads
1,081,845
Messages
5,361,663
Members
400,643
Latest member
RockStar89

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top