Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

copy defined range from one workbook to another

Posted on 2014-01-03
4
Medium Priority
?
305 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 1600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

618 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