*Donâ€™t you want to use Excel VBA and want to make a FOR LOOP in Excel using Formulas?*

* In this article, Iâ€™ve shown how you can make one using functions: OFFSET(), MAX(), and MIN()!*

If you know how to code with Excel VBA, youâ€™re blessed ðŸ™‚

If you never wrote code in VBA or want to keep your Excel workbook free of Excel VBA code, then you know: To create a simple loop you have to think out of the box most of the time.

Let me know the background that is encouraging me to write this blog post.

I am the author of some courses in Udemy. One of the courses is on Excel Conditional Formatting. The course title is: **Learn Excel Conditional Formatting with 7 Practical Problems** [to get free access to this course, click here].

In the course discussion board, a student asked me a question as below [screenshot image].

Read the above question carefully and try to solve itâ€¦

**Here are the steps to solve the above problem:**

- Your job is: open a new workbook and input the above values one by one into the worksheet [start from cell
**A2**]. - If youâ€™re done, just select the whole range [from cell
**A2: A31**]. - Click on the
**Conditional Formatting**dropdown in the**Home**ribbon, select**New Rule**from the drop-down,**New Formatting Rule**dialog box appears. - In the
**Select a Rule Type**window, select**Use a formula to determine which cells to format**option. - In the
**Format values where this formula is true**field, type this formula:Â*=OR(OFFSET(A2,MAX(ROW(A$2)-ROW(A2)+3,0),0,MIN(ROW(A2)-ROW(A$2)+1,4),1)-OFFSET(A2,MAX(ROW($A$2)-ROW(A2),-3),0,MIN(ROW(A2)-ROW(A$2)+1,4),1)=3)* - Now select the appropriate format type by clicking on the
**Formatâ€¦**button in the dialog box [I selected Sky Blue background]. - Now click
**OK**two times, youâ€™re done! You get the cells conditionally formatted like the following image.

My motto to write this post is not to show you how to solve the above problem. The motto is: how to make a loop in Excel using 3 Excel functions: OFFSET, MAX, and MIN. You can apply this technique to any loop related Excel formulas.

Let me show you the algorithm to solve the above problem:

