Solved

copy defined range from one workbook to another

Posted on 2014-01-03
4
277 Views
Last Modified: 2014-01-12
I have 2 workbooks "A" and "B"

Workbook "A" contains many worksheets with data in the first 6 columns. Column A contains the date and time.

Wb"B" contains a few worksheets with identical names to the ones in Wb"A"


The macro should find two similarly named worksheets and then search column A for the latest date. If the date in WbA is later then the one in WbB (it always will be), then the macro should copy the missing data into WbB to match the date. It should do this for all the worksheets in WbB.

i.e.
- in both workbooks there is a Sheet named "EURUSD"
- in WbA the date/time is 31/12/13 16:00  row 2000
- in WbB the date/time is 29/12/13 16:00  row 60
- The macro should copy the missing data of columns (A-F) in Wb"B" to row 60 in Wb"A" (12 rows)
- then repeat for all worksheets in WbA

Thanks,
CC
0
Comment
Question by:CC10
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Expert Comment

by:Harry Lee
ID: 39754744
CC10,

Please upload sample files.
0
 

Author Comment

by:CC10
ID: 39754883
Here you go
B.xlsm
A.xlsm
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 400 total points
ID: 39774418
I think this should do the job. As it is, it needs both workbooks open (as the other macros in there suggest that's already the case). The code can be put in either one or a seperate workbook like I have attached.
Const C_WORKBOOK_A = "a.xlsm"
Const C_WORKBOOK_B = "b.xlsm"

Sub CopyA2B()
    Dim wba As Workbook
    Dim sha As Worksheet
    Dim shb As Worksheet
    Dim rwa As Integer
    Dim rwb As Integer
    Set wba = Workbooks(C_WORKBOOK_A)
    For Each shb In Workbooks(C_WORKBOOK_B).Sheets
        Debug.Print "sheet: " & shb.Name
        On Error Resume Next
        Set sha = wba.Sheets(shb.Name)
        On Error GoTo 0
        If Not sha Is Nothing Then
            rwb = shb.Cells(shb.Rows.Count, 1).End(xlUp).Row
            rwa = sha.Cells(sha.Rows.Count, 1).End(xlUp).Row
            Debug.Print "< B " & rwb & ": " & shb.Cells(rwb, 1) & " " & Format(shb.Cells(rwb, 2), "hh:mm")
            Debug.Print "< A " & rwa & ": " & sha.Cells(rwa, 1) & " " & Format(sha.Cells(rwa, 2), "hh:mm")
            While sha.Cells(rwa, 1).Value > shb.Cells(rwb, 1).Value Or (sha.Cells(rwa, 1).Value = shb.Cells(rwb, 1).Value And sha.Cells(rwa, 2).Value > shb.Cells(rwb, 2).Value)
                rwa = rwa - 1
            Wend
            Debug.Print "> A " & rwa & ": " & sha.Cells(rwa, 1) & " " & Format(sha.Cells(rwa, 2), "hh:mm") & " (" & (sha.Cells(sha.Rows.Count, 1).End(xlUp).Row - rwa) & " new rows)"
            While rwa < sha.Rows.Count And sha.Cells(rwa + 1, 1).Value <> ""
                rwa = rwa + 1
                rwb = rwb + 1
                'Debug.Print "copy A " & rwa & " to B " & rwb
                sha.Range(sha.Cells(rwa, 1), sha.Cells(rwa, 6)).Copy shb.Cells(rwb, 1)
            Wend
        End If
        Set sha = Nothing
    Next
    Set wba = Nothing
End Sub

Open in new window

The output when I ran it with your sample files:
sheet: EURUSD
< B 3895: 29/11/2013 20:00
< A 3449: 03/01/2014 00:00
> A 3295: 29/11/2013 20:00 (154 new rows)
sheet: USDCHF
< B 3895: 29/11/2013 20:00
< A 3450: 03/01/2014 00:00
> A 3295: 29/11/2013 20:00 (155 new rows)
sheet: GBPUSD
< B 3895: 29/11/2013 20:00
< A 3448: 03/01/2014 00:00
> A 3295: 29/11/2013 20:00 (153 new rows)

Open in new window

Seems to suggest different data than what you reference in your question but looks ok to me.
C.xlsm
0
 

Author Comment

by:CC10
ID: 39774640
Yes, that works perfectly. Thanks very much for your help.
CC
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My experience with Windows 10 over a one year period and suggestions for smooth operation
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question