+ Reply to Thread
Results 1 to 4 of 4

Userform that allows user to select an open file?

Hybrid View

  1. #1
    Registered User
    Join Date
    12-23-2010
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    66

    Userform that allows user to select an open file?

    Hello,

    This may be kind of difficult to code. I'm looking for a UserForm that has a list of all the currently open excel files. I want to allow the user to select one of the files in that list and then once that file is selected, import data from that file.

    I know how to import data from a file, just not how to create the UserForm part. The reason I want to use a UserForm is because the name of the input file changes every day, and there is not pattern in naming convention I can take advantage of. Unless there's another better method, this is what I thought of.

    Thanks!

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,861

    Re: Userform that allows user to select an open file?

    wouldnt it be wise just to open the file with Open dialog?
    Of course you can browse workbooks collection (contains all open wbks in this instance of excel).
    something like
    dim wbk as excel.workbook
    for each wbk in application.workbooks
      if wbk.name <> thisworkbook.name then
      ' populate some control in Userform like list.add or something
       ' or check some characteristic sign that "this is the file" an if "it is -> exit for
      end if
    next wbk
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    12-23-2010
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    66

    Re: Userform that allows user to select an open file?

    Quote Originally Posted by Kaper View Post
    wouldnt it be wise just to open the file with Open dialog?
    Of course you can browse workbooks collection (contains all open wbks in this instance of excel).
    something like
    dim wbk as excel.workbook
    for each wbk in application.workbooks
      if wbk.name <> thisworkbook.name then
      ' populate some control in Userform like list.add or something
       ' or check some characteristic sign that "this is the file" an if "it is -> exit for
      end if
    next wbk
    Thanks! That would work too. Is there a way to open a file and then have it copy in data even if I don't know the name of that document (since it will change every day)? I just want to push a button, and have it copy in data. So whatever method is the easiest works.
    Last edited by anon; 02-28-2014 at 01:53 PM.

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,861

    Re: Userform that allows user to select an open file?

    Let's make it with small steps - here is a proposition of a framework for you. Create a button and assign the macro mytestmacro to it:
    First test it and then
    Sub mytestmacro()
    Dim FileName As Variant
    Dim SourceFile as Workbook, MainFile as workbook : Set MainFile=ThisWorkbook
    
    FileName = Application.GetOpenFilename(filefilter:="Excel files (*.xls*),*.xls*", FilterIndex:=3, Title:="Choose a file")
    If FileName = False Then  
       MsgBox "No file selected!", vbcritical
       Exit Sub
    else
       Set SourceFile = Workbooks.Open(Filename)
       ' and here your processing, for instance
       SourceFile.Sheets(1).Range("A1:D5").copy MAinFile.Sheets(1).cells(rows.count,"A").end(xlup).offset(1,0)
       ' then close file and let user (you) know it is finished
       sourcefile.close false
       msgbox "finished copying from " & Filename      
    end if
    End Sub
    if it works adjust the copying part:
       SourceFile.Sheets(1).Range("A1:D5").copy MAinFile.Sheets(1).cells(rows.count,"A").end(xlup).offset(1,0)
    to copy from and to right places in your workbooks (as it is now it copies from Sheet1!A1:D5 of newly open file to first blank cell in A column in Sheet1 in your main file.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Macro that prompts user to select a file to open and perform another macro on this file
    By grimmy26 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-16-2014, 08:39 AM
  2. select a item in userform listbox to open a file
    By simeonmein in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-17-2013, 09:36 AM
  3. [SOLVED] Select cell when userform is open
    By shiftyspina in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-14-2013, 09:21 AM
  4. [SOLVED] Open specific folder, select file to open and copy then paste
    By Kranky in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-22-2012, 12:14 AM
  5. UserForm is automatically run when user open the Excel file?
    By qed59 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-07-2010, 12:16 AM

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