3

I would like to loop through the amount of rows that are entered in the Input box +1.

Sub LoopEnter()
    myNum = Application.InputBox("Enter number")
    For Each r In Range ("A2":"A" & myNum +1)
        r.Offset (0.1) = "N"& r
        Next r
    End Sub

However, ("A2":"A" & myNum +1) is not recognized as a range. What's the correct way to make the range, for example (A2:A41) if the number entered in the InputBox is 40?

3
  • Use "N" & r.Row to get row number Commented Jul 20, 2018 at 9:24
  • Well spotted. I updated my answer to reflect your insight :0) Commented Jul 20, 2018 at 9:30
  • You could also use Range ("A2").resize(myNum) and avoid concatenating. Commented Jul 20, 2018 at 9:32

1 Answer 1

7

Try this ...

Sub LoopEnter()
    myNum = Application.InputBox("Enter number")
    For Each r In Range ("A2:A" & myNum +1)
        r.Offset (0,1) = "N" & r.Row
    Next r
End Sub

You had the : in the wrong place ... it should be inside the string, not between two strings!


Updated as suggested by @Santosh, as you'd also missed .Row out ... r is a Range object, r.Row is the row number property of that Range object.

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

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.