#Value! when returning Function output to excel (reading from other excel file)

maxicanfly

New Member
Joined
Jun 22, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,
I am developing an excel VBA function that reads a value from another worksheet and workbook. I have done the debugging of it and the results is stored in the variable just fine, but in the excel cell, I get #Value!. This Value! issue triggers immediately after I call off the worksheet in the VBA code below. If I freeze that sentence(and bodge up the code), my function returns the set variable.
Set ws = src.Worksheets(ExcelWorksheet) once active triggers #Value! as output of the function in my cell. Can someone help me? thanks

VBA Code:
Function Paragraph31()



'this code finds  the column and

Application.ScreenUpdating = False
Dim FilePath As String, FilePathPrelim As String, FileName, ExcelWorksheet As String
IWP = "IWP-4210-18-1100-050"

With CreateObject("Scripting.FileSystemObject")
FilePath = .GetParentFolderName(Application.ActiveWorkbook.Path) & "/Spreadsheets/" & IWP & "/"
End With
'
PartialFilename = "Detailing*"

Dim ws As Worksheet
Dim ExcelTable As TableObject

ExcelWorksheet = "Hairpin"
'
                    
       Dim src As Excel.Workbook
       FileName = Dir(FilePath & PartialFilename)

       Set src = Workbooks.Open(FilePathPrelim & FileName, True, True)
       Set ws = src.Worksheets(ExcelWorksheet)

       'Paragraph = "ok"
       Dim output As String

       ComparisonVariable = (src.Worksheets(ExcelWorksheet).Range("M1").Value)

       If ComparisonVariable = 1 Then
       'output = "YES"
       output = 1
       Else
        'output = "NO "
        output = 2
        End If


   src.Close
   
output2 = CStr(output)

'Paragraph31 = output2

'Paragraph31 = "test"
 
Application.ScreenUpdating = True
       
Paragraph31 = output2

'Paragraph31 = 2


End Function
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,214,827
Messages
6,121,803
Members
449,048
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top