SatheeshS
SatheeshS

Reputation: 15

How to get the String value from the function as my code gets the compile error 'Wrong number of arguments or invalid property assignment'

Below is my code to call the Function RandomBoysName to get the random name from the array of names in the function

Sub test()

Range("A1").Value = RandomBoysName

End Sub

Below is the function which returns the random boys name from the array.


Public Function RandomBoysName() As String

RandomBoysName = Array("Jacob", "Michael", "Joshua", "Matthew", "Christopher")

randArrIndex = Int((UCase(RandomBoysName) + 1) * Rnd)

RandomBoysName = "TEST"

RandomBoysName = RandomBoysName & RandomBoysName(randArrIndex)

End Function

Getting the error Wrong number of arguments or invalid property assignment in the below line

randArrIndex = Int((UCase(RandomBoysName) + 1) * Rnd)

Upvotes: 0

Views: 61

Answers (2)

VSA
VSA

Reputation: 26

I think u are using the variables wrong

this code will help u

Public Function RandomBoysName() As String

BoysName = Array("Jacob", "Michael", "Joshua", "Matthew", "Christopher")
' the number 4 is the number of items in your array including the 0
randArrIndex = Int((4 * Rnd) + 1)

RandomBoysName = "TEST" & BoysName(randArrIndex)

End Function

Upvotes: 0

Gary's Student
Gary's Student

Reputation: 96753

Consider:

Public Function RandomBoysName() As String
    Application.Volatile
    Randomize
    arr = Array("Jacob", "Michael", "Joshua", "Matthew", "Christopher")
    randArrIndex = Int((UBound(arr) + 1) * Rnd)
    RandomBoysName = "TEST"
    RandomBoysName = RandomBoysName & arr(randArrIndex)
End Function

Upvotes: 2

Related Questions