+ Reply to Thread
Results 1 to 9 of 9

Thread: Create a prompt in a macro to "browse for the Source file

  1. #1
    Registered User
    Join Date
    10-11-2010
    Location
    US
    MS-Off Ver
    NA
    Posts
    41

    Smile Create a prompt in a macro to "browse for the Source file

    I have a file that is being created weekly. There is a spreadsheet that contains data I want for another excel file. I want to copy the entire worksheet and past it into another file. However the date in the file name changes weekly. Is there a way to create a prompt in the VBA to browse for the file and permit the user to select the file needing to be imported.

    Thanks!

    Attached Files Attached Files
    Last edited by ggremel; 03-07-2011 at 02:21 PM.

  2. #2
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Create a prompt in a macro to "browse for the Source file

    Try this

    
    Option Explicit
    
    
    Sub openfile()
        Dim sFil   As String
        Dim sTitle As String
        Dim sWb    As String
        Dim iFilterIndex As Integer
    on error goto err_handler
        ' Set up list of file filters
        sFil = "Excel Files (*.xls),*.xls"
        ' Display *.xls by default
        iFilterIndex = 1
        ' Set the dialog box caption
        sTitle = "Select  File to Zip"
        ' Get the filename
        sWb = Application.GetOpenFilename(sFil, iFilterIndex, sTitle)
    
        Workbooks.Open Filename:=sWb
    Exit Sub
    err_handler:
    Msgbox "No selection made"
    End Sub
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  3. #3
    Registered User
    Join Date
    10-11-2010
    Location
    US
    MS-Off Ver
    NA
    Posts
    41

    Re: Create a prompt in a macro to "browse for the Source file

    You are a virtual rock star. Thank you.

  4. #4
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Create a prompt in a macro to "browse for the Source file

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  5. #5
    Registered User
    Join Date
    10-11-2010
    Location
    US
    MS-Off Ver
    NA
    Posts
    41

    Re: Create a prompt in a macro to "browse for the Source file

    Will do. If I want to past the file name in a cell (Filename:=sWb). How would I go about adding this to the report.xls file.

    Thanks

  6. #6
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Create a prompt in a macro to "browse for the Source file

    If the code is in the Report workbook then

    ThisWorkBook.Sheet1.Cells(1,1).Value=sWb
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  7. #7
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Create a prompt in a macro to "browse for the Source file

    If the code is in the Report workbook then

    ThisWorkBook.Sheet1.Cells(1,1).Value=sWb
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  8. #8
    Registered User
    Join Date
    10-11-2010
    Location
    US
    MS-Off Ver
    NA
    Posts
    41

    Re: Create a prompt in a macro to "browse for the Source file

    Quote Originally Posted by royUK View Post
    If the code is in the Report workbook then

    ThisWorkBook.Sheet1.Cells(1,1).Value=sWb
    I listed the code as part of the macro and then as a function. I am getting an error on each insertion. See updated documents and let me know if there is another step I need to implement.

    Thank you.
    Attached Files Attached Files

  9. #9
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Create a prompt in a macro to "browse for the Source file

    You can't place the line of code that I gave you into a cell.

    I've reviewed your code & made some improvements.

    Option Explicit
    
    
    Sub openfile()
        Dim sFil As String
        Dim sTitle As String
        Dim sWb As String
        Dim iFilterIndex As Integer
    
        With Application
            .ScreenUpdating = False
    
            ' Set up list of file filters
            sFil = "Excel Files (*.xls),*.xls"
            ' Display *.xls by default
            iFilterIndex = 1
            ' Set the dialog box caption
            sTitle = "Select  File to Open"
            ' Get the filename
            On Error GoTo err_handler
            sWb = Application.GetOpenFilename(sFil, iFilterIndex, sTitle)
    
            Workbooks.Open Filename:=sWb
            ActiveWorkbook.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets("BRT Data").Range("A1")
            ThisWorkbook.Sheets("Sheet1").Cells(1, 1) = "Data source: " & sWb
            'close source workbook
            ActiveWorkbook.Close False
    
    exit_proc:
            .ScreenUpdating = True
            .CutCopyMode = False
        End With
        Exit Sub
    err_handler:
        MsgBox "No selection made", vbCritical, "User Cancelled"
        Resume exit_proc
    End Sub
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

+ 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.2.0