Building dynamic variable names

  • Thread starter Thread starter andycharger
  • Start date Start date
A

andycharger

Can someone help me with this?

I have declared a load of variables at the top of my code, replacin
the last digits with the month number. I have also declared a tem
varable to get a month number out of my row

e.g.

Dim strFD1 as Integer
Dim strFD2 as Integer
etc......

Dim strTemp as Integer

strTemp = Cells(rows, "A").value


Now im going through a loop statement and checking one of the column
and adding the value to whichever month variable needs the value.

i.e

strFD(strTemp) = strFD(strtemp) + Cells(Rows, "B").value

Obviously this wont work but I need to make the variable name
combination of strFD and the value from strTemp.

Can anyone tell me how I do this properly?

Thank
 
You need to use an array

Dim strFd(1 to 12) as Integer

strFd(cLng(strTemp)) = strFd(cLng(strTemp)) + Cells(Rows, "B").value
 
Hi,

use arrays instead of variables.

1. Find Min and Max Value of strTemp

Dim intMin As Integer, intMax As Integer

Dim objCell As Range


intMin = 32000

intMax = -32000

For Each objCell In Range("TheNameOfYourRange")

If Cint(objCell.Value) > intMax Then intMax = Cint(objCell.Value)

If Cint(objCell.Value) < intMin Then intMin =

Cint(objCell.Value)

Next objCell


2. Declare an array that spans from min to max

Dim aintFD() As Integer

ReDim aintFD(intMin To intMax) As Integer


3. Loop

For i = intMin To intMax

aintFD(strTemp) = aintFD(strtemp) + Range("B" & i).value

Next i

4. Care for error handling

Greetings,

Holg.
 
It all works fine but when I hover over the variables, I noticed it i
turning my value of 389.09 into 389!
Does a integer not use decimal points?

If not, what is the correct syntax for what I should assign m
variables to?

These are monetary fields
 
Depends on where you declare it and where you want to use it.

If inside a procedure, then yes.

If at the top of a module, then for module visibility, yes. For project
visibility change Dim to Public
 
Use double or currency.

Integer is just that - a whole number

Are you really dynamically determining that the minimum month number is 1
and the maximum is 12?
 
Back
Top