Message box "OK"

mira2020

New Member
Joined
Sep 25, 2020
Messages
28
Office Version
  1. 2016
hi all,

how can i write a VBA code to check value in 2 column
for row 2 to check if any value in column A is Yes -> display" Quantity is wrong"
if any value in column B is Yes -> display " Price is wrong"
Message only with "OK " button. If user click OK, just stop the VBA and do nothing
only when both column A & B are No, then continue doing the rest of the code

1702304310902.png


thanks
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
VBA Code:
Sub test()
Dim msgstr1 As String, msgstr2 As String
If Application.WorksheetFunction.CountIf(Range("A:A"), "Yes") Then msgstr1 = "Quantity is wrong"
If Application.WorksheetFunction.CountIf(Range("B:B"), "Yes") Then msgstr2 = "Price is wrong"
If Len(msgstr1 & msgstr2) Then MsgBox msgstr1 & Chr(10) & msgstr2, vbOKOnly: Exit Sub
'Rest of Code here
End Sub
 
Upvote 0
as an example

VBA Code:
Sub abc()
Dim TmMatch As Long

TmMatch = WorksheetFunction.Match("yes", Range("A2"), 0)
If TmMatch > 0 Then
    MsgBox "Quantity is wrong in row " & TmMatch
Exit sub
End If
TmMatch = WorksheetFunction.Match("yes", Range("B2"), 0)
If TmMatch > 0 Then
    MsgBox "Price is wrong in row " & TmMatch
Exit sub
End If
End Sub
 
Last edited:
Upvote 0
VBA Code:
Sub CheckQuantityPrice()
    Dim A As Boolean
    Dim B As Boolean
    A = Sheet1.Range("A2").Value = "Yes"
    B = Sheet1.Range("b2").Value = "Yes"
    If A = False And B = False Then
        'doing the rest of the code
    ElseIf A Then
        VBA.MsgBox "Quantity Is wrong", vbCritical, "Wrong"
    ElseIf B Then
        VBA.MsgBox "Price is wrong", vbCritical, "Wrong"
    End If
End Sub
 
Upvote 0
Fix it
VBA Code:
Sub CheckQuantityPrice()
    Dim iRange As Range
    Dim A As Boolean
    Dim B As Boolean
    Set iRange = Sheet1.Range("A:A").Find("yes")
    A = iRange Is Nothing
    Set iRange = Sheet1.Range("B:B").Find("yes")
    B = iRange Is Nothing
    If A And B Then
        'doing the rest of the code
    ElseIf A = False Then
        VBA.MsgBox "Quantity Is wrong", vbCritical, "Wrong"
    ElseIf B = False Then
        VBA.MsgBox "Price is wrong", vbCritical, "Wrong"
    End If
End Sub
 
Upvote 0
How about.

VBA Code:
Sub abc()
Dim TmMatch As Long

TmMatch = WorksheetFunction.Match("yes", Range("A2"), 0)
TmMatch = WorksheetFunction.Match("yes", Range("B2"), 0)

If TmMatch > 0 And TmMatch > 0 Then
    MsgBox "Quantity is wrong in row "
    Exit sub
Else If TmMatch > 0 Then
    MsgBox "Quantity is wrong in row "
    Exit sub
Else If TmMatch > 0 Then
    MsgBox "Price is wrong in row "
    Exit sub
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
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