arrayname as variable

  • Thread starter Thread starter devo
  • Start date Start date
D

devo

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
 
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
 
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
 
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.calladine/excel/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
 
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.calladine/excel/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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top