+ Reply to Thread
Results 1 to 10 of 10

Macro to copy data to a workbook from another workbook with a changing file name

Hybrid View

  1. #1
    Registered User
    Join Date
    04-04-2012
    Location
    Gaithersburg, MD
    MS-Off Ver
    Excel 2010
    Posts
    7

    Macro to copy data to a workbook from another workbook with a changing file name

    Hi all,

    I'm pretty new to Excel and VBA...hope somebody can help me with this:

    I have built a workbook calculator to manage and sort data that comes in a weekly Excel file. As it is now, I have to copy and paste the data in the worksheet that I need to my workbook calculator and would like to automate the process with a macro. The problem I'm having is that the filename changes with the week that it comes in. For example...this week the file would be named "Weekly Data 4-06-2012" and next week the file would be named "Weekly Data 4-13-2012".

    Is there a way to have the macro identify just the first couple of words in the file name and recognize that as the file that it needs?

    Thanks so much for any help!!

    Ben

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Macro to copy data to a workbook from another workbook with a changing file name

    This would be great, but you would have many workbooks that start with the first two words.
    You could use a code such as
        Workbooks.Open Filename:= _
            "C:\Users\davesexcel\Downloads\" & Range("A1") & ".xls"
    Then it would open the workbook with the name in the folder location with the name you had in Cell A1.

    Change the folder location in the code.

  3. #3
    Registered User
    Join Date
    04-04-2012
    Location
    Gaithersburg, MD
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Macro to copy data to a workbook from another workbook with a changing file name

    That's not a bad idea, but the workbooks come once a week through Outlook and it's nothing that I have to save to a folder...the previous week's data gets saved over with the most current week in my active workbook. All I have to do is copy the cells from the entire worksheet into my workbook with all of the formulas in them. Is there a way to have Excel copy the data from the only other available open workbook besides my calculator? It wouldn't be as much of a problem if I didn't have to format the cells after that which I've figured out how to do with a macro.

    Is there a macro like "GetOpenFile" that would help?

    Thanks Dave!

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Macro to copy data to a workbook from another workbook with a changing file name

    Sure,
    Try this to open the workbook.
    Sub OpenWeeklyData()
    
        Dim File As String, Foldr As String
    
        Foldr = "C:\Users\davesexcel\Downloads\"
        File = Dir(Foldr & "Weekly Data*.xlsx")    '<= make sure ext is correct .xls 
    
        Workbooks.Open (Foldr & File)
    
    End Sub

  5. #5
    Registered User
    Join Date
    04-04-2012
    Location
    Gaithersburg, MD
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Macro to copy data to a workbook from another workbook with a changing file name

    The file is coming across as read-only so that macro won't read it unless I save it and turn read-only off...more steps than I was hoping for. If the file is already open from Outlook, I shouldn't need the "Folder" command, right? I did try to take that code and apply it to the recorded macro that wasn't working either but had the beginning elements that I was looking for:

    Sub Weekly_Data()
    
        Dim File As String
        File = "Weekly Data*.xls"
            
        Windows(File).Activate
        Sheets("Sheet1").Select
        Cells.Select
        Selection.Copy
        Windows("My Workbook.xlsm").Activate
        Sheets("Weekly Data").Select
        Cells.Select
        ActiveSheet.Paste
        Range("A1").Select
    End Sub
    I'm getting "Runtime Error 9: Subscript Out of Range" on the third line of code: Windows(File).Activate
    I tried changing the syntax of the second and third lines to see if that was the problem but no go. Here are the variations I tried:

    File = ("Weekly Data*.xls")
            
        Windows.Activate (File)
    Any thoughts?
    Last edited by gruittbm; 04-06-2012 at 06:33 AM.

  6. #6
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Macro to copy data to a workbook from another workbook with a changing file name

    Maybe we need to loop through the open workbooks.

    Sub Button1_Click()
    
        Dim Wb As Workbook
    
        For Each Wb In Workbooks
    
            If Wb.Name Like "Weekly Data" & "*" Then
    
                MsgBox "Found it " & Wb.Name
    
            End If
    
        Next Wb
    
    End Sub

  7. #7
    Registered User
    Join Date
    04-04-2012
    Location
    Gaithersburg, MD
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Macro to copy data to a workbook from another workbook with a changing file name

    Thank you so much!!! I've been working on this for days now (as I said, I'm new to this)

    Your code worked so I added it to what I already had:

    Sub Weekly_Data()
    
        Dim File As String
        Dim Wb As Workbook
        File = "Weekly Data*.xls"
        
        For Each Wb In Workbooks
            If Wb.Name Like "Weekly Data" & "*" Then
                Windows(Wb.Name).Activate
                Sheets("Sheet 1").Select
                Cells.Select
                Selection.Copy
                Windows("My Workbook.xlsm").Activate
                Sheets("Sheet 1").Select
                Cells.Select
                ActiveSheet.Paste
                Range("A1").Select
            End If
        Next Wb
        
    End Sub
    Is there anything extraneous in there from our earlier attempts? I'm trying to learn all this on the fly.

  8. #8
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Macro to copy data to a workbook from another workbook with a changing file name

    Sure thing,
    Instead of copying every cell in the worksheet, it just copies A1 to the last used cell.
    This is done without having to select the workbook or worksheet.

    You can use the worksheet name instead of the number if you want.

    These example codes could become usefull to you in the future
    http://www.davesexcel.com/vbacodes.htm#363409847

    Sub Weekly_Data2()
    
        Dim Rws As Long, Col As Integer, r As Range
        Dim Wb As Workbook, fRng As Range, Wsht As Worksheet
    
        For Each Wb In Workbooks
    
            If Wb.Name Like "Weekly Data" & "*" Then
    
                Set Wsht = Wb.Worksheets(1)
    
                With Wsht
    
                    Set r = Range("A1")
                    Rws = .Cells.Find(What:="*", After:=r, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                    Col = .Cells.Find(What:="*", After:=r, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    
                    Set fRng = Range(.Cells(1, 1), .Cells(Rws, Col))    ' range A1 to last cell on sheet
    
                    fRng.Copy Destination:=Workbooks("My Workbook.xlsm").Worksheets(1).Range("A1")
    
                End With
    
            End If
    
        Next Wb
    
    End Sub
    Last edited by davesexcel; 04-06-2012 at 07:50 AM.

  9. #9
    Registered User
    Join Date
    04-04-2012
    Location
    Gaithersburg, MD
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Macro to copy data to a workbook from another workbook with a changing file name

    That's a nice resource for future problems on your website...I bookmarked it so I can finish reading through it.

    Haven't had a chance to try the code you sent last week...hoping to get to it tonight or tomorrow. Just wanted to let you know I've seen it and appreciate all of your help.

    Thanks Dave!

  10. #10
    Registered User
    Join Date
    04-04-2012
    Location
    Gaithersburg, MD
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Macro to copy data to a workbook from another workbook with a changing file name

    That worked like a charm...and it doesn't cause seizures like the recorded macro I was working with that activated worksheet after worksheet. I have a few more parts to this problem that I'd like to make this simple too, but I want to try to figure them out on my own first using the reference link you gave me above. Do you mind if I contact you if I get stuck there?

    Thanks again for all of your help!!

    Ben

+ 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