VBA - Clear cell if value within any individual set of ranges nested in another range

teacoffee

New Member
Joined
May 5, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello. I've been scouring the internet looking for an example of this and I have not found anything similar to what I'm trying to do. I have this table below:

TestTest Against
4.015​
1.011​
1.010​
2.022​
3.031​
3.009​
5.050​
4.011​
2.020​
5.001​

I'm trying to run a loop starting at the first cell in the Test column moving downward one cell at a time and testing if the selected cell's value is within +/- 0.020 of ANY of the numbers in the Test Against column. If the selected number is not within any range applied to each cell's value in Test Against, then I would like to clear the contents of that Test cell. I've seen ways to test if the exact value of a selected cell is in another range, but not a way to test against only fixed +/- ranges applied to every individual cell in the range being tested against.

In this example, I would like to write a script that clears the contents of only cells 3.031 and 5.050 because neither of those values fall within +/- 0.020 of any cell's value in the Test Against column.

Thank you ahead of time for any help with this.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Welcome to the forum,

Try below code ... Assuming your data starts from cell A1
VBA Code:
Sub test()

Dim a
a = [A1].CurrentRegion

For x = 2 To UBound(a)
   For y = 2 To UBound(a)
      If Abs(a(x, 1) - a(y, 2)) < 0.02 Then Exit For
      If y = UBound(a) Then a(x, 1) = ""
   Next
Next

[A1].Resize(UBound(a), UBound(a, 2)) = a

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,919
Messages
6,175,368
Members
452,638
Latest member
Oluwabukunmi

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