Loop though table with dynamic IF Statement using VBA

dcoker

New Member
Joined
Dec 13, 2018
Messages
36
Can I loop through a set of data and determine if the current item is within a range of each previous items? In the attached example photo, I have the data. I want to check the following:

If TextBox 24.Top is between TextBox 7.Top and TextBox 7.Bottom OR if TextBox 24.Left is between TextBox 7.Left and TextBox 7.Right. Check if either is true and return true.

Loop to the next TextBox

If TextBox 27.Top is between TextBox. 24.Top and TextBox 24.Bottom OR if TextBox 27.Left is between TextBox 24.Left and TextBox 24.Right
OR TextBox 27.Top is between TextBox.7.Top and TextBox7.Bottom OR if TextBox27.Left is between TextBox7.Left and TextBox7.Right. Check if any is true and return true.

Loop to the next TextBox


I am trying to get the IF/OR statement to grow so that each range of values from all the previous text boxes are checked against the current text box. Is this possible? I have this data stored in a collection as well using VBA. I am just not sure how to proceed. Thanks for reading!
 

Attachments

  • Example Data.PNG
    Example Data.PNG
    33.2 KB · Views: 12

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
It can be done with a formula. Try:

Dante Amor
IJKLMN
1NameTopLeftBottomRightResult
2TextBox 7235240245267
3TextBox 24245219254242TRUE
4TextBox 27291283300315FALSE
5TextBox 39351297360329TRUE
6TextBox 43269288279320TRUE
Hoja2
Cell Formulas
RangeFormula
N3:N6N3=IF(COUNTIFS($J$2:J2,"<="&J3,$L$2:L2,">="&J3)+COUNTIFS($K$2:K2,"<="&K3,$M$2:M2,">="&K3)>0,TRUE)


NOTE XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
---------
 
Upvote 0
It can be done with a formula. Try:

Dante Amor
IJKLMN
1NameTopLeftBottomRightResult
2TextBox 7235240245267
3TextBox 24245219254242TRUE
4TextBox 27291283300315FALSE
5TextBox 39351297360329TRUE
6TextBox 43269288279320TRUE
Hoja2
Cell Formulas
RangeFormula
N3:N6N3=IF(COUNTIFS($J$2:J2,"<="&J3,$L$2:L2,">="&J3)+COUNTIFS($K$2:K2,"<="&K3,$M$2:M2,">="&K3)>0,TRUE)


NOTE XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
---------
Thank you. I think I can add this in the worksheet and use VBA to check that column. Thanks!
 
Upvote 0
Thank you. I think I can add this in the worksheet and use VBA to check that column. Thanks!

It can be done with a formula. Try:

Dante Amor
IJKLMN
1NameTopLeftBottomRightResult
2TextBox 7235240245267
3TextBox 24245219254242TRUE
4TextBox 27291283300315FALSE
5TextBox 39351297360329TRUE
6TextBox 43269288279320TRUE
Hoja2
Cell Formulas
RangeFormula
N3:N6N3=IF(COUNTIFS($J$2:J2,"<="&J3,$L$2:L2,">="&J3)+COUNTIFS($K$2:K2,"<="&K3,$M$2:M2,">="&K3)>0,TRUE)


NOTE XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
---------
Thank you, DanteAmor. The XL2BB Add-In is giving me problems when trying to create a mini-sheet of the data and freezes up excel forcing me to End Task in the Task Manager. I believe the formula is not quite right, since the formula increases a range of criteria, so it it seems to eventually returns true for all subsequent text boxes. I added a link to the workbook below.

One Drive Link to workbook
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,039
Members
448,940
Latest member
mdusw

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