Use AND Formula in VBA

tatertot

New Member
Joined
Apr 10, 2016
Messages
31
I created a conditional format in my report to fill the cell in red when this formula = TRUE.

[TABLE="width: 65"]
<tbody>[TR]
[TD="class: xl71, width: 65"]=AND(B16<>B17,B16<>"",B17<>"")

How do I apply this into a macro? I am adding a validation button that will confirm that this AND formula has been corrected. People will see the red but sometimes keep right on working through the report. I want the validation to stop them and make them change the cells value so the AND formula = FALSE.

I have zero idea where to start. The cell I want to "validate" is "B16". Thanks all!!![/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You can add the following code to your validation button code:

Code:
with  sheets("yoursheetname")
If .range("B16") <> .range("B17) then
      msgbox "B16 not equal to B17"
      .range("B16").select
End If

If .range("B16") = "" Then
   msgbox "B16 is empty"
   .range("B16").select
End If

'repeat for B17
End With

Of course, this doesn't really stop the user from proceeding, you'd have to stop your validation code and not let them proceed if one of these errors pops up, perhaps by using "exit sub" after the msgbox
 
Upvote 0
if you want to see how to write the code, you can record a macro and set the conditional format. Then stop recording and check the code.

When you record code it will write the code based on ranges you select and how you manipulate the view of the sheet by scrolling, etc... then you can edit the recorded code to just reference the specific ranges you want and you can remove all the selecting and activate stuff and scrolling code.
 
Last edited:
Upvote 0
When you say stop them? Do you want a popup to keep pestering them until they complete B16?
if so, go to the sheet where they are completing this data, right click, VIEW CODE then paste this in.
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("b16") <> Range("b17") And Range("b16") <> "" And Range("B17") <> "" Then
MsgBox "Validate message - for B16" ' change to whatever
Exit Sub
End If
End Sub
 
Upvote 0
Hi Roderick, when I use this code and attempt to run in VBA, it populates the Macro window to select a macro to run. Is this user error?
 
Upvote 0
Hi Cerfani,

I did that but could not set the recorded macro into an if then statement to generate a message box if the formula = true. Any advice?
 
Upvote 0
you don't run my code, it triggers every time the sheet is changed. meaning it will keep triggering everytime the user inputs/changes something in the sheet until the criteria/conditions aren't met
 
Upvote 0
Your code is only going to set datavalidation on a range. You will not test any condition. When you set the data validation object on a range. One property of the data validation object is where you describe the boolean test

can you paste your recorded code here

Hi Cerfani,

I did that but could not set the recorded macro into an if then statement to generate a message box if the formula = true. Any advice?
 
Upvote 0
meaning to test it, just type something in the sheet where you put it and if your B16 cell is red, it will trigger a popup
 
Upvote 0
Look at this, I recorded this with A1 selected...

Code:
Sub Macro1()'
' Macro1 Macro
'


'
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=AND(B1<>C1,B1<>"""",C1<>"""")"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = "NOPE!!"
        .InputMessage = ""
        .ErrorMessage = "NOPE! NOPE! NOPE!"
        .ShowInput = True
        .ShowError = True
    End With
End Sub

change to...

Code:
Sub Macro1()'
' Macro1 Macro
'


'
    With Range("A1").Validation
        .Delete
        .Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=AND(B1<>C1,B1<>"""",C1<>"""")"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = "NOPE!!"
        .InputMessage = ""
        .ErrorMessage = "NOPE! NOPE! NOPE!"
        .ShowInput = True
        .ShowError = True
    End With
End Sub

that is a simple example... then you can run the subroutine in an event or whenever you want, you can make a button or whatever for it

you can write a macro that will set any cells you want with any kind of validation you want

dont forget to set ignoreblank to true or false depending what you need to do
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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