Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello:

I wrote the following function:

VB
Public Function EEPosSheets(Index As Long, ByRef EmpSheets As Excel.Worksheet, ByRef PosSheets As Excel.Worksheet)

    'This function indexes all of the Employee and Position sheets
    'to use in various loops during he instal process
    '@param EEPosSheets, are the sheets to index

    Select Case Index

        Case 1
            EmpSheets = xlWSAllEEAnnul
            PosSheets = xlWSAllPositionAnnul

        Case 2
            EmpSheets = xlWSAllEEHourly
            PosSheets = xlWSAllPositionHourly

        Case 3
            EmpSheets = xlWSAllEESalary
            PosSheets = xlWSAllPositionSalary

    End Select

    Throw New ArgumentOutOfRangeException("Index")

End Function


I am trying to use that function on a For Loop as this:

VB
Sub copyFormulas()

    Dim eeRefSheets As Excel.Worksheet

    For i As Long = 1 To 3 Step 1

        eeRefSheets = EEPosSheets(i)

        With eeRefSheets
            Dim lngLr As Long

            lngLr = .Cells.Find(What:="*", SearchDirection:=Excel.XlSearchDirection.xlPrevious, SearchOrder:=Excel.XlSearchOrder.xlByRows).Row

            .Range("B6:AH6").AutoFill(.Range("B6:AH" & lngLr), Excel.XlAutoFillType.xlFillDefault)

        End With

    Next i

End Sub


But I am getting the following error:

VB
Error   1   Argument not specified for parameter 'EmpSheets' of 'Public Function EEPosSheets(Index As Long, ByRef EmpSheets As Microsoft.Office.Interop.Excel.Worksheet, ByRef PosSheets As Microsoft.Office.Interop.Excel.Worksheet) As Object


Not sure why, I am declaring eeRefSheetes as my parameter
Posted

1 solution

Hello,

For a start you function
EEPosSheets
does not work. It will always throw an exception whatever the index is...
You need to move the extension inside the case statement:
VB
Select Case Index

    Case 1
        EmpSheets = xlWSAllEEAnnul
        PosSheets = xlWSAllPositionAnnul

    Case 2
        EmpSheets = xlWSAllEEHourly
        PosSheets = xlWSAllPositionHourly

    Case 3
        EmpSheets = xlWSAllEESalary
        PosSheets = xlWSAllPositionSalary

    Case Else
        Throw New ArgumentOutOfRangeException("Index")
End Select


Secondly your method expects 3 parameters
EEPosSheets(Index As Long, ByRef EmpSheets As Excel.Worksheet, ByRef PosSheets As Excel.Worksheet)

So when you call it you need to pass 3 parameters
EEPosSheets(i, sheet1, sheet2 )


Thirdly a function should return something (somthing is an object of some kind...)
Public Function EEPosSheets(Index As Long, ByRef EmpSheets As Excel.Worksheet, ByRef PosSheets As Excel.Worksheet) As something


otherwise use a sub
Public Sub EEPosSheets(Index As Long, ByRef EmpSheets As Excel.Worksheet, ByRef PosSheets As Excel.Worksheet)


Good luck.

Valery.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900