ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   arrayname as variable (https://www.excelbanter.com/excel-programming/366771-arrayname-variable.html)

devo

arrayname as variable
 
Hi

Could someone please explain how to do the following

I wish to have a function which i can pass a string which is an array name
and then be allowed to manipulate the array

ie

array_width or array_drop

function dosomethingwitharray (arrayname as object)
test1 = arrayname.ubound
test2 = arrayname.lbound
' do something with array
End function

but i seem to be getting erros with setting and passing the arrayname



Jim Thomlinson

arrayname as variable
 
An array is a variable not an object so it sould be passed more like this...
I assume you know what type of varaibles the array holds. If not then specify
variant

public function dosomethingwitharray (arrayname() as string) as variant
test1 = ubound(arrayname)
test2 = lbound(arrayname)
' do something with array
End function

--
HTH...

Jim Thomlinson


"devo" wrote:

Hi

Could someone please explain how to do the following

I wish to have a function which i can pass a string which is an array name
and then be allowed to manipulate the array

ie

array_width or array_drop

function dosomethingwitharray (arrayname as object)
test1 = arrayname.ubound
test2 = arrayname.lbound
' do something with array
End function

but i seem to be getting erros with setting and passing the arrayname




Dan Thompson

arrayname as variable
 
Im not sure if I am getting this right but I think you have already assigned
an
Array() to a variable name if I got this right and now you want to pass it
to a function but your getting errors when the function try's to return a
value.

This is because when you pass an array to a function you almost always need
to specify if it is beeing passed "ByRef" or "ByVal" although I think you can
pass arrays to functions without the "ByVal" or "ByRef" if you are not using
Option Explicit not sure though cause I allways use them.
Anyhow try this...

function dosomethingwitharray (ByRef arrayname() as string) as variant
test1 = ubound(arrayname)
test2 = lbound(arrayname)
' do something with array
End function



"Jim Thomlinson" wrote:

An array is a variable not an object so it sould be passed more like this...
I assume you know what type of varaibles the array holds. If not then specify
variant

public function dosomethingwitharray (arrayname() as string) as variant
test1 = ubound(arrayname)
test2 = lbound(arrayname)
' do something with array
End function

--
HTH...

Jim Thomlinson


"devo" wrote:

Hi

Could someone please explain how to do the following

I wish to have a function which i can pass a string which is an array name
and then be allowed to manipulate the array

ie

array_width or array_drop

function dosomethingwitharray (arrayname as object)
test1 = arrayname.ubound
test2 = arrayname.lbound
' do something with array
End function

but i seem to be getting erros with setting and passing the arrayname




devo

arrayname as variable
 
Hi Jim

Thanks for your reply but still having difficulties in getting it to work..
I have included a little test form and the code i getting the problem with
....
maybe you could shed some light

you can download the code in frm and frx and also a workbook from
http://homepage.ntlworld.com/nick.ca...xcel/array.zip

Option Explicit
Dim WidthArray(), DropArray() As Variant
Dim arrayname, arraysize As Variant
Dim arraydata As String

Sub UserForm_Initialize()

opt_Width.value = True

End Sub
Private Sub butAdd_Click()

arraydata = txtInputValue.value
Call AddToArray(arrayname, arraydata)

End Sub

Private Sub opt_Drop_Click()

arrayname = "DropArray()"

End Sub

Private Sub opt_Width_Click()

arrayname = "WidthArray()"

End Sub

Private Function AddToArray(ByRef arrayname As Variant, arraydata) As
Variant

'get the current size of the array + 1
arraysize = UBound(arrayname) - LBound(arrayname) + 1

're dim the array to preserve the data and increase the storage
ReDim Preserve arrayname(arraysize, 2)

arrayname(arraysize, 0) = arraysize
arrayname(arraysize, 1) = arraydata

End Function

See if you can see what i am doing wrong.... !!!

Thanks


"Jim Thomlinson" wrote in message
...
An array is a variable not an object so it sould be passed more like
this...
I assume you know what type of varaibles the array holds. If not then
specify
variant

public function dosomethingwitharray (arrayname() as string) as variant
test1 = ubound(arrayname)
test2 = lbound(arrayname)
' do something with array
End function

--
HTH...

Jim Thomlinson


"devo" wrote:

Hi

Could someone please explain how to do the following

I wish to have a function which i can pass a string which is an array
name
and then be allowed to manipulate the array

ie

array_width or array_drop

function dosomethingwitharray (arrayname as object)
test1 = arrayname.ubound
test2 = arrayname.lbound
' do something with array
End function

but i seem to be getting erros with setting and passing the arrayname






devo

arrayname as variable
 
Hi Dan

Thanks for your reply but still having difficulties in getting it to work..
I have included a little test form and the code i getting the problem with
....
maybe you could shed some light

I throw this one to you as well ...

you can download the code in frm and frx and also a workbook from
http://homepage.ntlworld.com/nick.ca...xcel/array.zip

Option Explicit
Dim WidthArray(), DropArray() As Variant
Dim arrayname, arraysize As Variant
Dim arraydata As String

Sub UserForm_Initialize()

opt_Width.value = True

End Sub
Private Sub butAdd_Click()

arraydata = txtInputValue.value
Call AddToArray(arrayname, arraydata)

End Sub

Private Sub opt_Drop_Click()

arrayname = "DropArray()"

End Sub

Private Sub opt_Width_Click()

arrayname = "WidthArray()"

End Sub

Private Function AddToArray(ByRef arrayname As Variant, arraydata) As
Variant

'get the current size of the array + 1
arraysize = UBound(arrayname) - LBound(arrayname) + 1

're dim the array to preserve the data and increase the storage
ReDim Preserve arrayname(arraysize, 2)

arrayname(arraysize, 0) = arraysize
arrayname(arraysize, 1) = arraydata

End Function

See if you can see what i am doing wrong.... !!!

Thanks
"Dan Thompson" wrote in message
...
Im not sure if I am getting this right but I think you have already
assigned
an
Array() to a variable name if I got this right and now you want to pass it
to a function but your getting errors when the function try's to return a
value.

This is because when you pass an array to a function you almost always
need
to specify if it is beeing passed "ByRef" or "ByVal" although I think you
can
pass arrays to functions without the "ByVal" or "ByRef" if you are not
using
Option Explicit not sure though cause I allways use them.
Anyhow try this...

function dosomethingwitharray (ByRef arrayname() as string) as variant
test1 = ubound(arrayname)
test2 = lbound(arrayname)
' do something with array
End function



"Jim Thomlinson" wrote:

An array is a variable not an object so it sould be passed more like
this...
I assume you know what type of varaibles the array holds. If not then
specify
variant

public function dosomethingwitharray (arrayname() as string) as variant
test1 = ubound(arrayname)
test2 = lbound(arrayname)
' do something with array
End function

--
HTH...

Jim Thomlinson


"devo" wrote:

Hi

Could someone please explain how to do the following

I wish to have a function which i can pass a string which is an array
name
and then be allowed to manipulate the array

ie

array_width or array_drop

function dosomethingwitharray (arrayname as object)
test1 = arrayname.ubound
test2 = arrayname.lbound
' do something with array
End function

but i seem to be getting erros with setting and passing the arrayname







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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com