I have 2 workbooks and need to copy over data from rows that have a "yes" in the 26th column to specific cells in the destination workbook. I currently have the following code attached to a button on the source sheet:
Sub exportData()
Dim LastRow As Integer
Dim i As Integer
Dim erow As Integer
LastRow = ActiveSheet.Range("A" & rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Cells(i, 26).Value = "Yes" Then
Range(Cells(i, 1), Cells(i, 26)).Select
Selection.Copy
Workbooks.Open Filename:=ThisWorkbook.Path & "\GI New Starter Tracker 2017edit.xlsx"
Worksheets("Main").Select
erow = ActiveSheet.Cells(rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.Paste
'ActiveSheet.Range("$A$1:$AB$3000").RemoveDuplicates Columns:=2, Header:=xlYes
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CutCopyMode = False
End If
Next i
End Sub
The code works fine, however it copies over the entire row, but I only need to copy over certain information like "firstname", "Surname" "Date of birth". The target workbook doesnt have exactly the same headings so I need to be able to specify which column.
Ive been pulling my hair out and would really appreciate any help.
Thanks :)
This should do it. You'll need to change the column number to the corresponding columns in your workbooks....
Sub exportData()
Dim LastRow As Integer
Dim i As Integer
Dim erow As Integer
Dim wbk As Workbook
Dim SourceSheet As Worksheet
Dim DestSheet As Worksheet
Dim firstName, surName, DoB
Set SourceSheet = ActiveSheet
LastRow = SourceSheet.Range("A" & Rows.Count).End(xlUp).Row
Set wbk = Workbooks.Open(ThisWorkbook.Path & "\GI New Starter Tracker 2017edit.xlsx")
Set DestSheet = wbk.Sheets("Main")
For i = 2 To LastRow
If SourceSheet.Cells(i, 26).Value = "Yes" Then
'change the column numbers to the relevant number
firstName = SourceSheet.Cells(i, 23).Value
surName = SourceSheet.Cells(i, 24).Value
DoB = SourceSheet.Cells(i, 25).Value
erow = DestSheet.Cells(DestSheet.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
'change the column numbers to the relevant number
DestSheet.Cells(erow, 10).Value = firstName
DestSheet.Cells(erow, 11).Value = surName
DestSheet.Cells(erow, 12).Value = DoB
End If
Next i
wbk.Save
wbk.Close
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.