VBA use string as variable name

RuneKAnd

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?

Sam

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

VBA Creating a variable name by concatenating string and variable?

From Dev

To use a string value as a variable name

From Dev

To use a string value as a variable name

From Dev

VBA: Set Range Name with String variable

From Dev

Use a variable string to name another variable (Python)

From Dev

How to use variable string in index formula in VBA?

From Dev

VBA use public variable when private variable has the same name

From Dev

How to use variable with the same name as String Swift

From Dev

kdb+: use string as variable name

From Dev

C++: Use input string as variable name

From Dev

How to use string value as a variable name in Python?

From Dev

kdb+: use string as variable name

From Dev

How to use a string and/or integers as variable name

From Dev

Use string variable to set object variable in VBA? (Excel 2013)

From Dev

java: how to use String value as the class Name and variable name

From Dev

Is there a way to use a string in a variable's name when calling it?

From Dev

How to use a method's string argument as a method name variable

From Dev

How can i use string variable as an object name in JAVA?

From Dev

VBA use range with variable

From Java

Getting the name of a variable as a string

From Dev

R: variable name to string

From Dev

Getting a string to be a variable name

From Dev

Compare variable name to a string

From Dev

Using a string as a variable name

From Dev

String to variable name Haskell

From Dev

Get the name of a variable in a string

From Dev

Name a variable as string in Matlab

From Dev

Using a string as variable name

From Dev

Function name defined by a variable in VBA

Related Related

HotTag

Archive