Solved

copy defined range from one workbook to another

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to compare data in excel 5 27
Import data into excel from web page 10 25
MIN, using ARRAY 4 16
excel time calculation 11 22
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

22 Experts available now in Live!

Get 1:1 Help Now