• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA udf for comparing the sum

guitarist

New Member
Hi,

I am new to excel VBA, could you please help me in getting the user defined function vba code for the below formula highlighted in red.

upload_2016-5-16_23-53-19.png
 

Attachments

  • upload_2016-5-16_23-50-21.png
    upload_2016-5-16_23-50-21.png
    7.9 KB · Views: 10
Do you want a Cell Formula or a VBA user Defined Function?

If you want a UDF then
Code:
Public Function Check_Sum(rng1, rng2) As Variant
If WorksheetFunction.Sum(rng1) = WorksheetFunction.Sum(rng2) Then
  Check_Sum = WorksheetFunction.Sum(rng2)
Else
  Check_Sum = "Wrong"
End If

End Function

will work

To use it:
=Check_Sum(B3:C4,D3:D4)

Copy the code above and place it in a Code Module in VBA

Having supplied the code I'd now recommend against using this approach
This will slow down your model and make auditing more difficult
It can be just as easily achieved with a Function as you have already shown
 
Do you want a Cell Formula or a VBA user Defined Function?

If you want a UDF then
Code:
Public Function Check_Sum(rng1, rng2) As Variant
If WorksheetFunction.Sum(rng1) = WorksheetFunction.Sum(rng2) Then
  Check_Sum = WorksheetFunction.Sum(rng2)
Else
  Check_Sum = "Wrong"
End If

End Function

will work

To use it:
=Check_Sum(B3:C4,D3:D4)

Copy the code above and place it in a Code Module in VBA

Having supplied the code I'd now recommend against using this approach
This will slow down your model and make auditing more difficult
It can be just as easily achieved with a Function as you have already shown

Thanks a lot for the quick response. You made my day :)
God Bless..
 
Back
Top