0

I have 10 records (rows) in my Excel sheet and I am taking it into array and I am not able to return it from function. I get only the last value. Below is my script.

Function fnFetchDataUsingArray()
    Dim arrExcelValues()
    sSheetName = Environment.Value("TestName")

    sExcelWorkbookPath = "I:\ProLinkIII_TestAutomation\trunk\ProLink_TestAutomation\Datasheet\DTSheet_5700C.xlsx"

    Set objExcel = CreateObject("Excel.Application")
    Set objWorkbook = objExcel.Workbooks.Open(sExcelWorkbookPath)

    objExcel.Visible = False
    Set currentWorkSheet = objExcel.ActiveWorkbook.Worksheets(sSheetName)

    i = 1
    x = 0

    Do Until objExcel.Cells(i, 2).Value = ""
        ReDim Preserve arrExcelValues(x)

        arrExcelValues(x) = objExcel.Cells(i, 2).Value

        i = i + 1
        x = x + 1

        fnFetchDataUsingArray = arrExcelValues(x-1)
    Loop

    objExcel.Quit
End Function

Calling the function:

arr = fnFetchDataUsingArray()
MsgBox arr

I am getting only the last value. Attached screenshot of my datasheet.

Datasheet Screenshot

1

1 Answer 1

2

The line fnFetchDataUsingArray = arrExcelValues(x-1) is just repeatedly assigning the last value in arrExcelValues to the return value of the function. You need to assign the array arrExcelValues to the return value of the function after your Do loop.

So after your loop try this: fnFetchDataUsingArray = arrExcelValues.

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

Comments

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.