+ Reply to Thread
Results 1 to 18 of 18

Argument not optional error while passing value of variable between subs

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Argument not optional error while passing value of variable between subs

    Greetings,

    I would like to pass value of a variable (iRowdata) from Sub Test1 to Sub Test2. I followed the recommendation from this thread, unfortunately I get a "Argument not optional" error.

    Here's a snippet of the main code (all in the same standard module).

    Any ideas as to what is wrong? Many thanks in advance
    Asha

    Option Explicit
    Dim rCoDE As Range
    Dim rCoDB As Range
    Dim WF As WorksheetFunction
    
    Sub Test1()
    Dim rCell As Range
    Dim iCol As Integer             
    Dim iRowdata As Integer        
    Dim r As String                 
    
    'Define variable(s)
    Set rCoDE = Names("DE_Co").RefersToRange
    Set WF = Application.WorksheetFunction
    iRowdata = WF.CountIf(rCoDE, "<>-")
    iCol = 2
    
    Call Test2(iRowdata)
    
    ‘REST OF THE CODE
    
    Set rCoDE = Nothing
    Set rCoDB = Nothing
    Set WF = Nothing
    
    End Sub
    
    Sub Test2(iRowdata As Integer)
    
    Dim wDest As Worksheet              
    Dim iCNDup As Integer               
    Dim rCopy As Range                 
    Dim rErrCells As Range              
    
    Application.ScreenUpdating = False
    
    'Define variable(s)
    Set WF = Application.WorksheetFunction
    Set rCopy = Names("DE_InputRange").RefersToRange.Resize(WF.Max(iRowdata, 1))
    
    ‘REST OF THE CODE        
    
    Set wDest = Nothing
    Set rCopy = Nothing
    Set WF = Nothing
    Exit Sub
            
    End Sub
    Last edited by asha3010; 09-30-2010 at 05:21 AM.

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Argument not optional error while passing value of variable between subs

    I assume you've checked that iRowData is actually holding a value before it calls Test2?

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Argument not optional error while passing value of variable between subs

    You'd better start simple:

    Sub Test1()
       A_Test2 application.CountIf(range("DE_Co"), "<>-")
    End Sub
    
    Sub Test2(iRowdata As Integer)
       c01= Names("DE_InputRange").RefersToRange.Resize(iRowdata).address
    End Sub
    next step
    Sub Test1()
       x=application.CountIf(range("DE_Co"), "<>-")
       A_Test2 x
    End Sub
    
    Sub Test2(iRowdata As Integer)
       c01= Names("DE_InputRange").RefersToRange.Resize(iRowdata).address
    End Sub



  4. #4
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: Argument not optional error while passing value of variable between subs

    Hi Domski,

    Thanks for your reply. Yes, I did check that - it holds a current value of 1, which is correct.

    Asha

  5. #5
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Argument not optional error while passing value of variable between subs

    I see little reason why it wouldn't work. Have you tried snb's suggested code?

    Dom

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,996

    Re: Argument not optional error while passing value of variable between subs

    There's nothing wrong with the code syntactically. Where do you actually get the error? (on one of the lines you didn't post?)
    Everyone who confuses correlation and causation ends up dead.

  7. #7
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: Argument not optional error while passing value of variable between subs

    Thanks to all for your response.

    @ snb / Domski: As much as I hate to demonstrate my ignorance, I don't know what to do with snb's code.

    @ romperstomper: The code works as expected before adding the lines for passing the variable.

    Based on your response, I checked the optionbutton (to which macro Test2) is assigned. Macro Test2 was assigned before renaming it to Test2(iRowdata As Integer). So, I am assuming that is why I was getting the subject error.

    I changed the code for the optionbutton to

    Sub OptionButton20_Click()
       Call Test2(iRowdata)
    End Sub
    Now I get a Byref argument type mismatch error! I get the same error when I run Test1.

    Can someone please help?

    Thanks for your support
    Asha


  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,996

    Re: Argument not optional error while passing value of variable between subs

    It would have helped a lot if you had explained that macro2 was being called directly from a control rather than showing us it being called from another sub!
    You cannot pass iRowData like that because it is only declared inside Macro1. How is Test2 supposed to work when called directly from a control??

  9. #9
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: Argument not optional error while passing value of variable between subs

    Am sorry romperstomper for exasperating you or anyone else for not providing that information. I did not know it was vital.

    Thanks
    Asha

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,996

    Re: Argument not optional error while passing value of variable between subs

    OK, but that doesn't answer my question?

  11. #11
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Argument not optional error while passing value of variable between subs

    <cough>global variables<cough>

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,996

    Re: Argument not optional error while passing value of variable between subs

    Wash your mouth out.

  13. #13
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: Argument not optional error while passing value of variable between subs

    How is Test2 supposed to work when called directly from a control??
    This? I don't have a answer (or atleast a good one) to that! You can have a laugh - I thought if I could pass iRowdata from Test1 to Test2, it did not matter if I called Test2 from a control!

    Asha

  14. #14
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: Argument not optional error while passing value of variable between subs

    @Andrew-R: Thanks for your response. I will read up on the use of global variables.
    Asha

  15. #15
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,996

    Re: Argument not optional error while passing value of variable between subs

    But that's the issue. Test1 passes the value Test2 needs every time it is called. If you need to be able to call Test2 on its own, you may as well simply have it calculate the value of iRowData itself.

  16. #16
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: Argument not optional error while passing value of variable between subs

    Thanks for the response.

    Test1 calculates the value of iRowdata amongst doing other things. When Test2 is called, is it possible to get the value of iRowdata from Test1 without it running through the rest of the routine in Test1 or am I asking for too much?

    Asha

  17. #17
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,996

    Re: Argument not optional error while passing value of variable between subs

    No. Either move the three lines of code from Test1 (unless it uses the value elsewhere) into Test2, or move them to a separate function that both routines can call as needed:
    Function RowCount() as Long
    Dim rCode as Range
    Dim WF as WorksheetFunction
    Set rCoDE = Names("DE_Co").RefersToRange
    Set WF = Application.WorksheetFunction
    RowCount = WF.CountIf(rCoDE, "<>-")
    End Function

  18. #18
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: Argument not optional error while passing value of variable between subs


    As recommended I will move it to a separate function.

    Thank you so much romperstomper for your patience and support.
    Asha

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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