Solved

copy defined range from one workbook to another

Posted on 2014-01-03
4
258 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
  • 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

815 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now