gabx
gabx

Reputation: 482

can't get returned data from a function

I am unable to catch the return of my function when using in a Sub(). Here is my function. It iswriten in the same module as my Sub().

Option Explicit
Public Function GetMyPath(ByVal strPath As String) As String
' Get the directory strPath from the user
' strPath is the default path given by the function argument

Dim fldr As FileDialog
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)

On Error GoTo 0

With fldr
     .Title = "Selectionner un dossier"
     .AllowMultiSelect = False
     .InitialFileName = strPath
      If .Show <> -1 Then GoTo NextCode
      strPath = .SelectedItems(1)   
End With

NextCode:
Set fldr = Nothing

End Function

As you can see, it is entended to return a selected path by the user as string. This function works fine as a final MsgBox() will correctly show the selected path. Now the first part of my Sub ()

Sub SaveToPDF()
'Export RngToExport as FileFormat to MyPath 

Dim ToDate As String
Dim ws As Worksheet
Dim RngSelect As Range
Dim Response As Integer
Dim Fileformat As Excel.XlFileFormat
Dim RngToExport, NameOfWorker As Variant
Dim MyPath As String



Set ws = Workbooks("Gestion_salaires.xlsm").Application.Sheets("Export")
Set RngToExport = ws.Range(Cells(1, 1), Cells(43, 7))
Set NameOfWorker = Cells(14, 19)

MyPath = "Y:\BATORA_BATURIX\Employes\Walter H. BAQUE B\bulletin_salaire\"

''We want to warn about errors before saving
If Cells(12, 21).Value <> 0 Or Cells(12, 22) <> 0 Or Cells(12, 23) > 0 Then GoTo Warning_


Response = MsgBox(Prompt:="Voulez-vous utiliser l'emplacement par défaut ?", Buttons:=vbYesNo)
    If Response = vbNo Then
    MyPath = GetMyPath("C:\")
    End If

MsgBox(MyPath) '' just for debuging. MyPath is empty :-(

What am I doing wrong ? Writing Set MyPath = GetMyPath("C:\") doesn't work as it is not an object. Thank you for help.

Upvotes: 0

Views: 61

Answers (1)

Gary&#39;s Student
Gary&#39;s Student

Reputation: 96753

You need to insert a line like:

GetMyPath = strPath

before the End Function.

Otherwise the string is not getting passed back out of the function.

Upvotes: 2

Related Questions