ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Help with silly error (https://www.excelbanter.com/excel-programming/327977-help-silly-error.html)

[email protected]

Help with silly error
 
I have a really silly error which is killing me !

When I pass a filled array to a function, it loses all its content once
inside the function, as follows:

*** START CODE SNIPPET

Option Base 1

Sub MyRoutine

ReDim MyArray(3) As Variant

For x = 1 To 3
MyArray(x) = x
Next x

a = bAllFiguresUnique(MyArray()) '<== UBOUND HERE IS 3

End Sub

Function bAllFiguresUnique(ParamArray MyArray() As Variant) As Boolean

bAllFiguresUnique = True

For x = 1 To UBound(MyArray()) '<== UBOUND HERE IS 0
DoEvents
For y = 1 To UBound(MyArray())
DoEvents
If MyArray(x) = MyArray(y) Then
If x = y Then
'Ignore
Else
bAllFiguresUnique = False
Exit Function
End If
End If
Next y
Next x

End Function

*** END CODE SNIPPET

I must be doing something stupid, but I can't work out what !

Thanks in advance for all constructive suggestions.

Nick


Bob Phillips[_6_]

Help with silly error
 
This works for me

Sub MyRoutine()

ReDim MyArray(3) As Variant

For x = 1 To 3
MyArray(x) = x
Next x

a = bAllFiguresUnique(MyArray)

End Sub

Function bAllFiguresUnique(MyArray As Variant) As Boolean

bAllFiguresUnique = True

For x = 1 To UBound(MyArray)
DoEvents
For y = 1 To UBound(MyArray)
DoEvents
If MyArray(x) = MyArray(y) Then
If x = y Then
'Ignore
Else
bAllFiguresUnique = False
Exit Function
End If
End If
Next y
Next x

End Function

--

HTH

RP
(remove nothere from the email address if mailing direct)


wrote in message
ups.com...
I have a really silly error which is killing me !

When I pass a filled array to a function, it loses all its content once
inside the function, as follows:

*** START CODE SNIPPET

Option Base 1

Sub MyRoutine

ReDim MyArray(3) As Variant

For x = 1 To 3
MyArray(x) = x
Next x

a = bAllFiguresUnique(MyArray()) '<== UBOUND HERE IS 3

End Sub

Function bAllFiguresUnique(ParamArray MyArray() As Variant) As Boolean

bAllFiguresUnique = True

For x = 1 To UBound(MyArray()) '<== UBOUND HERE IS 0
DoEvents
For y = 1 To UBound(MyArray())
DoEvents
If MyArray(x) = MyArray(y) Then
If x = y Then
'Ignore
Else
bAllFiguresUnique = False
Exit Function
End If
End If
Next y
Next x

End Function

*** END CODE SNIPPET

I must be doing something stupid, but I can't work out what !

Thanks in advance for all constructive suggestions.

Nick




Tom Ogilvy

Help with silly error
 
Not sure why you want to use a parameter array, but if you do and this is a
simplified example, then this works:

Option Base 1

Sub MyRoutine()

ReDim MyArray(3) As Variant

For x = 1 To 3
MyArray(x) = x
Next x

a = bAllFiguresUnique(MyArray()) '<== UBOUND HERE IS 3
Debug.Print a
End Sub

Function bAllFiguresUnique(ParamArray MyArray() As Variant) As Boolean

bAllFiguresUnique = True
Debug.Print UBound(MyArray(0)) '<== UBOUND HERE IS 3
For x = 1 To UBound(MyArray(0))
DoEvents
For y = 1 To UBound(MyArray(0))
DoEvents
If MyArray(0)(x) = MyArray(0)(y) Then
If x = y Then
'Ignore
Else
bAllFiguresUnique = False
Exit Function
End If
End If
Next y
Next x

End Function



--
Regards,
Tom Ogilvy

wrote in message
ups.com...
I have a really silly error which is killing me !

When I pass a filled array to a function, it loses all its content once
inside the function, as follows:

*** START CODE SNIPPET

Option Base 1

Sub MyRoutine

ReDim MyArray(3) As Variant

For x = 1 To 3
MyArray(x) = x
Next x

a = bAllFiguresUnique(MyArray()) '<== UBOUND HERE IS 3

End Sub

Function bAllFiguresUnique(ParamArray MyArray() As Variant) As Boolean

bAllFiguresUnique = True

For x = 1 To UBound(MyArray()) '<== UBOUND HERE IS 0
DoEvents
For y = 1 To UBound(MyArray())
DoEvents
If MyArray(x) = MyArray(y) Then
If x = y Then
'Ignore
Else
bAllFiguresUnique = False
Exit Function
End If
End If
Next y
Next x

End Function

*** END CODE SNIPPET

I must be doing something stupid, but I can't work out what !

Thanks in advance for all constructive suggestions.

Nick




Bob Phillips[_6_]

Help with silly error
 
From his code, I assumed he didn't realy undedrstand how to use arrays, and
thus ParamArray was a mistake , after all it does say array<g

Bob


"Tom Ogilvy" wrote in message
...
Not sure why you want to use a parameter array, but if you do and this is

a
simplified example, then this works:

Option Base 1

Sub MyRoutine()

ReDim MyArray(3) As Variant

For x = 1 To 3
MyArray(x) = x
Next x

a = bAllFiguresUnique(MyArray()) '<== UBOUND HERE IS 3
Debug.Print a
End Sub

Function bAllFiguresUnique(ParamArray MyArray() As Variant) As Boolean

bAllFiguresUnique = True
Debug.Print UBound(MyArray(0)) '<== UBOUND HERE IS 3
For x = 1 To UBound(MyArray(0))
DoEvents
For y = 1 To UBound(MyArray(0))
DoEvents
If MyArray(0)(x) = MyArray(0)(y) Then
If x = y Then
'Ignore
Else
bAllFiguresUnique = False
Exit Function
End If
End If
Next y
Next x

End Function



--
Regards,
Tom Ogilvy

wrote in message
ups.com...
I have a really silly error which is killing me !

When I pass a filled array to a function, it loses all its content once
inside the function, as follows:

*** START CODE SNIPPET

Option Base 1

Sub MyRoutine

ReDim MyArray(3) As Variant

For x = 1 To 3
MyArray(x) = x
Next x

a = bAllFiguresUnique(MyArray()) '<== UBOUND HERE IS 3

End Sub

Function bAllFiguresUnique(ParamArray MyArray() As Variant) As Boolean

bAllFiguresUnique = True

For x = 1 To UBound(MyArray()) '<== UBOUND HERE IS 0
DoEvents
For y = 1 To UBound(MyArray())
DoEvents
If MyArray(x) = MyArray(y) Then
If x = y Then
'Ignore
Else
bAllFiguresUnique = False
Exit Function
End If
End If
Next y
Next x

End Function

*** END CODE SNIPPET

I must be doing something stupid, but I can't work out what !

Thanks in advance for all constructive suggestions.

Nick






Tom Ogilvy

Help with silly error
 
That would certainly be the more likely scenario and if that is the case,
then the OP probably won't understand my example anyway.

In any event, both contingencies have been covered. Perhaps someone will
think up a third.

--
Regards,
Tom Ogilvy

"Bob Phillips" wrote in message
...
From his code, I assumed he didn't realy undedrstand how to use arrays,

and
thus ParamArray was a mistake , after all it does say array<g

Bob


"Tom Ogilvy" wrote in message
...
Not sure why you want to use a parameter array, but if you do and this

is
a
simplified example, then this works:

Option Base 1

Sub MyRoutine()

ReDim MyArray(3) As Variant

For x = 1 To 3
MyArray(x) = x
Next x

a = bAllFiguresUnique(MyArray()) '<== UBOUND HERE IS 3
Debug.Print a
End Sub

Function bAllFiguresUnique(ParamArray MyArray() As Variant) As Boolean

bAllFiguresUnique = True
Debug.Print UBound(MyArray(0)) '<== UBOUND HERE IS 3
For x = 1 To UBound(MyArray(0))
DoEvents
For y = 1 To UBound(MyArray(0))
DoEvents
If MyArray(0)(x) = MyArray(0)(y) Then
If x = y Then
'Ignore
Else
bAllFiguresUnique = False
Exit Function
End If
End If
Next y
Next x

End Function



--
Regards,
Tom Ogilvy

wrote in message
ups.com...
I have a really silly error which is killing me !

When I pass a filled array to a function, it loses all its content

once
inside the function, as follows:

*** START CODE SNIPPET

Option Base 1

Sub MyRoutine

ReDim MyArray(3) As Variant

For x = 1 To 3
MyArray(x) = x
Next x

a = bAllFiguresUnique(MyArray()) '<== UBOUND HERE IS 3

End Sub

Function bAllFiguresUnique(ParamArray MyArray() As Variant) As Boolean

bAllFiguresUnique = True

For x = 1 To UBound(MyArray()) '<== UBOUND HERE IS 0
DoEvents
For y = 1 To UBound(MyArray())
DoEvents
If MyArray(x) = MyArray(y) Then
If x = y Then
'Ignore
Else
bAllFiguresUnique = False
Exit Function
End If
End If
Next y
Next x

End Function

*** END CODE SNIPPET

I must be doing something stupid, but I can't work out what !

Thanks in advance for all constructive suggestions.

Nick









All times are GMT +1. The time now is 11:14 PM.

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