VBA code to display where there is a value in Col D next to "Variance"

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,585
Office Version
  1. 2021
Platform
  1. Windows
I have written code to check if there is a value not equal to zero in Col D in the same row as where text "variance" appears in Col A


I want the code to check if there is a variance (Value not equal to zero) in Col D on all sheets, except the last sheet


The code runs, but does not display a message if there are any variances <> 0


In the example below Cell D5 must be displayed as there is a variance <>

Kindly amend my code pertaining to the above


Book1
ABCD
1BR11270-75217
2BR2127150000
3BR3127525619
4
5Variance402
6
7
8
9BR11785-85200
10BR2177645000
11BR3177240200
12
13Variance0
14
15
16
Sheet1
Cell Formulas
RangeFormula
D5=SUM(D1:D4)
D13=SUM(D9:D12)



Code:
 Sub Variance_Message()
With Application
.CutCopyMode = False
.Calculation = xlCalculationManual
End With

Dim ws As Worksheet, r As Range, msg As String, ff As String, I As Long
For I = 1 To Count

For Each ws In Sheets(I)




    Set r = ws.Columns("a").Find("Variance")
    If Not r Is Nothing Then
        ff = r.Address
        Do
            If Round(r.Offset(, 3).Value, 2) <> 0 Then
            
                           msg = msg & ws.Name & r.Address(0, 0)
            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")
Next I

With Application
.CutCopyMode = False
.Calculation = xlCalculationAutomatic
End With


End Sub
 
Last edited:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi,
not tested but see if this update to your code does what you want

Code:
Sub Variance_Message()
Dim ws As Worksheet
Dim r As Range
Dim msg As String, ff As String
Dim I As Long


For Each ws In ThisWorkbook.Worksheets
    Set r = ws.Columns("a").Find("Variance")
    If Not r Is Nothing Then
        ff = r.Address
        Do
            If Val(r.Offset(, 3).Text) <> 0 Then
             msg = msg & ws.Name & " " & r.Address(0, 0) & Chr(10)
            End If
            Set r = ws.Columns("a").FindNext(r)
        Loop Until ff = r.Address
    End If
    
    Set r = Nothing
Next




MsgBox IIf(Len(msg) > 0, msg, "No Variances Found")


End Sub

Dave
 
Upvote 0
Thanks for the help. I only shows if there is a variance in Col D on the last sheet where variance appears in Col A in the same row


I want the message box to show the cell address for each sheet , except the last sheet where there is a value in Col D <> 0 where "Variance" appears in Col A

Kindly amend your code
 
Upvote 0
I want the message box to show the cell address for each sheet , except the last sheet where there is a value in Col D <> 0 where "Variance" appears in Col A


Sorry, omitted that part of code - see if this update helps

Code:
Sub Variance_Message()
Dim ws As Worksheet
Dim r As Range
Dim msg As String, ff As String
Dim I As Long


For Each ws In ThisWorkbook.Worksheets
    Set r = ws.Columns("a").Find("Variance", lookat:=xlWhole, LookIn:=xlValues)
'ignore last sheet
    If ws.Index = Sheets.Count Then Exit For
    
    If Not r Is Nothing Then
        ff = r.Address
        Do
            If Val(r.Offset(, 3).Text) <> 0 Then
             msg = msg & ws.Name & " " & r.Address(0, 0) & Chr(10)
            End If
            Set r = ws.Columns("a").FindNext(r)
        Loop Until ff = r.Address
    End If
    
    Set r = Nothing
Next




MsgBox IIf(Len(msg) > 0, msg, "No Variances Found")


End Sub


Code should Find Text "Variance" in each but the last worksheet & if value in Column D on same Row <> 0 then add Sheet Name & Cell Address to msg variable.

Dave
 
Upvote 0
Thanks for the updated code

Need one more amendment please


If there variance is in debit in Col D where variance appears in the same row in Col A , then the message displayed. However, if the variance is in credit, the cell address containing the credit variance in Col D where text variance appears in Col A in the same row is not displayed
 
Upvote 0
try changing this line:

Code:
If Val(r.Offset(, 3).Text) <> 0 Then


to this

Code:
If Val(r.Offset(, 3).Text) < 0 Then

and see if this does what you want

Dave
 
Upvote 0
Hi Dave

Thanks for the help. need to show values > 0 as well as values < 0 In Col D where text "Variance" appears in Col A in the same row


I have amended the code as follows and it works perfectly


Code:
 Sub Variance_Message()

Dim ws As Worksheet
Dim r As Range
Dim msg As String, ff As String
Dim I As Long


For Each ws In ThisWorkbook.Worksheets
    Set r = ws.Columns("a").Find("Variance", lookat:=xlWhole, LookIn:=xlValues)
'ignore last sheet
    If ws.Index = Sheets.Count Then Exit For
     
    If Not r Is Nothing Then
        ff = r.Address
        Do
             If Round(r.Offset(, 3).Value, 2) <> 0 Then
            
                           msg = msg & ws.Name & r.Address(0, 0)
            End If
            Set r = ws.Columns("a").FindNext(r)
        Loop Until ff = r.Address
    End If
    
    Set r = Nothing
Next




MsgBox IIf(Len(msg) > 0, msg, "No Variances Found")


End Sub
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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