+ Reply to Thread
Results 1 to 9 of 9

How to copy values from one OPEN workbook (with variable file name) to another

Hybrid View

Kitko How to copy values from one... 01-25-2013, 08:16 AM
OllieB Re: How to copy values from... 01-25-2013, 08:32 AM
Kitko Re: How to copy values from... 01-25-2013, 08:51 AM
JOHN H. DAVIS Re: How to copy values from... 01-25-2013, 09:09 AM
OllieB Re: How to copy values from... 01-25-2013, 08:58 AM
OllieB Re: How to copy values from... 01-25-2013, 08:58 AM
OllieB Re: How to copy values from... 01-25-2013, 09:02 AM
Kitko Re: How to copy values from... 01-25-2013, 01:50 PM
mike7952 Re: How to copy values from... 01-25-2013, 09:03 AM
  1. #1
    Registered User
    Join Date
    01-25-2013
    Location
    Slovakia
    MS-Off Ver
    Excel 2011 Mac
    Posts
    7

    Question How to copy values from one OPEN workbook (with variable file name) to another

    Hello there :-)

    Every month I receive a batch of new files with names such as Analysis_Report_0001, 0002, 0003 etc. The Analysis_Report string is always there but the numbers are changing - you get the idea.

    Then I have a file Analysis Summary where I need to input certain values from the Analysis_Report files.

    Here's the code that works (I have both files already open and the macro is in the Analysis Summary file).

     Sub Macro1()
    '
    ' Macro1 Macro
    '
    
    '   Windows("Analysis_Report_0001.xlsx").Activate 
        Range("Q3:Q10").Select
        Selection.Copy
        Windows("Analyses Summary.xls").Activate
        Range("C2").Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=False
        Application.CutCopyMode = False
    
    End Sub
    What I need to do is to have a macro nested in the Analysis Summary file, that would activate any OPEN workbook that contains the "Analysis_Report" string in its file name, copy the selected range, and paste values to the first empty cell in the column C.

    Now, I don't need the macro to open the Analysis Report file, as I already have it open, and I need to do without references to C: stuff - the path would change every month anyway.

    I've spent the last two days comming out with a solution, and I'm going gray by now :-)

    Any help will be much appreciated.
    Last edited by Kitko; 01-25-2013 at 08:50 AM.

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to copy values from one OPEN workbook (with variable file name) to another

    Please wrap the code in your post with the CODE tags. Select your code and click on the # button.
    If you like my contribution click the star icon!

  3. #3
    Registered User
    Join Date
    01-25-2013
    Location
    Slovakia
    MS-Off Ver
    Excel 2011 Mac
    Posts
    7

    Re: How to copy values from one OPEN workbook (with variable file name) to another

    Thanks, done, won't happen again :-)

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: How to copy values from one OPEN workbook (with variable file name) to another

    Maybe:

    Sub Kitko()
    Dim wb As Workbook
    Dim Abk As Workbook
    Dim x As String
    
    x = "Analysis_Report_"
    For Each wb In Workbooks
    If Left(wb.Name, 16) = x Then
    wb.Activate
    End If
    Next wb
    Set Abk = ActiveWorkbook
    
    End Sub
    This gives the Workbook which contains Analysis_Report a variable (Abk) for you to work with as long as you don't have two Analysis_Reports open at the same time.

  5. #5
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to copy values from one OPEN workbook (with variable file name) to another

    Try this

    Public Sub UpdateAnalysisSummary()
    '#
    '# declare private variables
    '#
         Dim xlsWorkbook As Excel.Workbook
    '#
    '# loop for all open workbooks and select those workbooks where the name
    '# of the workbook contains 'Analysis_Report'
    '#
         For Each xlsWorkbook In Application.Workbooks
              If xlsWorkbook.Name Like "Analysis_Report*" Then
                   With ThisWorkbook
                        .Cells(.Rows.Count, "C").End(xlUp).Offset(1).Resize(8).Value = xlsWorkbook.Range("Q3:Q10").Value
                   End With
              End If
         Next xlsWorkbook
    End Sub

  6. #6
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to copy values from one OPEN workbook (with variable file name) to another

    Try this

    Public Sub UpdateAnalysisSummary()
    '#
    '# declare private variables
    '#
         Dim xlsWorkbook As Excel.Workbook
    '#
    '# loop for all open workbooks and select those workbooks where the name
    '# of the workbook contains 'Analysis_Report'
    '#
         For Each xlsWorkbook In Application.Workbooks
              If xlsWorkbook.Name Like "Analysis_Report*" Then
                   With ThisWorkbook.Activesheet
                        .Cells(.Rows.Count, "C").End(xlUp).Offset(1).Resize(8).Value = xlsWorkbook.ActiveSheet.Range("Q3:Q10").Value
                   End With
              End If
         Next xlsWorkbook
    End Sub
    Last edited by OllieB; 01-25-2013 at 09:02 AM.

  7. #7
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: How to copy values from one OPEN workbook (with variable file name) to another

    @Kitko,

    Please test with the code from post #5, there were some typing errors which I have corrected.

  8. #8
    Registered User
    Join Date
    01-25-2013
    Location
    Slovakia
    MS-Off Ver
    Excel 2011 Mac
    Posts
    7

    Re: How to copy values from one OPEN workbook (with variable file name) to another

    Quote Originally Posted by OllieB View Post
    @Kitko,

    Please test with the code from post #5, there were some typing errors which I have corrected.
    Thanx OllieB, that worked like a charm!!!

  9. #9
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: How to copy values from one OPEN workbook (with variable file name) to another

    Maybe this

    Sub Macro1()
     Const sSheetName As String = "Name of worksheet to paste to" '<-- Change here for your needs
     Dim wb As Workbook
    
        For Each wb In Workbooks
            If wb.Name Like "Analysis_Report*" Then
                With ThisWorkbook.Worksheets(sSheetName)
                    wb.Range("Q3:Q10").Copy .Cells(Rows.Count, "c").End(xlUp).Offset(1)
                End With
            End If
        Next
    End Sub
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

+ 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