+ Reply to Thread
Results 1 to 10 of 10

Help with Open File from Directory

Hybrid View

  1. #1
    Registered User
    Join Date
    03-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    39

    Help with Open File from Directory

    Dear Superusers,

    Could someone please tell me how I can add the 'select file from directory' for below mentioned code?

    Currently the code is picking all files that are in the folder location, however, I would like to select them.
    Tried couple of things but failed...

    Thanks for your help!



    Sub Merge2MultiSheets()
    Dim wbDst As Workbook
    Dim wbSrc As Workbook
    Dim wsSrc As Worksheet
    Dim MyPath As String
    Dim strFilename As String
        
        Application.DisplayAlerts = False
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        MyPath = "\\Nl-file01\elc-depts\Shared Services\Cash management\Cash forecast\2012 reports\04 April" ' change to suit
        Set wbDst = ActiveWorkbook
        strFilename = Dir(MyPath & "\*.xls", vbNormal)
        
        If Len(strFilename) = 0 Then Exit Sub
        
        Do Until strFilename = ""
            
                Set wbSrc = Workbooks.Open(Filename:=MyPath & "\" & strFilename)
                
                Set wsSrc = wbSrc.Worksheets(1)
                
                wsSrc.Copy after:=wbDst.Worksheets(wbDst.Worksheets.Count)
                
                wbSrc.Close False
            
            strFilename = Dir()
            
        Loop
        
        
        Application.DisplayAlerts = True
        Application.EnableEvents = True
        Application.ScreenUpdating = True
        
    End Sub

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Help with Open File from Directory

    Try this
    Dim FName as variant
    
    FName = Application.GetOpenFilename(FileFilter:="All files (*.*), *.*", Title:="Please open the CSV file")
    If FName = "False" Then
        MsgBox "You have not selected a file."
        Exit Sub
    Else
        Workbooks.Open Filename:=FName
    End If
    Change it as per your requirement.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    03-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Help with Open File from Directory

    Thanks Alrette for the Quick Reply - can you help me with adding a directory to this as well?

  4. #4
    Registered User
    Join Date
    03-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Help with Open File from Directory

    Quote Originally Posted by MarianneBal View Post
    Thanks Alrette for the Quick Reply - can you help me with adding a directory to this as well?
    And I would need to select multiple files.

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Help with Open File from Directory

    Use this line
     ChDir "D:\Test"
    (change it to your system path)

    before the code i gave you.

  6. #6
    Registered User
    Join Date
    03-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Help with Open File from Directory

    Hello again,

    It's not working - I also need to select multiple files...

    Any suggestions?

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Help with Open File from Directory

    What error are you getting? I tested it at my end and it works. When it prompts you to open the file, the folder you have selected will popup.

    To repeat for multiple files, do you want a popup asking "Do you want to open another file?" ?

  8. #8
    Registered User
    Join Date
    03-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Help with Open File from Directory

    Hello Arlette,

    With the code I want to get a POP UP where I can selecte multiple files.

    From my selection I want the code to copy the first worksheet in the Active Workbook.
    (this part does actually work, but with my code mentioned above it picks automatically all files in the folder)

    For another sheet I have a code that allows me to select multiple files, but I'm somehow not able to 'merge' those 2 codes.

    The Code with the multiple file selection that works is:

    Private Declare Function SetCurrentDirectoryA Lib _
        "kernel32" (ByVal lpPathName As String) As Long
    
    Sub ChDirNet(szPath As String)
        SetCurrentDirectoryA szPath
    End Sub
    
    
    Sub MergeSpecificWorkbooks()
        Dim MyPath As String
        Dim SourceRcount As Long, FNum As Long
        Dim mybook As Workbook, BaseWks As Worksheet
        Dim sourceRange As Range, destrange As Range
        Dim rnum As Long, CalcMode As Long
        Dim SaveDriveDir As String
        Dim FName As String
    
    
        Application.DisplayAlerts = False
        Application.EnableEvents = False
        Application.ScreenUpdating = False
    
        SaveDriveDir = CurDir
        ' Change this to the path\folder location of the files.
        ChDirNet "\\Nl-file01\elc-depts\Shared Services\Cash management\Cash forecast\2012 reports\04 April"
    
        FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", _
                                            MultiSelect:=True)
                                            
    ...

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Help with Open File from Directory

    Do you mean select multiple files at one time itself, using the Ctrl key?

    Is there a specific reason why you want to personally (i mean physically) select the files instead of having the macro do it?

  10. #10
    Registered User
    Join Date
    03-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Help with Open File from Directory

    I would rather have the possibility to select the files manually, as the files are in different folders every month and as I won't be the only user, I rather not want other users overwriting the code.

    Yes, Multiple files with Ctrl key.

    Furthermore, I was wondering if it would be possible to add the value of a Range to the name of the new Worksheets (that were copied to the active workbook)?

    Thanks so much for your help!
    Last edited by MarianneBal; 03-27-2012 at 10:28 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1