How will I Calling a particular value from other function for using in another function Visual Basic?

Viewed 64

I am using Visual Basic.I have two functions. I am creating a excel file and I assign a value in first function. after that, I need to reach a that value's cell and get the data in second function. But I can not reach it. How can i do that ? First Function :

Public Function FirstFunction As String:
        Dim xls As Microsoft.Office.Interop.Excel.Application
        Dim xlsworkbook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlsworksheet As Microsoft.Office.Interop.Excel.Worksheet

        xls = New Microsoft.Office.Interop.Excel.Application
        xlsworksheet = xlsworkbook.Worksheets("Template")
End Function

Second Function:

Public Function SecondFunction As String:
    Dim a1 As String = xlsworksheet.Range("A1").Value2
    xlsworksheet.Cells(B,2) = a1
End Function
2 Answers

If I understood your question, you need to send values from a function to another... The way to do this is

' If you don't need a value to be returned, use SUB instead
Public Sub FirstFunction As String
    
    Dim DataLeave As String = "Sending this to 2nd function"

    MsgBox(SecondFunction(DataLeave)) ' Will print "Now I need this"
End Sub



Public Function SecondFunction(DataEnter As String) As String
    Msgbox(DataEnter) ' Will print: "Sending this to 2nd function"

    DataEnter = "Now I need this"
            
    Return DataEnter
End Function

As per my understanding about your question is you want to access that worksheet object in another function. You can achieve that by two ways either you define your excel xlsworksheet object globally

public class YourClassName 
        Dim xls As Microsoft.Office.Interop.Excel.Application
        Dim xlsworkbook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlsworksheet As Microsoft.Office.Interop.Excel.Worksheet

        Public Function FirstFunction As String:
             xls = New Microsoft.Office.Interop.Excel.Application
             xlsworksheet = xlsworkbook.Worksheets("Template")
        End Function

        Public Function SecondFunction As String:
            if not isnothing(xlsworksheet)
                Dim a1 As String = xlsworksheet.Range("A1").Value2
                xlsworksheet.Cells(B,2) = a1
            End if 
        End function

End Class 

or

You can pass reference of that object to another function. One more thing you haven't return anything then why are you using function instead Sub

Public Function FirstFunction As String:
        Dim xls As Microsoft.Office.Interop.Excel.Application
        Dim xlsworkbook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlsworksheet As Microsoft.Office.Interop.Excel.Worksheet

        xls = New Microsoft.Office.Interop.Excel.Application
        xlsworksheet = xlsworkbook.Worksheets("Template")
        secondFunction(xlsworksheet )
End Function

Public Function SecondFunction (byref xlsworksheet as  Microsoft.Office.Interop.Excel.Worksheet)  As String:
    Dim a1 As String = xlsworksheet.Range("A1").Value2
    xlsworksheet.Cells(B,2) = a1
End function




Related