+ Reply to Thread
Results 1 to 7 of 7

Close Excel IF two cells aren't equal

Hybrid View

  1. #1
    Registered User
    Join Date
    12-15-2012
    Location
    Miami, FL
    MS-Off Ver
    Microsoft 2010
    Posts
    28

    Close Excel IF two cells aren't equal

    Hello,

    Could anyone help me with a code that would check whether two values (Cell A1, and B1 for example) are the same, and if they aren't then make the vba close excel?

    I can do the if a not equal b then close part, but I can't figure out how to give the user a chance to input some data that would make A1 and B1 equal..

    any ideas?

    Thanks

  2. #2
    Valued Forum Contributor
    Join Date
    11-02-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    564

    Re: Close Excel IF two cells aren't equal

    Ok, can I assume that data is already present in A1 and user would be entering data in B1..?

  3. #3
    Registered User
    Join Date
    12-15-2012
    Location
    Miami, FL
    MS-Off Ver
    Microsoft 2010
    Posts
    28

    Re: Close Excel IF two cells aren't equal

    Haripopuri - yes, there is data in A1, and the data inputted by the user would be in B1.

    Thanks

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Close Excel IF two cells aren't equal

    Maybe:

    Sub Jona2491()
    Dim x As String
    Dim y As String
    
    x = InputBox("Please Enter a Value for Range A1")
    y = InputBox("Please Enter a Value for Range B1")
    
    Range("A1").Value = x
    Range("B1").Value = y
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    If Range("A1") <> Range("B1") Then Application.Quit
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
        
    End Sub
    Last edited by JOHN H. DAVIS; 02-21-2013 at 10:32 AM.

  5. #5
    Registered User
    Join Date
    12-15-2012
    Location
    Miami, FL
    MS-Off Ver
    Microsoft 2010
    Posts
    28

    Re: Close Excel IF two cells aren't equal

    John,

    this looks like the right track, but I need it to prompt on start up - so as soon as they open the file, that box comes up.

    Also, as I said to Hari in the previous post, only 1 value has to be entered, and it has to be compared to a previous value

    thanks

  6. #6
    Valued Forum Contributor
    Join Date
    11-02-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    564

    Re: Close Excel IF two cells aren't equal

    Following code should be in ThisWorkBook module.
    Private Sub Workbook_Open()
    On Error Resume Next
    MyNumber = InputBox("Enter the value to be in Cell B1")
    Range("B1").Value = MyNumber
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
        If Range("A1") <> Range("B1") Then Application.Quit
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    End Sub

  7. #7
    Registered User
    Join Date
    12-15-2012
    Location
    Miami, FL
    MS-Off Ver
    Microsoft 2010
    Posts
    28

    Re: Close Excel IF two cells aren't equal

    Hari, you're great, I will star you

    but!

    if the wrong value is put in, there's an option to Save, Don't Save, or Cancel. If you hit "Cancel"...it just goes away and the program doesn't close :/

    i'm know for fact there's a way to close the program without prompting - would i just switch "Then Application.Quit" to "Then activeworkbook.close (0)" ?


    ** yes it works with activeworkbook close! thanks haripouri!!
    Last edited by Jona2491; 02-22-2013 at 10:54 AM.

+ 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