Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Assigning values from a selected range to individual variables

A user selects a range (eg: Range("A1:C1")) and runs a macro. The macro should then assign the value from each cell to its own variable. So cell A1 = 2, B1 = 4 and C1 = 6 and the variables should end up with NumA = 2, NumB = 4 and NumC = 6.

The problem is that I want to be able to handle any size of selected range the user would make and then assign the given number of values to an individual variabel.

Can anybody help out?

Thank you
Schwartz
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Assigning values from a selected range to individual variables

user selects a range (eg: Range("A1:C1")) and runs a macro. The macr
should then assign the value from each cell to its own variable. S
cell A1 = 2, B1 = 4 and C1 = 6 and the variables should end up wit
NumA = 2, NumB = 4 and NumC = 6.

The problem is that I want to be able to handle any size of selecte
range the user would make and then assign the given number of values t
an individual variabel.

You should try to use an array, which you expand in a dinamic way :

Dim intValues() as Integer
Dim r as Range
Dim intStartRow as Integer, intEndRow as Integer, intColumn as Integer
Dim i as Integer, j as Integer

set r = Selection
if r.Columns.Count1 then
msgbox ("The Selection must be made in one column !!")
exit sub
end if

ReDim intValues(r.Rows.Count)

intColumn = r.Colums(1).Column
intStartRow = r.Rows(1).Row
intEndRow = r.Rows(r.Rows.Count).Row
j=1
For i = intStartRow To intEndRow

intValues(j) = cells(i,intColumn)
j=j+1
Nex

--
Message posted from http://www.ExcelForum.com

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Assigning values from a selected range to individual variables

sorry, I forgot...

to have a more flexible solution :

If you want to be able to handle multicolumn selection :
1. remove the if block checking the number of columns
2. make a multidimensional ReDim. The second dimension should set th
number of columns in the selection.
3. change the loop, so you can peek the selected range through column

--
Message posted from http://www.ExcelForum.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Assigning values from a selected range to individual variables

Schwartz,

What is the criteria for assigning values. In your example, you use the even
numbers.

You could try something like

i=1
For each cell In Selection
cell,Value = i*2
i = i + 1
Next cell
--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Dr. Schwartz" wrote in message
...
A user selects a range (eg: Range("A1:C1")) and runs a macro. The macro

should then assign the value from each cell to its own variable. So cell A1
= 2, B1 = 4 and C1 = 6 and the variables should end up with NumA = 2, NumB =
4 and NumC = 6.

The problem is that I want to be able to handle any size of selected range

the user would make and then assign the given number of values to an
individual variabel.

Can anybody help out?

Thank you
Schwartz



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default Assigning values from a selected range to individual variables

You don't need to make it so complex. You can load a
variant directly from a range

eg
1)
DIM MyArray as Variant
MyArray = Range("G1:Y34")

2)
DIM MyArray as Variant
MyArray = Selection

You can easily find the bounds using the UBOUND ad LBOUND
functions.

Patrick Molloy
Microsoft Excel MVP

-----Original Message-----
sorry, I forgot...

to have a more flexible solution :

If you want to be able to handle multicolumn selection :
1. remove the if block checking the number of columns
2. make a multidimensional ReDim. The second dimension

should set the
number of columns in the selection.
3. change the loop, so you can peek the selected range

through columns


---
Message posted from http://www.ExcelForum.com/

.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Assigning values from a selected range to individual variables

If you use this method, don't forget to give correct dimensions while
you're reading your array, since the variant array is multidimensional.
Like this :

myarray(rownumber,1)

if your range would have 2 columns, then it supposed to look like this,
if you want to read a value from the second column :

myarray(rownumber,2)


---
Message posted from http://www.ExcelForum.com/

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
Assigning cell values to variables Tim Excel Discussion (Misc queries) 3 June 11th 09 04:30 PM
Convert Data Range in to individual values mistryrg Excel Discussion (Misc queries) 2 June 7th 06 04:48 PM
Assigning ranges for values (e.g., $1,005 the range is ">$1K to 5K jennifer Excel Worksheet Functions 1 August 11th 05 02:09 PM
Automatically clear values from a range of selected cells John Davies Excel Discussion (Misc queries) 1 June 28th 05 04:42 PM
Assigning Selected Cells to Code JMay Excel Programming 4 July 19th 03 05:29 PM


All times are GMT +1. The time now is 10:03 PM.

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

About Us

"It's about Microsoft Excel"