Consulting

Results 1 to 12 of 12

Thread: VBA - copying & pasting data using file path

  1. #1

    VBA - copying & pasting data using file path

    Firstly hello to all of you! Please be kind, I'm a total VBA noob!

    I would like to create a macro in a workbook called "Data". The macro should pull in data from a closed workbook called "Data Source" and into an existing sheet in the "Data" workbook called DataSheet.

    I would like the file p
    ath of the "Source Data" to be written in cell A1 of Sheet1 in the "Data" file (e.g. C:\Users\Fred\Desktop\Data Source.xlsm). This is so that I am easily able to change the file path of the Data Source spreadsheet.

    Would someone be kind enough to he
    lp me write this Macro?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Public Sub CopyData()
    Dim ws As Worksheet
    Dim filename As String
    
        With Application.FileDialog(msoFileDialogOpen)
        
            .AllowMultiSelect = False
        
            If .Show Then
            
                filename = .SelectedItems(1)
    
                Set ws = Workbooks.Open(filename:=filename)
                ws.Worksheets(1).UsedRange.Copy
                Workbooks("Data").Worksheets("DataSheet").Range("A1").Copy
                ws.Close SaveChanges:=False
                Set ws = Nothing
            End If
        End With
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Thank you for your response xld!

    Unfortunately I am getting a "Compile error: Method or data member not found". Any idea how I can get the data from Data Source wb into the Data wb?

    I am attaching the two workbooks.Data.xlsxData Source.xlsx

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try this

    Public Sub CopyData()
        Dim wb As Workbook
        Dim filename As String
         
        With Application.FileDialog(msoFileDialogOpen)
             
            .AllowMultiSelect = False
             
            If .Show Then
                 
                filename = .SelectedItems(1)
                 
                Set wb = Workbooks.Open(filename:=filename)
                wb.Worksheets(1).UsedRange.Copy Workbooks("Data.xlsx").Worksheets("DataSheet").Range("A1")
                wb.Close SaveChanges:=False
                Set wb = Nothing
            End If
        End With
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Hi xld,

    The code brings up "File Open" screen (screenshot attached) and doesn't pull the data directly. When I however pick the file it pulls the data!! DO you think the file path is in the wrong format?

    Screenshot.JPG

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yes, I have put a file picker in the code rather than have you hard-coding it into your workbook.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Ah ok! Is there any way the VBA can use the hard coded cell as the path?

    xld, if you are ever in London I need to buy you beers, many many beers!

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It could, but which workbook/worksheet should it be in? I would still use a file dialog to populate that cell.

    Actually, I will probably be in London next week, maybe meeting up with Ken Puls and Andy Pope
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Quote Originally Posted by xld View Post
    It could, but which workbook/worksheet should it be in? I would still use a file dialog to populate that cell.

    Actually, I will probably be in London next week, maybe meeting up with Ken Puls and Andy Pope
    I’d prefer a cell that has a hard coded file path in the Data workbook. This is because i have already used VBA to find the specific file name I am looking for (all data source workbooks use date and time stamp).

    Great, let me know! I’ll send you my details via message.

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    As I said, where is that cell. which workbook, which worksheet?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    Quote Originally Posted by xld View Post
    As I said, where is that cell. which workbook, which worksheet?

    Hi xld, I have the folder path in cell A1 of Sheet1 of the Data workbook. It’s the path highlighted in red in the Data workbook attached above.

    many thanks for all your help!

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    SO maybe this

    Public Sub CopyData()
        Dim wb As Workbook
        Dim filename As String
         
        filename = Workbooks("Data.xlsx").Worksheets(1).Range("A1").Value
         
        Set wb = Workbooks.Open(filename:=filename)
        wb.Worksheets(1).UsedRange.Copy Workbooks("Data.xlsx").Worksheets("DataSheet").Range("A1")
        wb.Close SaveChanges:=False
        Set wb = Nothing
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Tags for this Thread

Posting Permissions

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