none
Open a file in default directory RRS feed

  • Question

  • Hi All.......

    Please help me with the code to open a file from the /XYZ Directory located in the Default directory.

    File to have filename as value in selected cell plus *.xlsm....ie like MyFile*.xlsm

    TIA

    Chuck, CABGx3


    Chuck, CABGx3

    Tuesday, July 12, 2016 6:34 PM

Answers

  • For example:

    Sub TestOpen()
        Dim strPath As String
        Dim strFile As String
        Dim wbk As Workbook
        strPath = Application.DefaultFilePath & "\XYZ\"
        strFile = Dir(strPath & ActiveCell.Value & "*.xlsm")
        If strFile = "" Then
            MsgBox "File not found!", vbCritical
            Exit Sub
        End If
        Set wbk = Workbooks.Open(strPath & strFile)
        ' More code here
        ' ...
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by CABGx3 Tuesday, July 12, 2016 11:45 PM
    Tuesday, July 12, 2016 8:07 PM

All replies

  • For example:

    Sub TestOpen()
        Dim strPath As String
        Dim strFile As String
        Dim wbk As Workbook
        strPath = Application.DefaultFilePath & "\XYZ\"
        strFile = Dir(strPath & ActiveCell.Value & "*.xlsm")
        If strFile = "" Then
            MsgBox "File not found!", vbCritical
            Exit Sub
        End If
        Set wbk = Workbooks.Open(strPath & strFile)
        ' More code here
        ' ...
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by CABGx3 Tuesday, July 12, 2016 11:45 PM
    Tuesday, July 12, 2016 8:07 PM
  • Thank you, kind Sir

    A couple of small tweaks to fit my application and your code worked perfectly!

    Thanks also for presenting your answers in such a manner that I can understand and tweak what is needed instead of having to keep asking back for more help.

    Your assistance is much appreciated,

    Regards,

    Chuck, CABGx3



    Chuck, CABGx3

    Tuesday, July 12, 2016 11:48 PM