0

I have an issue with my VBA code. I try to go through a whole table that has a lot of data. I go through a first column with a first condition required. Once this condition is complete, I go through the column next to the first one but starting at the same position I stopped the previous one. Once the second condition is complete, I try to do a copy paste. But for some reasons I got the error "Subscript out of Range" Could you please help me?

Here is the code:

Sub Match()

Dim i As Integer
i = 0
Dim j As Integer
Do
    i = i + 1
Loop Until Sheets("Sheet1").Range("A1").Offset(i, 0).Text = Sheets("Sheet2").Range("I5").Text
j = i
Do
    j = j + 1
Loop Until Sheets("Sheet1").Range("B1").Offset(j, 0).Value = Sheets("Sheet2").Range("I11").Value

Sheets("Sheet1").Range("C1").Offset(j, 0).Copy
Sheets("Sheet2").Range("N11").Paste

End Sub

Thanks guys

4
  • 1
    Are you sure your values ever line up? If the values you've hardcoded to check for matching don't line up, then it will run forever. Commented Jul 31, 2015 at 15:30
  • Make it simple. Which line give you error? let me know! Commented Jul 31, 2015 at 15:38
  • Yes the values line up. The first do loop don't even execute but I don't know why. Commented Jul 31, 2015 at 15:40
  • Right now you loop in A , going down the rows until you find the value of I5. Then you move on to column B. You start at the row AFTER the one where you found I5 in A and you move down until you find I11. When you find I11 in B, you copy the cell next to it (in C) and you paste in Sheet2, N11. That's what you want to do? Commented Jul 31, 2015 at 15:49

3 Answers 3

1

This should do the same thing without any loops:

Sub Match()

Dim lastA As Long, lastB As Long
Dim i As Long, j As Long

With Sheets("Sheet1")
    last a = .Cells(.Rows.count, 1).End(xlUp).Row
    last b = .Cells(.Rows.count, 2).End(xlUp).Row
End With

i = WorksheetFunction.Match(Sheets("Sheet2").Range("I5").Text, Sheets("Sheet1").Range("A:A"), 0)
j = WorksheetFunction.Match(Sheets("Sheet2").Range("I11").value, Sheets("Sheet1").Range("B" & i & ":B" & lastB), 0)

Sheets("Sheet2").Range("N11").value = Sheets("Sheet1").Cells(j, 3).value

End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

You could actually retrieve the same result just using an INDEX/MATCH formula without using any VBA
0

I didn't get the same error as you but I changed the last line and it seems to work.

Sub Match()

Dim i As Integer
i = 0
Dim j As Integer
Do
    i = i + 1
Loop Until Sheets("Sheet1").Range("A1").Offset(i, 0).Text = Sheets("Sheet2").Range("I5").Text
j = i
Do
    j = j + 1
Loop Until Sheets("Sheet1").Range("B1").Offset(j, 0).Value = Sheets("Sheet2").Range("I11").Value

Sheets("Sheet1").Range("C1").Offset(j, 0).Copy Destination:=Sheets("Sheet2").Range("N11")

End Sub

I did notice that your code runs for ever if you do not get a match which is not good. You may want to add a solution to this. It can be as easy as adding Or i > 10000 on the Loop Until lines.

Comments

0

I modified your code slightly:

Sub Match()

    Dim i As Integer
    i = 0
    Dim j As Integer

    Do
        i = i + 1
    Loop Until Sheets("Sheet1").Range("A1").Offset(i, 0).Text = Sheets("Sheet2").Range("I5").Text

    j = i
    Do
        j = j + 1
    Loop Until Sheets("Sheet1").Range("B1").Offset(j, 0).Value = Sheets("Sheet2").Range("I11").Value
    Sheets("Sheet1").Range("C1").Offset(j, 0).Copy Sheets("Sheet2").Range("N11")
End Sub

and it worked fine with data like:

enter image description here

In Sheet1.

Note the B match must be below the A match.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.