Excel Vba to cut and paste data to other workbook if condition has been met

XQ4585

New Member
Joined
Feb 4, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I have collected data in Sheet A in Workbook A, i need to transfer data to Sheet B in Workbook B daily, so i need vba to detect if one of the column "Status" has showed value "Done", and then excel vba transfer data. Please help, i cant figure out how to write the code......Thanks.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hi,

Please check below code:

VBA Code:
Sub copyData()
Dim lastRow As Integer, rowno As Integer
Dim RowToCopy As Integer
Dim wb As Workbook

lastRow = ThisWorkbook.Sheets("SheetA").Cells(Rows.Count, 1).End(xlUp).Row
Set wb = Workbooks("result.xlsx")

For rowno = 1 To lastRow
    RowToCopy = wb.Sheets("SheetB").Cells(Rows.Count, 1).End(xlUp).Row + 1
    If ThisWorkbook.Sheets("SheetA").Range("L" & rowno) = "Done" Then
        ThisWorkbook.Sheets("SheetA").Range(rowno & ":" & rowno).Copy wb.Sheets("SheetB").Range(RowToCopy & ":" & RowToCopy)
    End If
Next
MsgBox "Data copied successfully", vbInformation
End Sub
 
Upvote 0
Hi,

Please check below code:

VBA Code:
Sub copyData()
Dim lastRow As Integer, rowno As Integer
Dim RowToCopy As Integer
Dim wb As Workbook

lastRow = ThisWorkbook.Sheets("SheetA").Cells(Rows.Count, 1).End(xlUp).Row
Set wb = Workbooks("result.xlsx")

For rowno = 1 To lastRow
    RowToCopy = wb.Sheets("SheetB").Cells(Rows.Count, 1).End(xlUp).Row + 1
    If ThisWorkbook.Sheets("SheetA").Range("L" & rowno) = "Done" Then
        ThisWorkbook.Sheets("SheetA").Range(rowno & ":" & rowno).Copy wb.Sheets("SheetB").Range(RowToCopy & ":" & RowToCopy)
    End If
Next
MsgBox "Data copied successfully", vbInformation
End Sub
Hi, thanks for reply, here a image may more clear to reach out what i try to explain. i have Workbook name "Book1" and sheet name "Data" i wish to copy the data when "Confirmed by" column show "HOD" and paste to another worksheet name "Book2" sheet name "Done". Im trying to write vba code but does not work....
1644483954914.png
 
Upvote 0
Hi, Seems it's a duplicate post.

Meanwhile use below code in Data Sheet of Book1. keep both workbook open.
When you type HOD, the row will be copied in 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
Hi, Seems it's a duplicate post.

Meanwhile use below code in Data Sheet of Book1. keep both workbook open.
When you type HOD, the row will be copied in 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
Thanks a lot
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,048
Members
449,206
Latest member
Healthydogs

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