INDEX and MATCH with VBA

ron.bo

Board Regular
Joined
Mar 15, 2011
Messages
129
Office Version
  1. 2007
Platform
  1. Windows
I need help creating a routine using Index and Match. What I need to do is;

1. Get input from a user input box = Amt
2. Starting at D4 (with range D4..O4) check D4 and if it =1, then
3. formula in D10 = INDEX(DATA!$A$5:DATA!$BB$200,MATCH($B8,DATA!$A$5:$A$200,0),MATCH(D$6,DATA!$A$5:$BB$5,0)) * Amt, then
4. Copy the formula down to D46 then change to value (rather than leaving as formula)
5. Check the next cell to the right of D4 (E4) and if it =1, repeat
6. Loop until the next cell to the right <> 1.

Notes:
1. The formula in the next cell (E4) = INDEX(DATA!$A$5:DATA!$BB$200,MATCH($B8,DATA!$A$5:$A$200,0),MATCH(E$6,DATA!$A$5:$BB$5,0)) * Amt (Note the last MATCH value changed from D$6 to E$6 and must do that for each corresponding formula.
2. I need the routine to start at D4 and loop through O4 looking for any cell in row 4 that has a 1 and then start at that column and continue until the next cell in row 4 <>1.

Thanks,
Ronbo
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try:

Code:
Sub Test()
    Dim Amt As Variant
    Dim Cell As Range
    Amt = InputBox("Enter amount")
    If Amt = "" Then Exit Sub
    For Each Cell In Range("D4:O4")
        If Cell.Value = 1 Then
            With Cell.Offset(6).Resize(37)
                .FormulaR1C1 = _
                    "=INDEX(DATA!R5C1:DATA!R200C54,MATCH(R[-2]C2,DATA!R5C1:R200C1,0),MATCH(R6C,DATA!R5C1:R5C54,0))*" & Amt
                .Value = .Value
            End With
        Else
            Exit Sub
        End If
    Next Cell
End Sub
 
Upvote 0
The routine puts the original amout in D8 and does not increase it by the input amt. Then it fills all of the other cells with the same amount. It is not changing the match cells or increasing it.

Thanks for your help.
 
Upvote 0
The code worked when I tried it. It puts formula is rows 10 to 46 (not row 8). If you want to see the formulas, temporarily comment out this line:

.Value = .Value
 
Upvote 0
I got it. I had other code that was conflicting. Thanks a lot for your time and expertise.

Ronbo.

PS I am new to this site. Is there a way to note that the issue/problem was solved? I did not see anything in the ethics but I did click the "thumbs-up".
 
Upvote 0
PS I am new to this site. Is there a way to note that the issue/problem was solved? I did not see anything in the ethics but I did click the "thumbs-up".

There is no formal way to mark a question as solved. An acknowledgement of the solution is fine.
 
Upvote 0
Andy -

I have another situation, that I did not do a good job explaining in my initial question.

In the range D4..O4 the value can be either 1 or 2. I have a different routine for each. What I need the routine to do is to check each cell in the range starting at D4 and continue until the next cell does not = the strating value some examples,

D4 E4 F4 G4 H4 I4 J4 K4 L4 M4 ... O4

a. 1 1 1 1 1 1 2 2 2 2 ... 2
b. 1 1 2 2 2 2 2 2 2 1 ... 1
c. 2 2 2 2 2 2 2 2 1 1... 1

In example a the routine for 1 would start at D4 and run through I4.
In example a the routine for 2 would start at J4 and run through O4

In example b the routine for 1 would start at D4 and run through E4.
In example b the routine for 2 would start at F4 and run through L4

In example c the routine for 1 would start at L4 and run through O4.
In example c the routine for 2 would start at D4 and run through K4

Again, thanks for the help,
Ronbo
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,277
Members
452,902
Latest member
Knuddeluff

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