Results 1 to 16 of 16

Getting unexpected Compile error: Variable not defined

Threaded View

  1. #1
    Forum Contributor
    Join Date
    09-10-2008
    Location
    Phoenix, AZ
    MS-Off Ver
    Office 365
    Posts
    988

    Getting unexpected Compile error: Variable not defined

    Hello All,

    I am getting an unexpected error when trying to use the form. Any Ideas would be appreciated

    Error:

    unexpected Compile error: Variable not defined This is at the Create Username and Password Section - (IN RED COLOR) and the error is highlited in Blue - iCnt =

    There are three Columns in the sheet (Users_Data)

    Name | Password | Sheet1 - Once the form works - I need to modify the form so that each entry that is made in the form the 3rd Column is automatically populated with the word "Sheet1"

    Added a pic of the UserForm - below



    
    Option Explicit
    Dim iCounta As Integer
    Private Sub cmbValidate_Click()
        Dim rUsers As Range, rPasses As Range, rSheets As Range
        Dim lUserRow As Long
        Dim wsName As Variant
        Dim wsArray As Variant
        With Worksheets("Users_Data").UsedRange
            Set rUsers = .Columns(1)
            Set rPasses = .Columns(2)
            Set rSheets = .Columns(3)
        End With
        If Application.WorksheetFunction.CountIf(rUsers, Me.tbxUser.Value) < 1 Then
            MsgBox "Invalid Username", vbExclamation, "Alert"
        Else
            lUserRow = Application.WorksheetFunction.Match(Me.tbxUser.Value, rUsers, False)
            If Not CStr(Me.tbxPW.Value) = CStr(rPasses.Rows(lUserRow).Value) Then
                MsgBox "Invalid Password", vbExclamation, "Alert"
            Else
                UnlockSheets (rSheets.Rows(lUserRow).Value)
                Unload Me
                Worksheets("Users_Data").Cells(lUserRow, 5).Value = Worksheets("Users_Data").Cells(lUserRow, 5).Value + 1
                End
            End If
        End If
        With Me
            .LblTries.Caption = .LblTries.Caption - 1
            tbxUser.Value = vbNullString
            tbxPW.Value = vbNullString
            tbxUser.SetFocus
        End With
        iCounta = iCounta + 1
        If iCounta > 2 Then
            MsgBox "3 Invalid Attempts. WorkBook Will Now Close", vbOKOnly + vbCritical, "Warning"
            ActiveWorkbook.Close savechanges:=False
        End If
    End Sub
    
    Private Function UnlockSheets(sShts As Variant)
        Dim sh As Variant
        On Error GoTo BadShts
        If sShts = "" Then GoTo BadShts
        
        For Each sh In Split(sShts, ",")
           With ThisWorkbook.Sheets(sh)
             .Visible = True
              .Select
            End With
        Next sh
        
        Worksheets("Users_Data").Visible = xlSheetVeryHidden
    
        MsgBox "Congratulations - " & sShts & " Unlocked", vbInformation
        On Error GoTo 0
        Exit Function
    BadShts:
        MsgBox "Invalid Sheet Names : " & sShts, vbCritical
    End Function
    
    
    Private Sub Label5_Click()
    
    End Sub
    
    Private Sub UserForm_Initialize()
        Me.LblTries.Caption = 3
    
    End Sub
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
        If CloseMode = 0 Then
            Cancel = True
            If Not MsgBox("Would You Like To Close The Document?", vbYesNo + vbInformation, "Close Request") = vbNo Then
                ActiveWorkbook.Close savechanges:=False
            End If
        End If
    End Sub
    
    '___________Create Username and Password Section______________________________________________________
    
    
    'Variable Declaration
    Dim BlnVal As Boolean
    
    
    Private Sub UserForm_Initialize2()
        'Variable declaration
        Dim IdVal As Integer
        
        'Finding last row in the Data Sheet
        IdVal = fn_LastRow(Sheets("Users_Data"))
        
    
    End Sub
    Sub cmdAdd_Click()
        'Variable declaration
        Dim txtName, txtPassword
        'Dim iCnt As Integer
        
        'find next available row to update data in the data worksheet
        iCnt = fn_LastRow(Sheets("Users_Data")) + 1
         
        'Update userform data to the Data Worksheet
        With Sheets("Users_Data")
     
            .Cells(iCnt, 1) = frmData.txtName
    
            .Cells(iCnt, 2) = frmData.txtPassword
          
        'Finding last row in the Data Sheet
        IdVal = fn_LastRow(Sheets("Users_Data"))
        
         End With
    End Sub
    
    
    'In this example we are finding the last Row of specified Sheet
    Function fn_LastRow(ByVal Sht As Worksheet)
    
        Dim lastRow As Long
        lastRow = Sht.Cells.SpecialCells(xlLastCell).Row
        lRow = Sht.Cells.SpecialCells(xlLastCell).Row
        Do While Application.CountA(Sht.Rows(lRow)) = 0 And lRow <> 1
            lRow = lRow - 1
        Loop
        fn_LastRow = lRow
    
    End Function
    
    'Exit from the Userform
    Private Sub cmdCancel_Click()
        Unload Me
    End Sub
    
    ' Check all the data(except remarks field) has entered are not on the userform
    Sub Data_Validation()
         If txtName = "" Then
            MsgBox "Enter Name!", vbInformation, "Name"
            Exit Sub
        ElseIf txtCNum = "" Then
            MsgBox "Enter Password!", vbInformation, "Password"
            Exit Sub
        Else
            BlnVal = 1
        End If
    End Sub
    
    'Clearing data fields of userform
    Private Sub cmdClear_Click()
        Application.ScreenUpdating = False
            txtName.Text = ""
            txtPassword = ""
    
        Application.ScreenUpdating = True
    End Sub
    Attached Images Attached Images
    Last edited by JJFletcher; 08-28-2018 at 11:59 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Compile Error - Variable Not defined
    By larrysdime in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2015, 11:01 AM
  2. Compile error: variable not defined
    By jwoodman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-12-2014, 06:52 AM
  3. [SOLVED] Compile Error: Variable Not Defined
    By Limitedtimeonearth in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-03-2013, 03:07 PM
  4. Compile Error Variable not Defined
    By Mooseman60 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-16-2010, 10:13 AM
  5. Compile error on variable (not defined)
    By mqdias in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-12-2007, 06:47 AM
  6. Variable not defined compile error
    By Phil Hageman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-17-2005, 09:05 AM
  7. [SOLVED] Compile error, variable not defined
    By davegb in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-19-2005, 12:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1