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 Feb 22nd, 2002, 02:39 PM   #1
DominikWells
New Member
 
Join Date: Feb 2002
Posts: 7
Default

If I have declared an array

Random_Data(1 To 15) As Integer

How can I pass these values between modules? Can I call it in a subroutine?

Thanks.
DominikWells is offline   Reply With Quote
Old Feb 22nd, 2002, 02:53 PM   #2
DRJ
MrExcel MVP
 
DRJ's Avatar
 
Join Date: Feb 2002
Location: California
Posts: 3,857
Default

Hi

You can call the sub with the variables or you can globaly declare them.

Option 1

Sub MyCode()

Dim MyArray(14) as Integer

MyArray = something you set it to equal

More Code

Call MyCode2(MyArray)

End Sub

Sub MyCode2(NewArray(14) as Integer)

More Code

End Sub

Or

Option 2

Dim MyArray(14) As Integer

Sub MyCode

More Code (MyArray is declared and will retain its values)

Call MyCode2

End Sub

Sub MyCode2()


More Code (MyArray is declared and will retain its values)

End Sub


HTH

DRJ
DRJ is offline   Reply With Quote
Old Feb 22nd, 2002, 03:05 PM   #3
DominikWells
New Member
 
Join Date: Feb 2002
Posts: 7
Default

It did help, thank you! I couldn't get the former portion to work, but the latter worked just fine. Thanks again.

DEW
DominikWells is offline   Reply With Quote
Old Feb 22nd, 2002, 03:43 PM   #4
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

Quote:

Sub MyCode2(NewArray(14) as Integer)
Off of the top of my head, this would have failed because you need to pass arrays ByRef and not ByVal. You also couldn't specify the size and I believe maybe not even the typ of the array (my help file is knackered right now or else I'd look it up). I believe this code would work with something like:

Sub MyCode2(ByRef NewArray() as Variant)


It tends to be a bad idea to have any global variables in your program as this makes the program more susceptible to errors. Usually there is a way to pass the argument rather than to rely on Global variables.


_________________
[b] Mark O'Brien

[ This Message was edited by: Mark O'Brien on 2002-02-22 14:44 ]
Mark O'Brien is offline   Reply With Quote
Old Feb 22nd, 2002, 04:11 PM   #5
Russell Hauf
MrExcel MVP
 
Russell Hauf's Avatar
 
Join Date: Feb 2002
Location: Portland, OR USA
Posts: 1,374
Default

Mark is correct. And you can declare the type of the array in the argument list. In fact, it's a very good idea (unless you have an array with mixed types) - this way if you try to pass the wrong array to your function that takes an array, it won't work (and won't mess up your array or give an unexpected error). Here is a simple example:

Code:
Sub TestArray1()
    Dim arr(1 To 2) As Integer
    arr(1) = 4
    arr(2) = 8
    Call TestArray2(arr)
    Debug.Print arr(1) & "  " & arr(2)
End Sub

Sub TestArray2(arr() As Integer)
    Dim intI As Integer
    For intI = 1 To UBound(arr)
        arr(intI) = arr(intI) * 2
    Next intI
End Sub
You don't need to have the same name in both procedures/functions - I just used a short name.

Hope this gives you a better understanding,

Russell
Russell Hauf is offline   Reply With Quote
Old Jul 31st, 2002, 07:13 AM   #6
tealeaf
Board Regular
 
Join Date: May 2002
Location: mtl, canada
Posts: 160
Default

Quote:
On 2002-02-22 15:11, Russell Hauf wrote:
Mark is correct. And you can declare the type of the array in the argument list. In fact, it's a very good idea (unless you have an array with mixed types) - this way if you try to pass the wrong array to your function that takes an array, it won't work (and won't mess up your array or give an unexpected error). Here is a simple example:

Code:
Sub TestArray1()
    Dim arr(1 To 2) As Integer
    arr(1) = 4
    arr(2) = 8
    Call TestArray2(arr)
    Debug.Print arr(1) & "  " & arr(2)
End Sub

Sub TestArray2(arr() As Integer)
    Dim intI As Integer
    For intI = 1 To UBound(arr)
        arr(intI) = arr(intI) * 2
    Next intI
End Sub
You don't need to have the same name in both procedures/functions - I just used a short name.

Hope this gives you a better understanding,

Russell
hi i'm trying to do something similar EXCEPT i don't declare the type of the array at the beginning. so i'm getting an error when i try passing the array. here's some code to further explain (and there is a reason i use dim then redim)
Code:
Sub TestArray1()
    Dim arr
    ReDim arr(1 To 2, 1 To 2)
    arr(1, 1) = 4
    arr(1, 2) = 5
    arr(2, 1) = 8
    arr(2, 2) = 6
    Call TestArray2(arr)
    Debug.Print arr(1, 1) & "  " & arr(1, 2) & "  " & arr(2, 1) & "  " & arr(2, 2)
End Sub

Sub TestArray2(arr() As Integer)
    Dim intI As Integer
    Dim x As Integer
    
    For intI = 1 To UBound(arr)
        For x = 1 To 2
        arr(intI, x) = arr(intI, x) * 2
        Next x
    Next intI
End Sub
thanks
tealeaf is offline   Reply With Quote
Old Jul 31st, 2002, 08:08 AM   #7
tealeaf
Board Regular
 
Join Date: May 2002
Location: mtl, canada
Posts: 160
Default

for those interested my mistake was quite dumb:
it should read
Dim arr() integer <-- notice the ()

Code:
Sub TestArray1()
    Dim arr()
    ReDim arr(1 To 2, 1 To 2)
    arr(1, 1) = 4
    arr(1, 2) = 5
    arr(2, 1) = 8
    arr(2, 2) = 6
    Call TestArray2(arr)
    Debug.Print arr(1, 1) & "  " & arr(1, 2) & "  " & arr(2, 1) & "  " & arr(2, 2)
End Sub

Sub TestArray2(arr() As Integer)
    Dim intI As Integer
    Dim x As Integer
    
    For intI = 1 To UBound(arr)
        For x = 1 To 2
        arr(intI, x) = arr(intI, x) * 2
        Next x
    Next intI
End Sub
tealeaf is offline   Reply With Quote
Old Jul 31st, 2002, 08:24 AM   #8
Juan Pablo González
MrExcel MVP
 
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
Default

Just one more thing to add

ByRef is the default, so, if its ommitted, VBA will understand that the variable being passed is ByRef. If you're using ByVal, you need to write it that way.
__________________
Regards,

Juan Pablo González
http://www.juanpg.com
Juan Pablo González 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 07:07 PM.


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