Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default 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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default 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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 125
Default 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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default 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





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default 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





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
Runtime Error '91' Object variable or With block variable not set Alec Coliver Excel Discussion (Misc queries) 2 October 24th 09 02:29 PM
Sum cells based on a row variable and seperate column variable CheeseHeadTransplant Excel Worksheet Functions 10 September 23rd 05 06:59 PM
why is it saying sheetcnt is "variable not defined" how to do a global variable to share over multiple functions in vba for excel? Daniel Excel Worksheet Functions 1 July 9th 05 03:05 AM
Run-time error '91': "Object variable or With block variable not set Mike[_92_] Excel Programming 2 December 30th 04 10:59 AM
Cells.Find error Object variable or With block variable not set Peter[_21_] Excel Programming 2 May 8th 04 02:15 PM


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

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

About Us

"It's about Microsoft Excel"