Very basic IF statement is going wrong

TheRedCardinal

Board Regular
Joined
Jul 11, 2019
Messages
243
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi all,

I have added a prompt for my user to decide if they want to save a file after some processes have been applied to it.

It's a simple message box with a Yes No Option. It worked fine until recently when it started always exiting without opening the Dialog box, even if clicking yes. I stepped through and discovered the Variable was getting a 6 or 7 instead of Yes or No.

So I amended my code:

VBA Code:
Answer = MsgBox("Would you like to save your new VAT submission?", vbYesNo + vbQuestion, "Save Submission")
        
            If Answer = vbNo Or Answer = 7 Then
                
               End If
            
            Else
            
                Application.Dialogs(xlDialogSaveAs).Show
              
            End If

But it still doesn't work.

As you can see from the screenshot as I walked through, Answer = 6 but the IF statement is still proceeding to the End If Line, even though the logical test should have failed (I pressed yes on the button, and you can see the Variable = 6)

What very obvious thing am I missing?
2021-02-12_16-04-58.png
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
vbNo and 7 are the same thing.

use:
Code:
If Answer = vbNo  Then 
    'something here
     ' or another IF
     if false then
           beep
     endif
Else
Application.Dialogs(xlDialogSaveAs).Show
End If
 
Upvote 0
I'm surprised that even compiles. You have 2 End If lines for one If. Try something like:

VBA Code:
Answer = MsgBox("Would you like to save your new VAT submission?", vbYesNo + vbQuestion, "Save Submission")
        
            If Answer = vbYes
            
                Application.Dialogs(xlDialogSaveAs).Show
              
            End If
 
Upvote 0
Solution
Hi thanks for the answer,

Yes I know that - I wasn't sure why the macro suddenly shifted from being "vbNo" to 7 so I left both in.

What I don't understand is why the IF statement proceeds to the "End If" when neither condition is met?

vbNo and 7 are the same thing.

use:
Code:
If Answer = vbNo  Then
    'something here
     ' or another IF
     if false then
           beep
     endif
Else
Application.Dialogs(xlDialogSaveAs).Show
End If
 
Upvote 0
I'm surprised that even compiles. You have 2 End If lines for one If. Try something like:

VBA Code:
Answer = MsgBox("Would you like to save your new VAT submission?", vbYesNo + vbQuestion, "Save Submission")
       
            If Answer = vbYes
           
                Application.Dialogs(xlDialogSaveAs).Show
             
            End If

Hi - I flipped it around to do it your way and it seems to be working now.

The extra End If turned out to be a trailing one from earlier in the code, so I've tidied that up.

I'm still not sure I understand why mine didn't work though (or rather suddenly stopped working) but I have a solution now which is the important bit!
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,262
Members
449,075
Latest member
staticfluids

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