0

I am new to programming and obviously not very experienced with writing functions. I wrote the function below, but as I get to the end, the End Function statements has an error: Function "viewSheets" doesn't return a value on all code paths.

I have researched the issue, but to no avail. Here is my first try at a function:

Option Strict On
Option Explicit On

Imports Microsoft.Office.Interop.Excel
Imports System.Windows.Forms
Module sheetView

Function viewSheets(sheetName As String, status As Boolean, show As String) As String

    Dim ThisApplication As Excel.Application = New Excel.Application()
    Dim WB As Excel._Workbook
    Dim WS As Excel.Worksheet

    WB = CType(Globals.ThisWorkbook.Application.ActiveWorkbook, Excel.Workbook)
    WS = DirectCast(WB.Sheets("sheetName"), Excel.Worksheet)

    With ThisApplication

        .ScreenUpdating = False

        WS.Select()

        .ActiveWindow.DisplayGridlines = status
        .ActiveWindow.DisplayHeadings = status
        .ActiveWindow.DisplayWorkbookTabs = status
        .DisplayFormulaBar = status
        .DisplayStatusBar = status
        .ExecuteExcel4Macro("Show.ToolBar(""Ribbon"", show )")


        .ScreenUpdating = True


    End With

End Function

End Module

1
  • Actually if you are not going to returning a value from your function, I would just make it a subroutine(method) which does not require a return statement Commented Jul 6, 2013 at 3:06

2 Answers 2

2

You did declare a function, which by definition must return a value.

In VB you have to assign the function name or since VB.Net you can return using the return statement:

Before the End Function just put:

    viewSheets=""

You should read more about Functions and Sub routines in VB.

Difference between Private Sub, Function and Class

Sign up to request clarification or add additional context in comments.

1 Comment

Use the Return statement in VB.NET instead of assigning a value to the function's name: Return ""
2

It's declared as a function that returns a string. Because you have Option Strict turned on, you are required to have a Return statement on all code paths. The Return needs to return a string.

3 Comments

Actually, are you sure it's not just a warning? It's not because of Option Strict as I just found out, but my answer still applies.
what if it is just a warning ?
If it's just a warning, it means that you have at least one code path in the function which does not Return a value. If that code is hit, the function will return a null value (Nothing). That is, FunctionThatDoesNotReturn() is Nothing will be True.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.