- To make you understand the algorithm easily, I will explain the whole thing with two reference cells: cell A8 and A14. In cell A8 and A14, the values are 10 and 20 respectively (above image). If you are used to Excel formulas, then you can smell the OFFSET function, as OFFSET function works with reference points.
- Imagine I am taking the values of cell ranges A5: A8 & A8: A11, and A11: A14 & A14: A17 side by side [image below]. Reference cells are A8 and A14 and I am taking a total of 7 cells around the reference cell. You will get an imaginary picture like the following. From the first part, you can find a pattern from the image. A6 â€“ A9=3, A7-A10=3, there is a pattern. But for the second part, there is no such pattern.
- So letâ€™s build the algorithm with the above pattern keeping in mind. Before building the common formula, I shall show what the formulas will be for the cells A8 and A14 and then will modify the formula to make it common for all. For a reference point (like A8 or A14), we shall take a total of 7 cells around it (including the reference point) and place them side by side in the formula creating arrays. Then we shall find out the difference of the arrays if any of the differences is equal to 3 that the reference cell will be TRUE valued.
- We can do that easily using OFFSET function as OFFSET function returns array. Say for cell reference A8, I can write the formula like this:
*=OR(OFFSET(A8, 0, 0, 4, 1)-OFFSET(A8, -3, 0, 4, 1)=3).Â*What this formula will return? The first offset function of the formula will return array: {10; 11; 12; 15}, second offset function will return array {5; 8; 9; 10}. And you know {10; 11; 12; 15} â€“ {5; 8; 9; 10} = {10-5; 11-8; 12-9; 15-10} = {5; 3; 3; 5}. When this array is logical tested with =3 then Excel calculates internally like this: {5=3; 3=3; 3=3; 5=3} = {False; True; True; False}. When OR function is applied on this array: OR({False; True; False; True}, we get TRUE. So cell A8 gets true values as returned. - I think you have got the whole concept of how this algorithm is going to work. Now there is a problem. This formula can work from cell A5, above cell A5, there are 3 cells. But for cells A2, A3, and A4 this formula cannot work. So the formula should be modified for these cells.
- For cells A2 to A4, we want that the formula will not take into consideration the upper 3 cells. For example, for cell A3, our formula will not be like the formula for cell A8:
*=OR(OFFSET(A8, 0, 0, 4, 1)-OFFSET(A8, -3, 0, 4, 1)=3).Â* - For cell A2, the formula will be like:Â
*=OR(OFFSET(A2, 3, 0, 1, 1)-OFFSET(A2, 0, 0, 1, 1)=3).* - For cell A3, the formula will be like:Â
*=OR(OFFSET(A3, 2, 0, 2, 1)-OFFSET(A3, -1, 0, 2, 1)=3).* - For cell A4, the formula will be like:Â
*=OR(OFFSET(A4, 1, 0, 3, 1)-OFFSET(A3, -2, 0, 3, 1)=3).* - For cell A5, the formula will be like:Â
*=OR(OFFSET(A5, 0, 0, 4, 1)-OFFSET(A5,-3, 0, 4, 1)=3); [this is the general formula]* - For cell A6, the formula will be like:Â
*=OR(OFFSET(A6, 0, 0, 4, 1)-OFFSET(A6,-3, 0, 4, 1)=3); [this is the general formula]* - Do you find some patterns from the above formulas? The first Offset functionâ€™s
**rows**argument has decreased from 3 to 0; the**height**argument has increased from 1 to 4. The second Offset functionâ€™s**rows**argument has decreased from 0 to -3 and height argument has increased from 1 to 4. - First Offset functionâ€™s
**rows**argument will be modified like this:Â*MAX(ROW(A$2)-ROW(A2)+3,0)* - The second Offset functionâ€™s
**rows**argument will be modified like this:*MAX(ROW($A$2)-ROW(A2),-3)Â* - The First Offset functionâ€™s
**height**argument will be modified like this:Â*MIN(ROW(A2)-ROW(A$2)+1,4)* - Second Offset functionâ€™s
**height**argument will be modified like this:Â*MIN(ROW(A2)-ROW(A$2)+1,4)* - Try to understand the above modification. These are not that tough to understand. All these four modifications are working as
**FOR LOOPS**of Excel VBA but Iâ€™ve built them with Excel Formulas. - So you got the ways how the general formula works for the cells from A2: A31.

So we were talking about Looping in Excel spreadsheets. This is a perfect example of looping in Excel. Every time the formula takes 7 cells and works on the cells to find out a specific value.

If you have any feedback to add to this post, please feel free to put it in the comment box below.

**Table of Contents**hide

**Read More:Â **

**Read More:Â**

*How to Use the Do While Loop in Excel VBA*

*For Next Loop in VBA Excel (How to Step and Exit Loop)*

## Download Working File

Download the working file that Iâ€™ve used to create this blog post from the link below:

Hi Kawser,

nice post. However, I couldn’t solve this loop using offset.. Any ideas?

what i’m trying to do is add the letter “A” in front of the string “BCD” until length of the string becomes 10. (for which A would need to be added 7 times in front of “BCD” to make it “AAAAAAABCD”

If cell A1 holds your string “BCD” and you want to show “AAAAAAABCD” in cell B1, input this formula in cell B1: =REPT(“A”, 10-LEN(A1))&A1.

To solve this problem, you don’t have to use any looping. Or, do you have some other requirement for this problem? Let me know.

Best regards.

hi,

Thank you for details information.

I have 2 columns of numbers , say each column 10 numbers , total 20 numbers.

I want delete the similar numbers in both columns and keep just the numbers are different. Numbers need be sorted from small to large too.

this is urgent case.

Any comment I appreciate.

can i know how can i make the rows change as per the set values. Ex:

If i entered 5, rows in excel=5 & if 6, rows in excel=6 & so on…

Great formula but I can’t understand how to incorporate it into a SUMIFS formula that I need to perform a loop.

I think there is a slight error in this formula, for cell A2 u r using =OR(OFFSET(A2, 3, 0, 1, 1)-OFFSET(A2, 0, 0, 1, 1)=3). It means if A5 – A2 = 3 then highlight. In the given case the first four values are 1,2,3,5 hence it worked. If the values are like 1,3,2,4 then it will not work as it highlights the cells even when they do not meet the criteria

I think there is an error in the formula, for cell A2 you are using =OR(OFFSET(A2, 3, 0, 1, 1)-OFFSET(A2, 0, 0, 1, 1)=3). It means if Cell A5-A2=3 then highlight, it works for the above given data as the first four values starting from A2 are 1,2,3,5. But if the values are 1,5,2,4 (for example) it highlights them even if they do not meet the criteria.

The criteria of this problem supposed that the data are sorted. Your examples aren’t in sorted order.