macro to transfer cell data to another worksheet

XQ4585

New Member
Joined
Feb 4, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
1644483607159.png

i am figuring out vba code to transfer data from one workbook to another workbook, wish someone could help me. Thanks.
Left hand side was Book1 with sheet name Data while right hand side was Book2 with sheet name Done. I wish to move data from Book1 when "confirmed by" show "HOD" and paste to Book2.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi,

Please use below code in Worksheet Data of Book1.
  • Keep both workbook open.
  • When you type HOD in Column E of Done sheet that row will be copied to Done sheet of Book2.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim wb As Workbook
Dim targetRow As Integer
Set wb = Workbooks("Book2.xlsx")
targetRow = wb.Sheets("Done").Cells(Rows.Count, 1).End(xlUp).Row + 1
If Target.CountLarge > 1 Then Exit Sub
If Not Intersect(Target, Range("E:E")) Is Nothing Then
    If Range("E" & Target.Row) = "HOD" Then
        ActiveSheet.Range(Target.Row & ":" & Target.Row).Copy wb.Sheets("Done").Range("A" & targetRow)
    End If
End If

End Sub
 
Upvote 0
VBA Code:
Sub XQ()
     Dim sH1, sH2, LO1, LO2

     Set sH1 = Workbooks("book1.xlsm").Sheets("data")              'your sourcesheet, check names
     Set sH2 = Workbooks("book2.xlsm").Sheets("Done")              'your destinationsheet, check names
     Set LO1 = sH1.Range("A1").ListObject                       'listobject in the source
     Set LO2 = sH2.Range("A1").ListObject                       'listobject in the destination

     With LO1
          .Range.AutoFilter 5, "HOD"                            'filter source on "HOD" in the 5th column
          If .Range.Columns(1).SpecialCells(xlVisible).Count > 1 Then     'at least 1 visible row in the source
               .DataBodyRange.Copy 'copy visible cells
               LO2.Range.Cells(LO2.ListRows.Count + 2, 1).PasteSpecial xlValues
          End If
     End With
End Sub
 
Upvote 0
Hi,

Please use below code in Worksheet Data of Book1.
  • Keep both workbook open.
  • When you type HOD in Column E of Done sheet that row will be copied to Done sheet of Book2.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim wb As Workbook
Dim targetRow As Integer
Set wb = Workbooks("Book2.xlsx")
targetRow = wb.Sheets("Done").Cells(Rows.Count, 1).End(xlUp).Row + 1
If Target.CountLarge > 1 Then Exit Sub
If Not Intersect(Target, Range("E:E")) Is Nothing Then
    If Range("E" & Target.Row) = "HOD" Then
        ActiveSheet.Range(Target.Row & ":" & Target.Row).Copy wb.Sheets("Done").Range("A" & targetRow)
    End If
End If

End Sub
Thankyou so much
 
Upvote 0
VBA Code:
Sub XQ()
     Dim sH1, sH2, LO1, LO2

     Set sH1 = Workbooks("book1.xlsm").Sheets("data")              'your sourcesheet, check names
     Set sH2 = Workbooks("book2.xlsm").Sheets("Done")              'your destinationsheet, check names
     Set LO1 = sH1.Range("A1").ListObject                       'listobject in the source
     Set LO2 = sH2.Range("A1").ListObject                       'listobject in the destination

     With LO1
          .Range.AutoFilter 5, "HOD"                            'filter source on "HOD" in the 5th column
          If .Range.Columns(1).SpecialCells(xlVisible).Count > 1 Then     'at least 1 visible row in the source
               .DataBodyRange.Copy 'copy visible cells
               LO2.Range.Cells(LO2.ListRows.Count + 2, 1).PasteSpecial xlValues
          End If
     End With
End Sub
Thanks a lot
 
Upvote 0

Forum statistics

Threads
1,215,778
Messages
6,126,841
Members
449,343
Latest member
DEWS2031

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