+ Reply to Thread
Results 1 to 3 of 3

Meeting two conditions before proceeding with code

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-03-2012
    Location
    Washington State
    MS-Off Ver
    Excel 365
    Posts
    340

    Meeting two conditions before proceeding with code

    Hello,

    I am stuck on some code where I am trying to meet 2 conditions after pressing a button on my userform. First thing I do is press Button1 and then assign a variable to the contents I pull in to 2 textboxes.

    Dim aVal
    
    CommandButton1_Click()
    
    'Bring in data
    aVal = TextBox1
    'rest of my code
    End Sub
    Now, when I press Button2, I need to check 2 things:
    1. Check PC name
    2. Is TextBox1 > aVal

    So, if both conditions are TRUE then I want to call another sub.
    CommandButton2_Click()
    'Update data to Sheet1
    Dim aHostName As String
    aHostName = Environ$("computername")
    
    If TextBox1 > aVal & aHostName = "LYNPC7318" Then
    Call Update
    End If
    
    'run rest of code
    End Sub
    Now this is where I am stuck... I have tried all sorts of IF statements for this.
    IF the PC name is NOT correct, but the user put more data in the textbox then I want to message them saying "Incorrect PC for updating" then exit sub.

    -OR-

    IF the PC name is not correct and the value of TextBox1 has not changed, then just continue with the code and do nothing. Hopefully I made sense explaining my issue. Thanks in advance!

    -Mike

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Meeting two conditions before proceeding with code

    Hi

    is this it?

    If textbox1 <> aval & ahostname = "LYNPC7318" Then
        Call Update
      ElseIf textbox1 <> aval & ahostname <> "LYNPC7318" Then
        MsgBox "Incorrect PC for updating"
        Exit Sub
      End If
    'run rest of code
    rylo

  3. #3
    Forum Contributor
    Join Date
    04-03-2012
    Location
    Washington State
    MS-Off Ver
    Excel 365
    Posts
    340

    Re: Meeting two conditions before proceeding with code

    Hi Rylo,

    I tested your code and if it is NOT the right PC it does not let me run the rest of the code if textbox1 stays unchanged. I am quite new to this but this is what I came up with today and it seems to work but seems to not be written properly.

    Private Sub CommandBtn1_Click()
    
    Dim aHostName As String
    aHostName = Environ$("computername")
    
    
    If Me.TextBox1 > aVal And aHostName = "LYNPC7318" Then
    Call Update
    End If
    
    
    If Me.TextBox1 > aVal And Not aHostName = "LYNPC7318" Then
    MsgBox "Wrong PC to update job numbers", vbCritical, "PC Error"
    TextBox1 = aVal
    TextBox16 = bVal
    Exit Sub
    
    Else
    'Run remaining code
    End If
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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