Message box to show if there is a value that is not zero in certain cells when opening workbook

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,701
Office Version
  1. 2019
Platform
  1. Windows
I have tried to write code to show a message if either H11 or H24 has a value that is not zero

No message box pops up where there is a zero that is not zero

Code:
 Private Sub Variance()
With Sheets("Sales Recon")
If .Range("H11") Or .Range("H24") <> 0 Then
MsgBox "There is a variance"

 
End With

End Sub


It would be appreciated if someone could amend this
 

Some videos you may like

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Saurabhj

Active Member
Joined
Jun 6, 2020
Messages
398
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi,

Below code will show message box when either H11 or H24 has non-zero value.

VBA Code:
Private Sub Variance()
    With Sheets("Sales Recon")
        If .Range("H11") <> 0 Or .Range("H24") <> 0 Then
            MsgBox "There is a variance"
        End If
    End With

End Sub
 

Saurabhj

Active Member
Joined
Jun 6, 2020
Messages
398
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi Howard,

The code is showing the message box. See Image.

Do you need to display any specific message/ value ?

Thanks,
Saurabh
 

Attachments

  • variance.PNG
    variance.PNG
    31.5 KB · Views: 1

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,701
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

Thanks for the reply

I placed this code in the workbook module and when I open the workbook the message box showing the message does not appear

Not sure why message box does not display on my workbook as per your image
 

Saurabhj

Active Member
Joined
Jun 6, 2020
Messages
398
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi Howard,

If you like to show message box when workbook opens then write macro on workbook open event.

Replace your code from below.

VBA Code:
Private Sub Workbook_Open()
        With Sheets("Sales Recon")
        If .Range("H11") <> 0 Or .Range("H24") <> 0 Then
            MsgBox "There is a variance"
        End If
    End With
End Sub
 

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,701
Office Version
  1. 2019
Platform
  1. Windows
Many thanks for the help. It works perfectly
 

Watch MrExcel Video

Forum statistics

Threads
1,122,984
Messages
5,599,190
Members
414,296
Latest member
nachname

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
Top