Copy data from one worbook to another workbook while specific row data and column headings.

juniorb2002

New Member
Joined
Jul 4, 2021
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone I would like to copy data from one workbook to another. They data should match by the date column information which will be the leftmost column in both workbooks. However the dates in both date columns are not exact, one could be in 15 minute intervals(the source) while the other is in 1/2 hour intervals. The destination column is in 1/2 hour interval, I want to copy the source book information that matches the 1/2 hour intervals in my sheet. The destination workbook headers may not be in the same exact columns and may be less than the source workbook columns, hence the need to match the data with the headers as well. The way it is set up now the Source heading and Destination has to be the same order and same column count copying over or it will give an error or spill more data than necessary data. I have posted my code and a sample minisheet below. I would greatly any assistance with this. If this can be solved I will post my actual sheets to provide feedback thanks.

Sub transfer()
Application.ScreenUpdating = False
Dim i As Long, j As Long, lastrow1 As Long, lastrow2 As Long, lCol1 As Long, lCol2 As Long, header As Range, foundHeader As Range
Dim mydate As Date
Dim srcWS As Worksheet, desWS As Worksheet

lastrow1 = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To lastrow1
mydate = Sheets("sheet1").Cells(i, "A").Value

Sheets("sheet2").Activate
lastrow2 = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row

For j = 2 To lastrow2

If Sheets("sheet2").Cells(j, "A").Value = mydate Then
Set srcWS = ThisWorkbook.Worksheets("Sheet1")
Set desWS = ThisWorkbook.Worksheets("Sheet2")

lCol1 = srcWS.Cells(1, Columns.Count).End(xlToLeft).Column
lCol2 = desWS.Cells(1, Columns.Count).End(xlToLeft).Column

For Each header In desWS.Range(desWS.Cells(1, 4), desWS.Cells(1, lCol2))
Set foundHeader = srcWS.Rows(1).Find(header, LookIn:=xlValues, lookat:=xlWhole)
If Not foundHeader Is Nothing Then
srcWS.Range(srcWS.Cells(i, foundHeader.Column), srcWS.Cells(i, lCol1)).Copy
desWS.Range(desWS.Cells(j, header.Column), desWS.Cells(j, lCol2)).PasteSpecial xlPasteValues
End If
Next header
End If

Next j
Application.CutCopyMode = False
Application.ScreenUpdating = True
Next i
Sheets("sheet1").Activate
Sheets("sheet1").Range("A1").Select


End Sub

Copy Sheet and transfer specific data.xlsm
ABCDEFGHIJ
1NamePhoneHouse noLocalityCityZip/Pin
230/4/2021 0:1529213330w 2GK 1New Delhi110048
330/4/2021 0:3029213341w 3Gk 2New Delhi110049
430/4/2021 0:4529213352w 4GK 3New Delhi110050
530/4/2021 1:0029213363w 5GK 4New Delhi110051
630/4/2021 1:1529213374w 6GK 5New Delhi110052
730/4/2021 1:3029213385w 7GK 6New Delhi110053
830/4/2021 1:4529213396w 8GK 7New Delhi110054
930/4/2021 2:0029213407w 9GK 8New Delhi110055
1030/4/2021 2:1529213418w 10GK 9New Delhi110056
1130/4/2021 2:3029213429w 11GK 10New Delhi110057
1230/4/2021 2:4529213440w 12GK 11New Delhi110058
1330/4/2021 3:0029213451w 13GK 12New Delhi110059
1430/4/2021 3:1529213462w 14GK 13New Delhi110060
1530/4/2021 3:3029213473w 15GK 14New Delhi110061
1630/4/2021 3:4529213484w 16GK 15New Delhi110062
1730/4/2021 4:0029213495w 17GK 16New Delhi110063
1830/4/2021 4:1529213506w 18GK 17New Delhi110064
1930/4/2021 4:3029213517w 19GK 18New Delhi110065
2030/4/2021 4:4529213528w 20GK 19New Delhi110066
2130/4/2021 5:0029213539w 21GK 20New Delhi110067
2230/4/2021 5:1529213550w 22GK 21New Delhi110068
2330/4/2021 5:3029213561w 23GK 22New Delhi110069
2430/4/2021 5:4529213572w 24GK 23New Delhi110070
2530/4/2021 6:0029213583w 25GK 24New Delhi110071
2630/4/2021 6:1529213594w 26GK 25New Delhi110072
2730/4/2021 6:3029213605w 27GK 26New Delhi110073
Sheet1



Copy Sheet and transfer specific data.xlsm
ABCDEFGH
1NameMobileBirthdayPhoneHouse noLocalityCityZip/Pin
230/4/2021 0:00292133301-Dec-80
330/4/2021 0:30292133412-Dec-80
430/4/2021 1:00292133523-Dec-80
530/4/2021 1:30292133634-Dec-80
630/4/2021 2:00292133745-Dec-80
730/4/2021 2:30292134626-Dec-80
830/4/2021 3:00292134737-Dec-80
930/4/2021 3:30292134848-Dec-80
1030/4/2021 4:00292134959-Dec-80
1130/4/2021 4:302921350610-Dec-80
1230/4/2021 5:002921351711-Dec-80
1330/4/2021 5:302921352812-Dec-80
1430/4/2021 6:002921353913-Dec-80
15
16
Sheet2
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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