1

I wrote the following VBA to loop through the same cells in each column from column 2 to 1002. That is, in the coding, when i=2, I have Range("B3", "B1298"); when i=3, I need to have have Range("C3", "C1298"), and so on. How to achieve this, please? Thank you.

Sub ForwardRScen()

    Application.ScreenUpdating = False

    For i = 2 To 102

        Range("ChgBP").Value = Sheets("sth").Cells(2, i).Value
        Sheets("sth").Range("B3", "B1298").Value = Range("DResults").Value

    Next i

    Application.ScreenUpdating = True

End Sub
3
  • 1
    use Offset Commented Jun 5, 2018 at 10:44
  • @Banana any example code? Commented Jun 5, 2018 at 10:45
  • 2
    Sheets("sth").Range("B3", "B1298").Offset(0,i).Value = Range("DResults").Value and adjust i accordingly Commented Jun 5, 2018 at 10:47

1 Answer 1

2

Assigning a range and changing it in the loop is a good way to go:

Public Sub TestMe()

    Dim i As Long
    For i = 2 To 12
        Dim myRange As Range
        Set myRange = Range(Cells(2, i), Cells(1298, i))
        myRange = i
    Next i

End Sub

This is what you will get:

enter image description here

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.