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
Solved

copy defined range from one workbook to another

Posted on 2014-01-03
4
270 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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

792 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