+ Reply to Thread
Results 1 to 4 of 4

Error for VBA function - "Argument Not Optional?"

Hybrid View

  1. #1
    Registered User
    Join Date
    04-03-2014
    Location
    Woodbury, GA
    MS-Off Ver
    Excel 2010 and Excel 2013
    Posts
    37

    Question Error for VBA function - "Argument Not Optional?"

    I'm getting an error when running my script. The error points to the function below and says "Argument is not optional." I've done similar things before, so I know this should be able to work, but I've played with this code for a couple days now and cannot figure out where the issue is located. I feel like I'm missing something very simple.

    The code:
    Function findCoordinatesForX(lineID As Long, dataSheetName As String) As Variant
    Dim rowCount As Long
    Dim i As Integer
    Dim returnValue As Variant
    rowCount = Worksheets(dataSheetName).Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To rowCount Step 1
        If Range("A" & i).Value = lineID Then
            returnValue = Range("I" & i).Value
            findCoordinatesForY = returnValue
        End If
    Next i
    End Function
    The Call:
    dValue = busbarArray(busbarIterator)
    xstart = findCoordinatesForX(dValue, dataSheetName)
    ystart = findCoordinatesForY(dValue, dataSheetName)
    I've never had to worry about it with these smaller macros, but perhaps I should specify to pass by reference or value?

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Error for VBA function - "Argument Not Optional?"

    Your Function is looking for a lineid as a LONG number

    You are defining LineID = Dvalue = busbarArray(busbarIterator) So what is that?
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Error for VBA function - "Argument Not Optional?"

    How is datasheetname being defined?
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Error for VBA function - "Argument Not Optional?"

    All your function does is return the lineID if it's found; I'd have thought it would return the line number. Either way, the code is looking on the active worksheet, not in dataSheetName.

    I would code it like this to return the row number:

    Sub EG()
      Dim lineID As Long
      
      lineID = 42
      
      If findCoordinatesForX(lineID, Worksheets("Bob")) <> 0 Then
        ' whatever
      Else
        ' whatever else
      End If
    End Sub
    
    Function findCoordinatesForX(lineID As Long, wks As Worksheet) As Long
      Dim cell As Range
      
      Set cell = wks.Range("A:A").Find(What:=lineID, LookIn:=xlValues, LookAt:=xlWhole)
      If Not cell Is Nothing Then findCoordinatesForX = cell.Row
    End Function
    Or this to just return True if the value is found:

    Sub EG()
      Dim lineID As Long
      
      lineID = 42
      
      If findCoordinatesForX(lineID, Worksheets("Bob")) Then
        ' whatever
      Else
        ' whatever else
      End If
    End Sub
    
    Function findCoordinatesForX(lineID As Long, wks As Worksheet) As Boolean
      Dim cell As Range
      
      Set cell = wks.Range("A:A").Find(What:=lineID, LookIn:=xlValues, LookAt:=xlWhole)
      findCoordinatesForX = Not cell Is Nothing
    End Function
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. help with "invalid procedure call or argument" error.
    By taylorsm in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-02-2017, 11:41 AM
  2. [SOLVED] "ByRef argument type mismatch error" trying to use my first UDF
    By capson in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-23-2014, 08:46 PM
  3. Run-time Error 5 "Invalid Procedure Call or Argument"
    By dinakar.yadav in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-15-2014, 09:22 AM
  4. Using a Boolean function within an If statement: Argument not optional error message
    By elfsprin in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-16-2011, 04:12 PM
  5. "Invalid procedure call or argument" error when creating Pivot table
    By Kaigi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2009, 06:02 AM
  6. Error: "Argument is not an option" on Macro
    By Luminary Xion in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2009, 08:53 PM
  7. "ByRef argument type mismatch" Error
    By Baapi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-16-2005, 08:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1