+ Reply to Thread
Results 1 to 18 of 18

How do i code a VBA to ask the user to select which file to use

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    How do i code a VBA to ask the user to select which file to use

    The current VBA i have is as follows;

    Workbooks.Open Filename:= _
            "\\Via.novonet\dfs\LIFE\F\test\test\test\Source Files\test\test\test 300412.xls"
        Range("A1:DK47073").Select
        Selection.Copy
        Windows("test.xlsm").Activate
        Sheets("test Data").Select
        Range("A1").Select
        ActiveSheet.Paste
        Sheets("test pivot").Select
        Range("D15").Select
        Application.CutCopyMode = False
        ActiveSheet.PivotTables("PivotTable3").PivotCache.Refresh
    I want to be able to code it so the macro asks the user which file to use but will use the same steps following this?

    Any ideas

  2. #2
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: How do i code a VBA to ask the user to select which file to use

    I have tried this but there is an error somewhere as it pastes all the data from a summary sheet to the next sheet in the workbook.

    I need it to open a new file, copy all the data and paste it to "test sheet" then close the opened sheet.

    NewFN = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
        'Workbooks.Open Filename:=NewFN
    '
        
        Range("A1:DK47073").Select
        Selection.Copy
        Windows("test.xlsm").Activate
        Sheets("test").Select
        Range("A1").Select
        ActiveSheet.Paste
        Sheets("test").Select
        Range("D15").Select
        Application.CutCopyMode = False
        ActiveSheet.PivotTables("PivotTable3").PivotCache.Refresh

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

    Re: How do i code a VBA to ask the user to select which file to use

    It will be good if you try explaining your problem with the help of an attached excel file. Can you clearly state what you require the macro to do?

    Also, you can do away with the select & activate statements.
    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]

  4. #4
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: How do i code a VBA to ask the user to select which file to use

    I required coding to prompt the user for the file, copy all the data from the data sheet of the opened file and paste it into the data tab of the summary template.

    Attached is the summary document.
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: How do i code a VBA to ask the user to select which file to use

    Can you view the attachment

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

    Re: How do i code a VBA to ask the user to select which file to use

    I just edited the code you had to this
    Sub Macro7()
    '
    ' Macro7 Macro
    
        Dim wbOpen As Workbook
         
        Set wbOpen = Workbooks.Open _
       ("\\Via.novonet\dfs\LIFE\F\INVReporting\CONFIDENTIAL\Inv_Reporting_2012\Source Files\S29" & Range("FileName"))
         
              
        With wbOpen
            .Worksheets("Data").UsedRange.Copy ThisWorkbook.Worksheets("S29 Data").Range("A1")
            .Close False
    End With
    Add the pivot refresh code after this.

  7. #7
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: How do i code a VBA to ask the user to select which file to use

    Thanks, i get the following error message with this line;

    
    Set wbOpen = Workbooks.Open _
       ("\\Via.novonet\dfs\LIFE\F\INVReporting\CONFIDENTIAL\Inv_Reporting_2012\Source Files\S29" & Range("FileName"))
    Error is Method 'Range' of object _global failed

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: How do i code a VBA to ask the user to select which file to use

    Try this line of code:

    Application.FindFile
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  9. #9
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: How do i code a VBA to ask the user to select which file to use

    Still not happy.

    Application.FindFile
        
        With wbOpen
            .Worksheets("Data").UsedRange.Copy ThisWorkbook
            Window.Worksheets("S29 Data").Range ("A1")
            .Close False
            
        Windows("Exposure template Macro.xlsm").Activate
        Sheets("S29 Data").Select
        Range("A1").Select
        ActiveSheet.Paste
        Sheets("S29 Pivot").Select
        Range("D15").Select
        Application.CutCopyMode = False
        ActiveSheet.PivotTables("PivotTable3").PivotCache.Refresh
    Once the file has been opened it needs to copy the workbook and then switch back to the previous workbook and paste the copied data into the sheet.

    I'm not sure this is doing those things.

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

    Re: How do i code a VBA to ask the user to select which file to use

    Ok hold on. I have already provided you the code to do the copy paste. What is currently not working is the workbooks.open part. Can you give me the path from where you open the file ? Also, give me the name of the file.

  11. #11
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: How do i code a VBA to ask the user to select which file to use

    The filepath of the data is;

    \\Via.novonet\dfs\LIFE\F\INVReporting\CONFIDENTIAL\Inv_Reporting_2012\Source Files\S29

    File name will be S29 Data then DDMMYY (this will be a different date each month, which is why i wanted to prompt the user to select which file

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

    Re: How do i code a VBA to ask the user to select which file to use

    There is no drive letter before the path? Like C or D?

  13. #13
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: How do i code a VBA to ask the user to select which file to use

    It's a network drive so doesn't need to be?

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

    Re: How do i code a VBA to ask the user to select which file to use

    Try this
    dim FName as variant
    
    chdir "\\Via.novonet\dfs\LIFE\F\INVReporting\CONFIDENTIAL\Inv_Reporting_2012\Source Files\"
    FName = Application.GetOpenFilename(FileFilter:="All files (*.*), *.*", Title:="Please open the file")
    If FName = "False" Then
        MsgBox "You have not selected a file."
        Exit Sub
    Else
        Workbooks.Open Filename:=FName
        FName = ActiveWorkbook.Name
    End If
    With workbooks(Fname)
            .Worksheets("Data").UsedRange.Copy ThisWorkbook.Worksheets("S29 Data").Range("A1")
            .Close False
    End With
    Last edited by arlu1201; 05-15-2012 at 10:04 AM. Reason: Edited code

  15. #15
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: How do i code a VBA to ask the user to select which file to use

    I have updated and re run

    
    .Worksheets("Data").UsedRange.Copy ThisWorkbook.Worksheets("S29 Data").Range("A1")
    Error message is object required.

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

    Re: How do i code a VBA to ask the user to select which file to use

    Sorry, i have made the changes to the above code.

  17. #17
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: How do i code a VBA to ask the user to select which file to use

    Spot on, works a treat, thank you for help, really apprecaite it!

  18. #18
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: How do i code a VBA to ask the user to select which file to use

    Hi,

    Did you ever try "Application.FindFile"??

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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