Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Store values in variables based on count

I'm trying to write a macro that declares variables in a dim statement where
the number of variables are based on a count (number of cells in a range for
example). To illustrate I present the following macro :

Sub SetVariableNames()

Dim myVarRange As Range
Dim myCell(1 To 6) As Variant
Dim x As Integer
Dim i As Integer

'Assigns values to myCell based on contents of myRange

i = 1
x = ActiveSheet.Range("myRange").Count
Set myVarRange = Worksheets(1).Range("myRange")

For i = 1 To x
myCell(i) = myVarRange.Cells(i).Value
Next i

End Sub

The above macro work fine becasue I specified in the dim statement that I
had 6 values to assign to myCell(1 to 6). However, I would like the macro
to set the upper range based on the count of cells in the selected range so
that values could be stored to a variable based on each value in the range
which could change. I can't figure out how to do that. The variables would
be used elsewhere in the routine woth an action based on the variable's
value. I originally tried myCell(1 to X) but I get a variable not defined
error.

Thanks in advance for the help!

Microsmith
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Store values in variables based on count

You could use ReDim:

Dim myCell As Variant
Dim x As Long

x = ActiveSheet.Range("myRange").Count
ReDim myCell(1 to x)

but you might also want to consider simply using myCell as a
two-dimensional array):

Dim myCell As Variant
Dim i As Long

myCell = Worksheets(1).Range("myRange").Value
For i = 1 To UBound(myCell, 1)
Debug.Print myCell(i, 1)
Next i





In article ,
Microsmith wrote:

I'm trying to write a macro that declares variables in a dim statement where
the number of variables are based on a count (number of cells in a range for
example). To illustrate I present the following macro :

Sub SetVariableNames()

Dim myVarRange As Range
Dim myCell(1 To 6) As Variant
Dim x As Integer
Dim i As Integer

'Assigns values to myCell based on contents of myRange

i = 1
x = ActiveSheet.Range("myRange").Count
Set myVarRange = Worksheets(1).Range("myRange")

For i = 1 To x
myCell(i) = myVarRange.Cells(i).Value
Next i

End Sub

The above macro work fine becasue I specified in the dim statement that I
had 6 values to assign to myCell(1 to 6). However, I would like the macro
to set the upper range based on the count of cells in the selected range so
that values could be stored to a variable based on each value in the range
which could change. I can't figure out how to do that. The variables would
be used elsewhere in the routine woth an action based on the variable's
value. I originally tried myCell(1 to X) but I get a variable not defined
error.

Thanks in advance for the help!

Microsmith

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,365
Default Store values in variables based on count

Use a dynamic array.

Dim myCell() as Variant

then after you've set your x variable based on the count, add this line
Redim myCell(1 to x)

"Microsmith" wrote:

I'm trying to write a macro that declares variables in a dim statement where
the number of variables are based on a count (number of cells in a range for
example). To illustrate I present the following macro :

Sub SetVariableNames()

Dim myVarRange As Range
Dim myCell(1 To 6) As Variant
Dim x As Integer
Dim i As Integer

'Assigns values to myCell based on contents of myRange

i = 1
x = ActiveSheet.Range("myRange").Count
Set myVarRange = Worksheets(1).Range("myRange")

For i = 1 To x
myCell(i) = myVarRange.Cells(i).Value
Next i

End Sub

The above macro work fine becasue I specified in the dim statement that I
had 6 values to assign to myCell(1 to 6). However, I would like the macro
to set the upper range based on the count of cells in the selected range so
that values could be stored to a variable based on each value in the range
which could change. I can't figure out how to do that. The variables would
be used elsewhere in the routine woth an action based on the variable's
value. I originally tried myCell(1 to X) but I get a variable not defined
error.

Thanks in advance for the help!

Microsmith

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Store values in variables based on count

Thanks JLatham and JE McGimpsey. This is exactly what I needed!

"JE McGimpsey" wrote:

You could use ReDim:

Dim myCell As Variant
Dim x As Long

x = ActiveSheet.Range("myRange").Count
ReDim myCell(1 to x)

but you might also want to consider simply using myCell as a
two-dimensional array):

Dim myCell As Variant
Dim i As Long

myCell = Worksheets(1).Range("myRange").Value
For i = 1 To UBound(myCell, 1)
Debug.Print myCell(i, 1)
Next i





In article ,
Microsmith wrote:

I'm trying to write a macro that declares variables in a dim statement where
the number of variables are based on a count (number of cells in a range for
example). To illustrate I present the following macro :

Sub SetVariableNames()

Dim myVarRange As Range
Dim myCell(1 To 6) As Variant
Dim x As Integer
Dim i As Integer

'Assigns values to myCell based on contents of myRange

i = 1
x = ActiveSheet.Range("myRange").Count
Set myVarRange = Worksheets(1).Range("myRange")

For i = 1 To x
myCell(i) = myVarRange.Cells(i).Value
Next i

End Sub

The above macro work fine becasue I specified in the dim statement that I
had 6 values to assign to myCell(1 to 6). However, I would like the macro
to set the upper range based on the count of cells in the selected range so
that values could be stored to a variable based on each value in the range
which could change. I can't figure out how to do that. The variables would
be used elsewhere in the routine woth an action based on the variable's
value. I originally tried myCell(1 to X) but I get a variable not defined
error.

Thanks in advance for the help!

Microsmith


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Count w/ multiple variables & text values king60611 Excel Worksheet Functions 5 June 10th 09 09:55 PM
count unique values if 2 variables Tony7659 Excel Worksheet Functions 3 May 18th 09 06:45 PM
Count unique values and create list based on these values vipa2000 Excel Worksheet Functions 7 August 5th 05 01:17 AM
Count rows based on multiple variables Murph Excel Programming 3 October 27th 04 11:35 AM
Store variables between XLA sessions Timo Autiokari Excel Programming 2 January 16th 04 12:02 PM


All times are GMT +1. The time now is 05:30 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"