juniorb2002
New Member
- Joined
- Jul 4, 2021
- Messages
- 1
- Office Version
- 2016
- Platform
- 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
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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | |||
1 | Name | Phone | House no | Locality | City | Zip/Pin | ||||||
2 | 30/4/2021 0:15 | 29213330 | w 2 | GK 1 | New Delhi | 110048 | ||||||
3 | 30/4/2021 0:30 | 29213341 | w 3 | Gk 2 | New Delhi | 110049 | ||||||
4 | 30/4/2021 0:45 | 29213352 | w 4 | GK 3 | New Delhi | 110050 | ||||||
5 | 30/4/2021 1:00 | 29213363 | w 5 | GK 4 | New Delhi | 110051 | ||||||
6 | 30/4/2021 1:15 | 29213374 | w 6 | GK 5 | New Delhi | 110052 | ||||||
7 | 30/4/2021 1:30 | 29213385 | w 7 | GK 6 | New Delhi | 110053 | ||||||
8 | 30/4/2021 1:45 | 29213396 | w 8 | GK 7 | New Delhi | 110054 | ||||||
9 | 30/4/2021 2:00 | 29213407 | w 9 | GK 8 | New Delhi | 110055 | ||||||
10 | 30/4/2021 2:15 | 29213418 | w 10 | GK 9 | New Delhi | 110056 | ||||||
11 | 30/4/2021 2:30 | 29213429 | w 11 | GK 10 | New Delhi | 110057 | ||||||
12 | 30/4/2021 2:45 | 29213440 | w 12 | GK 11 | New Delhi | 110058 | ||||||
13 | 30/4/2021 3:00 | 29213451 | w 13 | GK 12 | New Delhi | 110059 | ||||||
14 | 30/4/2021 3:15 | 29213462 | w 14 | GK 13 | New Delhi | 110060 | ||||||
15 | 30/4/2021 3:30 | 29213473 | w 15 | GK 14 | New Delhi | 110061 | ||||||
16 | 30/4/2021 3:45 | 29213484 | w 16 | GK 15 | New Delhi | 110062 | ||||||
17 | 30/4/2021 4:00 | 29213495 | w 17 | GK 16 | New Delhi | 110063 | ||||||
18 | 30/4/2021 4:15 | 29213506 | w 18 | GK 17 | New Delhi | 110064 | ||||||
19 | 30/4/2021 4:30 | 29213517 | w 19 | GK 18 | New Delhi | 110065 | ||||||
20 | 30/4/2021 4:45 | 29213528 | w 20 | GK 19 | New Delhi | 110066 | ||||||
21 | 30/4/2021 5:00 | 29213539 | w 21 | GK 20 | New Delhi | 110067 | ||||||
22 | 30/4/2021 5:15 | 29213550 | w 22 | GK 21 | New Delhi | 110068 | ||||||
23 | 30/4/2021 5:30 | 29213561 | w 23 | GK 22 | New Delhi | 110069 | ||||||
24 | 30/4/2021 5:45 | 29213572 | w 24 | GK 23 | New Delhi | 110070 | ||||||
25 | 30/4/2021 6:00 | 29213583 | w 25 | GK 24 | New Delhi | 110071 | ||||||
26 | 30/4/2021 6:15 | 29213594 | w 26 | GK 25 | New Delhi | 110072 | ||||||
27 | 30/4/2021 6:30 | 29213605 | w 27 | GK 26 | New Delhi | 110073 | ||||||
Sheet1 |
Copy Sheet and transfer specific data.xlsm | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
1 | Name | Mobile | Birthday | Phone | House no | Locality | City | Zip/Pin | ||
2 | 30/4/2021 0:00 | 29213330 | 1-Dec-80 | |||||||
3 | 30/4/2021 0:30 | 29213341 | 2-Dec-80 | |||||||
4 | 30/4/2021 1:00 | 29213352 | 3-Dec-80 | |||||||
5 | 30/4/2021 1:30 | 29213363 | 4-Dec-80 | |||||||
6 | 30/4/2021 2:00 | 29213374 | 5-Dec-80 | |||||||
7 | 30/4/2021 2:30 | 29213462 | 6-Dec-80 | |||||||
8 | 30/4/2021 3:00 | 29213473 | 7-Dec-80 | |||||||
9 | 30/4/2021 3:30 | 29213484 | 8-Dec-80 | |||||||
10 | 30/4/2021 4:00 | 29213495 | 9-Dec-80 | |||||||
11 | 30/4/2021 4:30 | 29213506 | 10-Dec-80 | |||||||
12 | 30/4/2021 5:00 | 29213517 | 11-Dec-80 | |||||||
13 | 30/4/2021 5:30 | 29213528 | 12-Dec-80 | |||||||
14 | 30/4/2021 6:00 | 29213539 | 13-Dec-80 | |||||||
15 | ||||||||||
16 | ||||||||||
Sheet2 |