Vba code for an if and statements

ifu06416

Board Regular
Joined
Sep 5, 2011
Messages
56
Office Version
  1. 365
Hello,

I have a table where users enter data. The final entry, in col M, is their name.

I would like to create an VBA code that will display a msg box if the user enters their name in col M without having entered a date in col J.

I've written the following if function which seems to do the job but I don't know how to change it into VBA

=IF(AND(M12="",J12=""),"",IF(AND(M12="",J12<>""),"",IF(AND(J12<>"",M12<>""),"","DATE NEEDED")))

Any help would be appreciated.

John.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Your formula will probably simplify to
=IF(AND(J12="",M12<>""),"date needed","")
In VBA this could be
Code:
if range("J12") = "" and range("M12") <> "" then msgbox "date needed"

But I'd want to see more of your code so I can see it in context - how it's called, whether other ranges are affected etc., because this single line isn't complete
 
Upvote 0
Something like

Code:
If Cells(12,13)<>"" and Cells(12,10)="" Then
    MsgBox "Date Needed"
End If
 
Upvote 0
Hi Baitmaster,

There isn't actually any further code, this will be the only macro in use.

I've applied your code in the following way

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("J12") = "" And Range("M12") <> "" Then
MsgBox "date needed"
End If
End Sub

It works great for row 12 but how would I edit this code so that it applies to, say rows 12 down to row 5000?

Thanks for the reply.

John.
 
Upvote 0
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDates As Range: Set rngDates = Range("J12:J5000")
Dim rngNames As Range: Set rngNames = Range("M12:M5000")
If Not Intersect(Target, Union(rngDates, rngNames)) Is Nothing Then
    If Intersect(rngDates, Target.EntireRow) = "" And Intersect(rngNames, Target.EntireRow) <> "" Then MsgBox "date needed: row " & Target.Row
End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,121
Members
452,381
Latest member
Nova88

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