1

I'm still pretty new with VBA, and keep getting the subscript out of range error. I am trying to write code that reads data from one excel tab ("ip") and writes some of that data to another tab ("rtf"). When the ip tab has duplicate values for the variable ipIPCMP then I want to add a new row below the active row in my second tab rtf. The error occurs at the else part of the conditional statement. Thank you.

UPDATE: changed else to elseif and changed the with statement. Still get subscript out of range.

For i = 1 To n   

connectIPCMP = iPCMP(i)

    For j = 1 To m

    ipIPCMP = Sheets("ip").Cells(j + 1, 2)

        If ipIPCMP = connectIPCMP And k = 1 Then

            supplierList = Sheets("ip").Cells(j, 3)

            Sheets("rft").Cells(3 + j, 4) = list
            k = k + 1

         ElseIf ipointIPCMP = connectIPCMP And k > 1 Then

             With Worksheets("rtf")

                .Range(.Cells(3 + i, 1), .Cells(3 + i, 8)).Activate
                .ActiveCell.Offset(1).EntireRow.Insert

             End With

            Sheets("rft").Cells(3 + j, 4) = supplierList

            k = k + 1

        End If
    Next

Next

6
  • 3
    Else: ipointIPCMP = connectIPCMP And k > 1 did you mean elseif? Commented Dec 29, 2015 at 16:52
  • Change Else: to ElseIf and add Then to the end of the line. Commented Dec 29, 2015 at 16:55
  • You just need ElseIf. Using only Else means that when the If statement isn't next, no matter what, run what's after Else. Using ElseIf will: When the If statement isn't met, look to the condition in ElseIf to see if that's met..if not, it will continue (in your case, to End If). Commented Dec 29, 2015 at 16:55
  • What are you trying to do with addrow = ... The activate does not work with the =. Are you trying to set a range, or is addrow an array? Commented Dec 29, 2015 at 17:15
  • I want to select a range (a row) and then add a row below that row. Commented Dec 29, 2015 at 17:17

1 Answer 1

3
  1. Change Else: to ElseIf and add Then to the end of the line.
  2. Reference the range properly.

    With Worksheets("rtf")
         .Range(.Cells(3 + i, 1), .Cells(3 + i, 8)).Activate
         ActiveCell.Offset(1).EntireRow.Insert
    End with
    

Note .Cells and not Cells. See this.

Better Approach

    For i = 1 To n
        connectIPCMP = iPCMP(i)
        With Worksheets("rtf")   '<~~CHECK THIS WORKSHEET NAME!!!!!
            For j = 1 To m
                ipIPCMP = Worksheets("ip").Cells(j + 1, 2)
                If ipIPCMP = connectIPCMP And k = 1 Then
                    supplierList = Sheets("ip").Cells(j, 3)
                    .Cells(3 + j, 4) = List
                    k = k + 1
                ElseIf ipointIPCMP = connectIPCMP And k > 1 Then
                    .Cells(3 + i, 1).Offset(1).EntireRow.Insert  '<~~only need a single cell if you are going to insert an entire row
                    .Cells(3 + j, 4) = supplierList
                    k = k + 1
                End If
            Next j
        End With
    Next i

The code sample you provided only tells half of the story but the above is the best I can offer without seeing the full story.


¹ See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals.

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

6 Comments

I changed the with statement now to your suggestion but it still gives me the same error there, With Worksheets("rtf") addrow = Range(.Cells(3 + i, 1), .Cells(3 + i, 8)).Activate End With
You cannot use the = and .Activate like that. Range should be .Range. Please see With ... End With statement.
Thank you for your help! I adjusted the code accordingly but still get the same exact error. I have troubles understanding what should be included within the with statement. Can I include the activate statement in the with statement?
The short answer is Yes. The long answer is Why would you want to? Relying on ActiveCell, Select, etc is unreliable at best and error-prone at worst.
btw, did you know that you are using "rft" in one place and "rtf" in another? Perhaps that is generating the confusion.
|

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.