Macro to show Message where that is a variance

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,601
Office Version
  1. 2021
Platform
  1. Windows
I have a spreadsheet, which contains the word variance in column A and and value in column D that is in line with the word "variance".

I have written code that will advise me where there is a value that is either greater or less that zero in column D in line with the word "variance", but cannot get it to work propertly. I have also attached sample data below. It would be appreciated if you could assist me

Sub Variance_Message()
Sheets("sheet1").Select
Dim ws As Worksheet, r As Range, msg As String, ff As String
For Each ws In Sheets
Set r = ws.Columns("b").Find("Variance")
If Not r Is Nothing Then
ff = r.Address
Do
If (r.Offset(, 3).Value< 0) + (r.Offset(, 3).Value > 0) Then
msg = msg & ws.Name & r.Address(0, 0)
End If
Set r = ws.Columns("b").FindNext(r)
Loop Until ff = r.Address
End If
Next
MsgBox IIf(Len(msg) > 0, msg, "No Variances Found")
End Sub

Variances.xls
ABCD
5
6BER111450.00
711460.00
811470.00
9114825.00
101149180.00
1111800.00
12
13Variance205.00
14
15BER211450.00
1611460.00
1711470.00
18114835.00
1911490.00
2011800.00
21
22Variance35.00
23
Sheet1
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi Howard,

Try this modified version:
Code:
Sub Variance_Message()
    Dim ws As Worksheet, r As Range, msg As String, ff As String
    For Each ws In ActiveWorkbook.Sheets
        Set r = ws.Columns("A").Find(what:="Variance", LookIn:=xlValues)
        If Not r Is Nothing Then
            ff = r.Address
            Do
                If (r.Offset(, 3).value < 0) Or (r.Offset(, 3).value > 0) Then
                        msg = msg & ws.Name & "." & r.Address & vbLf
                End If
                Set r = ws.Columns("A").FindNext(r)
            Loop Until ff = r.Address
        End If
    Next
    MsgBox IIf(Len(msg) > 0, msg, "No Variances Found")
End Sub

The primary change was the need for an Or operator instead of + to test if your value in Column D is not 0.

Alternatively, you could replace that line with:
Code:
                If (r.Offset(, 3).value <> 0) Then

I'm guessing your reference to Column "b" instead of "a" was merely the
result of deleting a column prior to transfering your code and screen shot
to this Board.
 
Upvote 0
Hi Jerry

Thanks for the help, much appreciated

Regards

Howard
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,351
Members
452,907
Latest member
Roland Deschain

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