Copy data Dynamically to another workbook.

saprater

New Member
Joined
Oct 25, 2017
Messages
5
Hello all,
I have a situation where i have multiple plants sending me files that i copy and paste into a master workbook with all the plants info. I have found ways to copy from workbook 1 to workbook 2, but only with specific ranges or an entire sheet (sort of hard coded). What i want to do is take a file and copy the certain cell ranges based on the Row header in Column A. Below is an example of a sheet i would get from a plant. The line items are in the 100's, and not always in the same order. So what i want to do, is take Column A, and read maintenance and then copy the cell data in b3 to f3 to another workbook. The target workbook may have maintenance in Row 6 instead of row 3, so it would find Maintenance in the target sheet and then paste the data in that row.
I have built some user forms with VB, and researched the copy features, but do not understand how to dynamically place the Data into the target sheet. Can anyone point me in a direction, if this is even possible?

Thanks in Advance.



MondayTuesdayWednesdayThursdayFriday
Maintenance6598205208365
Supplies556058080595
Labor52006300360036003600
Materials800800600600900

<colgroup><col><col span="2"><col><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Here is an example, there were a few unknowns in your question so this will need to be adjusted to work for you but should be a good start.

Assuming you have 2 files, one named "CopyFrom.xlsm" and one named "CopyTo.xlsm" and you have data in the Copy From File. Your CopyTo file should have the headers but no data, if it has data it will be overwritten.

Copy From
MonTuesWedThurFri
Maintenance5565758595
Supplies3222101516
Labor2327295312
Materials1019504965

<tbody>
</tbody>

Copy To

MonTuesWedThurFri
Supplies
Matrerials
Labor
Maintenance

<tbody>
</tbody>


Code:
Sub CopyData()


Dim CopyF As Workbook   ' Copy From Workbook
Dim CopyT As Workbook   ' Copy To Workbook


Dim myRange1 As String  ' Copy Range
Dim myRange2 As String  ' Paste Range


Dim CheckVal1 As String ' Store Value to compare 1
Dim CheckVal2 As String ' Store Value to compare 2


Set CopyF = Workbooks("CopyFrom.xlsm") ' Set Varialble
Set CopyT = Workbooks("CopyTo.xlsm")    ' Set Varialble


myRange1 = CopyF.Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row ' Identify Range to check from
myRange2 = CopyT.Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row  ' Identify Range to check to


' Run through the options in the copy from list
For i = 2 To myRange1
' Capture the name from the Copy From sheet, used to compare and determine where to paste
CheckVal1 = CopyF.Sheets("Sheet1").Range("A" & i).Value


' Internal loop that will run through the options in the copy to sheet until it find a match
' or hits the end of the list
For e = 2 To myRange2


' Capturing the value to compare with
CheckVal2 = CopyT.Sheets("Sheet1").Range("A" & e).Value
' If it finds a match then copy Columns B to F and move on to looking for the next item.
If CheckVal1 = CheckVal2 Then
CopyT.Sheets("Sheet1").Range("B" & e & ":F" & e).Value = CopyF.Sheets("Sheet1").Range("B" & i & ":F" & i).Value
Exit For
End If


Next e


Next i


Set CopyF = Nothing
Set CopyT = Nothing


End Sub
 
Upvote 0
Another option
Code:
Sub CopyData()
   Dim Fnd As Range
   Dim Ary As Variant
   Dim Ws1 As Worksheet
   Dim Ws2 As Worksheet
   Dim i As Long
   
   Set Ws1 = Workbooks("[COLOR=#ff0000]Book1.xlsm[/COLOR]").Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")
   Set Ws2 = Workbooks("[COLOR=#0000ff]Master.xlsm[/COLOR]").Sheets("[COLOR=#0000ff]Sheet1[/COLOR]")
   Ary = Ws1.Range("A2", Ws1.Range("A" & Rows.Count).End(xlUp))
   For i = 1 To UBound(Ary)
      Set Fnd = Ws2.Range("A:A").Find(Ary(i, 1), , , xlWhole, , , False, , False)
      If Not Fnd Is Nothing Then Ws1.Range("B" & i + 1).Resize(, 5).Copy Fnd.Offset(, 1)
      Set Fnd = Nothing
   Next i
End Sub
Change the values in red to match the workbook/sheet you are copying from & the values in blue to the workbook/sheet you want to copy to.
 
Upvote 0
WOW that was nice, I just set that up and it worked perfect.

Many Thanks!!

Here is an example, there were a few unknowns in your question so this will need to be adjusted to work for you but should be a good start.

Assuming you have 2 files, one named "CopyFrom.xlsm" and one named "CopyTo.xlsm" and you have data in the Copy From File. Your CopyTo file should have the headers but no data, if it has data it will be overwritten.

Copy From
MonTuesWedThurFri
Maintenance5565758595
Supplies3222101516
Labor2327295312
Materials1019504965

<tbody>
</tbody>

Copy To

MonTuesWedThurFri
Supplies
Matrerials
Labor
Maintenance

<tbody>
</tbody>


Code:
Sub CopyData()


Dim CopyF As Workbook   ' Copy From Workbook
Dim CopyT As Workbook   ' Copy To Workbook


Dim myRange1 As String  ' Copy Range
Dim myRange2 As String  ' Paste Range


Dim CheckVal1 As String ' Store Value to compare 1
Dim CheckVal2 As String ' Store Value to compare 2


Set CopyF = Workbooks("CopyFrom.xlsm") ' Set Varialble
Set CopyT = Workbooks("CopyTo.xlsm")    ' Set Varialble


myRange1 = CopyF.Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row ' Identify Range to check from
myRange2 = CopyT.Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row  ' Identify Range to check to


' Run through the options in the copy from list
For i = 2 To myRange1
' Capture the name from the Copy From sheet, used to compare and determine where to paste
CheckVal1 = CopyF.Sheets("Sheet1").Range("A" & i).Value


' Internal loop that will run through the options in the copy to sheet until it find a match
' or hits the end of the list
For e = 2 To myRange2


' Capturing the value to compare with
CheckVal2 = CopyT.Sheets("Sheet1").Range("A" & e).Value
' If it finds a match then copy Columns B to F and move on to looking for the next item.
If CheckVal1 = CheckVal2 Then
CopyT.Sheets("Sheet1").Range("B" & e & ":F" & e).Value = CopyF.Sheets("Sheet1").Range("B" & i & ":F" & i).Value
Exit For
End If


Next e


Next i


Set CopyF = Nothing
Set CopyT = Nothing


End Sub
 
Upvote 0

Forum statistics

Threads
1,216,445
Messages
6,130,685
Members
449,585
Latest member
Nattarinee

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