MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 21st, 2002, 08:36 AM   #1
RET79
Board Regular
 
Join Date: Mar 2002
Location: England, UK.
Posts: 526
Default

What I would like is instead of having to do this sort of thing:

B = Array(2,5,7,8,10,12)

because the array itself could be quite large and so very tedious to write it all in.
However, I have the numbers 2,5,7,8,10,12 in Range("A1:A6") of my excel worksheet. Ideally I want something like this (but this doesn't work but you should see what I am getting at):

B= Array(Range("A1:A6"))
= Array(2,5,7,8,10,12)

Since in my code I enjoy using the B(0)=2,
B(3)=8 properties you can have with arrays, but I don't want to type perhaps hundreds of numbers into the array code when there could/should/must be a way of using the values you have in the range.

Thanks.
RET79 is offline   Reply With Quote
Old Mar 21st, 2002, 08:40 AM   #2
RET79
Board Regular
 
Join Date: Mar 2002
Location: England, UK.
Posts: 526
Default

I forgot to add, even better if you can take a dynamic range and make it into an array. This would cane !
RET79 is offline   Reply With Quote
Old Mar 21st, 2002, 12:46 PM   #3
RET79
Board Regular
 
Join Date: Mar 2002
Location: England, UK.
Posts: 526
Default

Was this message removed?

Anyway, does anyone know if what I am asking for is possible? It would cane so much if it was.
RET79 is offline   Reply With Quote
Old Mar 21st, 2002, 01:31 PM   #4
Tommy Bak
Board Regular
 
Join Date: Feb 2002
Location: Guderup, Denmark
Posts: 287
Default

Hi
to put a range into an array first declare the array as variant:
mat(x,1) is first column. mat(x,2) second column.

Sub test()
Dim mat As Variant
mat = Range("A1:B6")
For x = 1 To 6
Debug.Print mat(x, 1), mat(x,2)
Next

'the other way around
Range("c1:d6") = mat
End Sub



regards Tommy
Tommy Bak is offline   Reply With Quote
Old Mar 21st, 2002, 02:09 PM   #5
RET79
Board Regular
 
Join Date: Mar 2002
Location: England, UK.
Posts: 526
Default

Tommy,

I want to do this since I want to use the array function property that if

B = Array (2,5,6,8,10)

then B(0) = 2, B(1) = 5 etc.

I have the integers

2
5
6
8
10
like that in column A, [a1:a5] and I am saying that rather than type 2,5,6,8,10 in manually in the code, I rather pick up the values from column A.

So, if u say mat is an array, then i shoudl be able to use things like

mat(0) = 2, mat(1) = 5 etc.

Hope that helps, I think you may be on the right track though, jsut needs to be finished off I think.
RET79 is offline   Reply With Quote
Old Mar 21st, 2002, 02:19 PM   #6
Tommy Bak
Board Regular
 
Join Date: Feb 2002
Location: Guderup, Denmark
Posts: 287
Default

Ret79
You are correct. Mat is an array so you are able to do what you suggested, but it is an array(x,y) where x is for rows and y is for column, so if we use just one column as your example you would fill B(x,1). That is instead of refering to it as B(2) you shall refer to it as B(2,1). If you had 10 columns it would be B(2,10) if you refer to the last column, second read row.

Sub test()
Dim B As Variant
mat = Range("A1:A6")
For x = 1 To 6
Debug.Print B(x, 1)
Next

'the other way around
Range("c1:c6") = B
End Sub




Tommy Bak is offline   Reply With Quote
Old Mar 21st, 2002, 03:22 PM   #7
RET79
Board Regular
 
Join Date: Mar 2002
Location: England, UK.
Posts: 526
Default

Sub test()
Dim a As Integer
Dim B As Variant
B = Range("A1:A6")
For x = 1 To 6
Debug.Print B(x, 1)
Next

'the other way around
Range("c1:c6") = B

a = B(4, 1)
Range("A22") = a *2
End Sub


It's just what I needed !!!

Thanks so much, I am very grateful indeed!

[ This Message was edited by: RET79 on 2002-03-21 14:24 ]
RET79 is offline   Reply With Quote
Old Mar 21st, 2002, 03:36 PM   #8
davers5
Board Regular
 
Join Date: Feb 2002
Posts: 255
Default

Many times when you read values into an array you are reading them from a text file and has an end of file character so you know when the values have stopped. If you have the values in a list in excel you can assume that if you hit a blank space you have come to the last value (or two blank spaces). Here is some code:

Dim aintNum(0 to 20) as integer
Dim n as integer

cells(1,1). select **Selects cell a1
n=0
Do while activecell.value<>""
aintNum(n)=activecell.value
activecell.offset(1,0).select
n=n+1
Loop

This will loop through all the values and read them into an array. Not that this is NOT a dynamic array, it only has 21 values (0 to 20), you can increase the values by increasing the number from 20 to whatever in the declaration statement. If you want to redimension your array it is slightly more difficult.

To print this values try this:

dim p as integer
For p=0 to n
cells(p,2).value=aintNum(p)
Next p

This will print all your values in b (2nd) column.

I hope this helps you out.

Dave
davers5 is offline   Reply With Quote
Old Mar 21st, 2002, 03:44 PM   #9
davers5
Board Regular
 
Join Date: Feb 2002
Posts: 255
Default

Sorry, that row in the second loop:

cells(p,1).value needs to be cells(p+1,1).value since the first value of p is 0 and there is no 0 row it would come up with an error unless you use p+1.

Dave
davers5 is offline   Reply With Quote
Old Mar 21st, 2002, 04:46 PM   #10
RET79
Board Regular
 
Join Date: Mar 2002
Location: England, UK.
Posts: 526
Default

Dave, thanks so much I think that's cool.

Now, I think the next thing that I am gonna try and do is get rid of that constant at the beginning and make it into a dynamic array. I have tried to do this using count, value and stuff but am having trouble with having to declare the thing as a constant.

Any suggestions anyone?
Cheers.
RET79 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 10:25 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes