VBA If sum value is outside of range

cortexnotion

Board Regular
Joined
Jan 22, 2020
Messages
150
Office Version
  1. 2013
Platform
  1. Windows
Hi All

In need of your help with some VBA I can't get quite right!

The calculation is: (Column G+H) - (Column M+N). Then if the result is lower than -0.1 or greater than 0.1 apply the string value. What I have below is not generating the right results.

Any help appreciated, thanks

VBA Code:
For VRes = 4 To LR3
     If WorksheetFunction.Sum((.Range("G" & VRes).Value + .Range("H" & VRes).Value) - (.Range("M" & VRes).Value - .Range("N" & VRes).Value)) < -0.1 Or _
        WorksheetFunction.Sum((.Range("G" & VRes).Value + .Range("H" & VRes).Value) - (.Range("M" & VRes).Value - .Range("N" & VRes).Value)) > 0.1 Then
                .Range("R" & VRes).Value = "Change Location to XYs " & .Range("G" & VRes).Value & ", " & .Range("H" & VRes).Value
 
Last edited:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
The calculation is: (Column G+H) - (Column M+N).
You have (M-N) in your code, which is correct?
VBA Code:
If Abs((.Range("G" & VRes).Value + .Range("H" & VRes).Value) - (.Range("M" & VRes).Value + .Range("N" & VRes).Value) + 1) > 0.1 Then
                .Range("R" & VRes).Value = "Change Location to XYs " & .Range("G" & VRes).Value & ", " & .Range("H" & VRes).Value
 
Upvote 0
Hi @jasonb75, I'm not following your reply sorry, I need to check if the sum results in either >0.1 or lower than -0.1, anything in between -.0.1 and 0.1 would result in no action.

thanks
 
Upvote 0
You said
The calculation is: (Column G+H) - (Column M+N).
but the calculation in your code was (Column G+H) - (Column M-N) so is the quote correct or is the code correct?

The line of code that I added to my reply checks <-0.1 and >0.1 at the same time by making negative results positive so that you don't need to do everything twice.
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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