Code to transfer data from one workbook to another

Fergus

Well-known Member
Joined
Mar 10, 2004
Messages
1,174
I have one workbook called "Records.xlsx" in column A from A2 downwards are a list of reference codes, look like G1.01.010 etc. In row 1 starting from C1 are a series of numbers. In each column C, D, E etc from row 2 downwards are quantities, ie. numbers.

I have a second workbook called "Invoice.xlsx" in column A from A2 downwards there are reference codes. The codes will be some, but not all of the codes in "Records.xlsx" and not necessarily in the same order. I want to be able to enter a number into cell C1 of "Invoice.xlsx" which will then lookup each code in the first column, then go to "Records.xlsx" find the column which corresponds to the number in C1 and then the quantity in the row that corresponds to the code in column A and enter that number into "Invoice.xlsx".

I want to be able to activate this from a button on "Invoice.xlsx"

Can anyone suggest a code that could do this?
 
Last edited:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Cross posted http://www.vbaexpress.com/forum/sho...to-transfer-data-from-one-workbook-to-another

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Sorry about that. It's more than 8 years since I last posted here and the rules have changed.
 
Upvote 0
How about
Code:
Sub UpdateInvoice()
   Dim Invws As Worksheet
   Dim Recws As Worksheet
   Dim Cl As Range
   Dim Col As Variant
   
   Set Invws = ThisWorkbook.Sheets("Detail")
   Set Recws = Workbooks("Records.xlsm").Sheets("Quants")
   
   Col = Application.Match(Invws.Cells(1, 3), Recws.Rows(1), False)
   If IsError(Col) Then Exit Sub
   With CreateObject("scripting.dictionary")
      For Each Cl In Recws.Range("A2", Recws.Range("A" & Rows.Count).End(xlUp))
         .Item(Cl.Value) = Cl.Offset(, Col - 1).Value
         Debug.Print .Item(Cl.Value)
      Next Cl
      For Each Cl In Invws.Range("A2", Invws.Range("A" & Rows.Count).End(xlUp))
         Cl.Offset(, 2).Value = .Item(Cl.Value)
      Next Cl
   End With
End Sub
 
Upvote 0
That's brilliant, thank you very much. I am still trying to work out how it does that. I don't understand how the CreateObject("scripting.dictionary") works, especially as in my real project I have 3500 rows in both the Records file and the Invoice file. It must somehow create a huge store before copying back into the Invoice but I cannot see how it does that!

I managed to add to the code to make it run whenever the cell containing the number on the invoice is changed.

So many thanks
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top