2

I have workbooks with datasets (of varying lengths) from measurements in different rooms. I need to sort each datasets. My code finds the start and end rows of each dataset and stores them as StartRowRoom1, EndRowRoom1, StartRowRoom2, EndRowRoom2 etc.

I want to go through each dataset in a while loop like this.

Dim StartRowRoom1 As Integer
Dim StartRowRoom2 As Integer
Dim EndRowRoom1 As Integer
Dim EndRowRoom2 As Integer

n = 1

While n < NumberOfRooms
    startRow = "StartRowRoom" & n
    endRow = "EndRowRoom" & n

    With Range(Cells(startRow, 4), Cells(endRow, 4))
        .FormulaR1C1 = "=RC[-2]+RC[-1]"
        #sorting and graph creation
    End With
    n = n + 1
Wend

My problem is that the startRow and endRow variables are strings ("StartRowRoom1" and "EndRowRoom1", for n=1). so they cannot be used in Cells(). I want them to refer to the variables defined as integers. Does anyone have a solution?

2 Answers 2

1

This is what arrays are for.

You should declare your variables as

Dim StartRowRoom(1 to 2) As Integer
Dim EndRowRoom(1 to 2) As Integer

StartRowRoom(1) = [your value here]
StartRowRoom(2) = [your value here]

EndRowRoom(1) = [your value here]
EndRowRoom(2) = [your value here]

Then you can access them as

startRow = StartRowRoom(n)
endRow = EndRowRoom(n)

using n as the index

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

1 Comment

Arrays, Collections or Dictionaries
0

I think you should try to store the StartRowRoom1 variables like Range type and use .offset() and .end() in getting that references.

Then in your loop you don't need the Cells(,):

With Range(startRow, endRow) ...

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.