Message box to stop Macro if two cells are filled

rachel06

Board Regular
Joined
Feb 3, 2016
Messages
126
Office Version
  1. 365
Platform
  1. Windows
Hi!!

This one should be pretty easy. I need a message box to appear if both cells C1 and C2 are populated with "OK" as the only option

Code:
   If Range("C1").Value = <> "" and Range.("C2")<> "" Then
    mycheck = MsgBox("Cannot append text to both ends of file name. Please correct and re-run macro.")
      Exit Sub
    End If
  End If

I also have one already done that's just an FYI if C1 isn't populated, but if I could edit that to pop up if either or both are blank, and an indication which one is blank, that would be a wonderful addition. That might be super complicated though

Code:
 If Range("C1").Value = "" Then
    mycheck = MsgBox("Quarter is blank. Continue?", vbYesNo)
    If mycheck = vbNo Then
      Exit Sub
    End If
  End If

I also need these two to play nice together! This is what the whole thing looks like together now. It's obviously giving me an error.

Code:
Sub CheckQtr()
  Dim mycheck As VbMsgBoxResult
 
  If Range("C1").Value = "" Then
    mycheck = MsgBox("Quarter is blank. Continue?", vbYesNo)
    If mycheck = vbNo Then
      Exit Sub
    End If
  End If
  
   If Range("C1").Value = <> "" and Range.("C2")<> "" Then
    mycheck = MsgBox("Cannot append text to both ends of file name. Please correct and re-run.")
      Exit Sub
    End If
  End If
 
  Call Copyfilefromto
End Sub

Thanks in advance for any help or guidance!!
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Is something like this what you are looking for?
VBA Code:
If (Range("C1").Value = "OK") And (Range("C2").Value = "OK") Then
    MsgBox "Stopping Macro now!"
    Exit Sub
End If
 
Upvote 1
Solution
Is something like this what you are looking for?
VBA Code:
If (Range("C1").Value = "OK") And (Range("C2").Value = "OK") Then
    MsgBox "Stopping Macro now!"
    Exit Sub
End If
That's super close. I've been able to manipulate that to get part of what I need. How to I swap that OK value to be is not blank? I've tried the below and similar and no luck.

Code:
  If (Range("C1").Value <>= "") And (Range("C2").Value <>= "") Then
  MsgBox "Cannot add values before and after file name. Please review and re-run."
    Exit Sub
 
Upvote 0
To check for NOT equal to, it should just be "<>", not "<>=" (no equal sign in there!)
 
Upvote 1
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,147
Members
453,021
Latest member
Justyna P

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