If two out of three cells are empty, create msgbox

pook_666

Board Regular
Joined
Aug 16, 2018
Messages
94
Hi there,

I have three cells, say A1, A2 & A3.

In order to be able to run a macro, one of these cells have to contain a value. No more than one of these cells can contain a value.

If there is a value in two or more or these three cells, or no values at all in the three cells, I want a msgbox to appear stating "error".

I was thinking of maybe doing some type of loop to do this, but am not sure?

I know this sounds like a puzzle Sherlock Holmes would be proud to investigate, but any help would be appreciated!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
In worksheet module

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("A1", "A3")) Is Nothing Then
    Set Rng = Range("A1", "A3")
    nCount = Application.WorksheetFunction.CountA(Rng)
    If nCount > 1 Then MsgBox "Error"
End If

End Sub
 
Upvote 0
Hi,
try adding following in your code & see if helps

VBA Code:
Dim a As Integer
a = Application.CountA(Range("A1:A3"))
If a = 1 Then
'run macro
Else
    MsgBox "One Cell Only Must Be Completed", 48, "Error"
End If

note: you will need to qualify the range if the sheet is not the activesheet

Dave
 
Last edited:
Upvote 0
Solution
@dmt32 Marvelous! Thank you for this.......glad I didn't go down the loop route otherwise would've been stuck for ages!
When you mentioned loop, I thought you want macro to automatically run when condition is True. That is why I use Worksheet Change event ;)
 
Upvote 0
@Zot ah sorry, what I meant in my original question was the only way I could think of doing some sort of loop but wasn't sure....apologies for the confusion & thanks for your help!!
 
Upvote 0

Forum statistics

Threads
1,215,026
Messages
6,122,743
Members
449,094
Latest member
dsharae57

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